Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
GTween, multiple tween chaining and repeating them. — Gideros Forum

GTween, multiple tween chaining and repeating them.

DenisiDenisi Member
edited January 2016 in General questions
Ive been trying to chain tweens with functions but it got too messy. Is there any way to chain two tweens after one is complete and then repeat the whole chain?

Comments

  • antixantix Member
    edited January 2016 Accepted Answer
    I myself came across this very issue a while ago. Some timelined event firing system seemed to be what was missing, so I created a simple one..
    --[[
     
    Sequencer v1.0 - A simple event dispatcher for timelined events
     
    (c) 2015 Antix Software
     
    --]]
     
    Sequencer = Core.class()
     
    function Sequencer:init()
      self.timer = Timer.new(0, 1)
      self.nextEvent = nil
      self.sequence = nil
      self.timer:addEventListener(Event.TIMER, self.advanceEvent, self)
    end
     
    function Sequencer:shutDown()
      self.timer:stop()
      self.timer:removeEventListener(Event.TIMER, self.advanceEvent, self)
      self.timer = nil
      self.sequence = nil
      self.nextEvent = nil
    end
     
    function Sequencer:advanceEvent(event)
      self.timer:stop()
      self.timer:reset()
     
      self.sequence[self.nextEvent][2]() -- HERE WE ACTUALLY FIRE THE EVENT
     
      local lastTime = self.sequence[self.nextEvent][1]
      self.nextEvent = self.nextEvent + 1
     
      local nextTime = self.sequence[self.nextEvent][1]
      if nextTime ~= 9999 then
        local delay = nextTime - lastTime -- NUMBER OF SECONDS TILL NEXT EVENT
        self.timer:setDelay(1000 * delay)
        self.timer:start()
      else
        self.timer:removeEventListener(Event.TIMER, self.advanceEvent, self) -- IF 9999 THEN THE SEQUENCE IS ENDED
      end
    end
     
    function Sequencer:pause()
      self.timer:stop()
    end
     
    function Sequencer:resume()
      self.timer:start()
    end
     
    function Sequencer:startSequence(eventList)
      self.nextEvent = 1
      self.sequence = eventList
     
      self.timer:setDelay(1000 * eventList[1][1])
      self.timer:start()
    end
    Here is how to use it..
    local function seq_001(event)
      -- START A TWEEN OR DO SOME OTHER THING HERE
    end
    local function seq_002(event)
    end
    local function seq_003(event)
    end
     
    local sequences = {
      {0,     seq_001}, -- FIRST SEQUENCE ALWAYS FIRED IMMEDIATELY
      {1.0 ,  seq_002}, -- THIS STARTS AT 1 SECOND
      {3.00,  seq_003}, -- THIS ONE AT 3 SECONDS
      -- ETC, ETC, ETC
      {9999,  nil} -- SIGNIFIES END OF TIMELINE
    }
     
    local Sequencer = Sequencer.new()
     
    Sequencer:startSequence(sequences)
    I use this class to play cinematics in my programs.

    It is still a wee bit messy but it is much better than trying to string everything together using GTweens inbuilt abilities.

    I know its pretty basic and doesn't have all the functionality that you need but you can always add that :)

    Likes: Denisi

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    @john26 showed me how this could be done with coroutines not long ago, the idea was rather good. Maybe he can explain it here...

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • Also to loop Gtween you need to set repeatCount to less than 0
    GTween.new(sprite, 1, {rotation = 360}, {repeatCount=-1})
    But chaining could look like this:
    local tween1 = GTween.new(sprite, 1, {rotation = 360})
    local tween2 = GTween.new(sprite, 1, {rotation = 0})
    tween1.nextTween = tween2
    tween2.nextTween =tween1

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • MovieClip can be used for consecutive tweens i guess, look for info about it on the forum too.
  • john26john26 Maintainer
    edited February 2016
    Yes, consecutive animations and game logic can be managed with coroutines which gives code which is much more readable than GTween/onComplete which often produces spaghetti code.

    Coroutines let you do animations with for loops and allow things to happen consecutively in the order the code was written. Technically a coroutine is a function that can pick up where it left off when called again, maintaining state. Lua fully supports coroutines.

    See this example:
    function move()
      for x=1,100 do
         sprite:setX(x)
         coroutine.yield()
      end
     
      for y=1,100 do
          sprite:setY(y)
          coroutine.yield()
      end
     
      print ("Animation complete")
    end
     
    function pulse()
      local i=0
     
      while (true) do
        sprite:setAlpha((math.sin(i))^2)
        i=i+0.1
        coroutine.yield()
      end
    end
     
    function update()
      if coroutine.status(co)~="dead" then
         coroutine.resume(co)
      end
      coroutine.resume(copulse)
    end
     
    co=coroutine.create(move)
    copulse=coroutine.create(pulse)
     
    sprite=Shape.new()
    sprite:setFillStyle(Shape.SOLID,0xFF0000)
    sprite:moveTo(0,0)
    sprite:lineTo(100,0)
    sprite:lineTo(100,100)
    sprite:lineTo(0,100)
    sprite:endPath()
     
    stage:addChild(sprite)
    stage:addEventListener(Event.ENTER_FRAME,update)
    This causes a red square to move along the x-axis and then along the y-axis. The movement is handled using function move which is a coroutine. Notice that the movement is done in two "for" loops and these happen one after the other (but you must remember to write coroutine.yield in the body of the loop). The message "Animation complete" is printed only after both coroutines have completed. In other words, "move" behaves just as you would expect on a non-multitasking computer. Coroutines allow you to write code as if you "own" the whole computer like we used to program the Commodore 64 or in MS-DOS for example!

    However, Gideros does not have a built in coroutine manager so you have to create and update the coroutines within an ENTER_FRAME event as shown. But it would be easy to write a manager in Lua I think.

    If you have a complex game with lots of sequential animations coroutines can make your code much more structured IMO. Worth considering.

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.