Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
GTween onComplete question — Gideros Forum

GTween onComplete question

GhandoGhando Member
edited January 2012 in Bugs and issues
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?
Tagged:

Comments

  • atilimatilim Maintainer
    edited January 2012
    You've made a little mistake:
    onComplete=self:doTween2()
    should be
    onComplete=self.doTween2

  • GhandoGhando Member
    edited January 2012
    Well spotted!
    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:
    function myShape:doTween1()
      print("Inside doTween1")
      self.tween1 = GTween.new(self, 2, {x = _W /2 + 50}, {onComplete=self.doTween2})
      print("After GTween new")
    end
    Ok, I'm almost there except now I get an error message and the console shows:

    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
  • atilimatilim Maintainer
    edited January 2012
    oh yes, onComplete doesn't transfer the variable self to the function doTween2. This should work:
    self.tween1 = GTween.new(self, 2, {x = _W /2 + 50}, {})
    self.tween1:addEventListener("complete", self.doTween2, self)
    self.tween1.dispatchEvents = true
    Edit: added dispatchEvents for future reference

  • We've been discussing tweens here:
    www.giderosmobile.com/forum/discussion/328/movie-clips-vs-gtween

    atilim suggested this way of chaining:
    And here is the correct way to chain tweens:
    local tween1 = GTween.new(sprite, 1, {x = 240}, {})
    local tween2 = GTween.new(sprite, 1, {y = 100}, {autoPlay = false})
    local tween3 = GTween.new(sprite, 1, {alpha = 0}, {autoPlay = false})
    tween1.nextTween = tween2
    tween2.nextTween = tween3
    Also this is same:
    local tween3 = GTween.new(sprite, 1, {alpha = 0}, {autoPlay = false})
    local tween2 = GTween.new(sprite, 1, {y = 100}, {autoPlay = false, nextTween = tween3})
    local tween1 = GTween.new(sprite, 1, {x = 240}, {nextTween = tween2})

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
  • Yup, that's it! So for anyone having a similar problem (eg, they also didn't read all the docs :) it's:

    self.tween = GTween.new(...)
    self.tween:addEventListener("complete", callback, parameter)
    self.tween.dispatchEvents = true

    Thanks atilim!

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
  • GhandoGhando Member
    edited January 2012
    Thanks Caroline,
    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:
    	if (self.tween:isPaused() == false) then
    		self.tween:setPaused(true)
    	else
    		self.tween:setPaused(false)
    	end
    and pause my animation regardless of what tween is running.
Sign In or Register to comment.