I'm having problems trying to get my head around "chaining" tweens one after the other.
I've tried using the onComplete value to call a function *after* the first tween has completed but it looks like it immediately runs when the tween is instantiated.
myShape.lua
-----------
myShape = gideros.class(Shape)
function myShape:init()
local bitmap = Bitmap.new(Texture.new("square.png"))
bitmap:setAnchorPoint(0.5, 0.5)
self:addChild(bitmap)
self.isFocus = false
end
function myShape:doTween1()
print("Inside doTween1")
self.tween1 = GTween.new(self, 2, {x = _W /2 + 50, onComplete=self:doTween2()})
print("After GTween new")
end
function myShape:doTween2()
print("Inside doTween2")
self.tween2 = GTween.new(self, 4, {rotation=90})
print("After Gtween 2 new")
end |
main.lua
-------
_W = application:getDeviceWidth()
_H = application:getDeviceHeight()
local shape = myShape.new()
stage:addChild(shape)
shape:setX(_W/2)
shape:setY(_H/2)
shape:doTween1() |
In the player the square immediately starts moving AND rotating and In the console I'm seeing:
Inside doTween1
Inside doTween2
After Gtween 2 new
After GTween new
So what am I doing wrong?
Comments
onComplete=self:doTween2()
should be
onComplete=self.doTween2
Comparing my code to MikeHart's Basic Shooter code I noticed I'd put the onComplete in the values rather than the params so now the function looks like:
Inside doTween1
After GTween new
Inside doTween2
After Gtween 2 new
gtween.lua:386: attempt to call method 'get' (a nil value)
stack traceback:
gtween.lua:386: in function 'init2'
gtween.lua:212: in function 'setPosition'
gtween.lua:81: in function
www.giderosmobile.com/forum/discussion/328/movie-clips-vs-gtween
atilim suggested this way of chaining:
Likes: atilim
self.tween = GTween.new(...)
self.tween:addEventListener("complete", callback, parameter)
self.tween.dispatchEvents = true
Thanks atilim!
Likes: atilim
I'm implementing a "pause tween on mouse_up" and so was trying to work out the best way to chain tweens but still make it easy to pause the currently running tween.
Turns out (at least in the 5 mins of testing I've just done) that if I throw each tween in a function I can just re-use the same self.tween variable which allows me to then do: