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?
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)endfunction Sequencer:shutDown()
self.timer:stop()
self.timer:removeEventListener(Event.TIMER, self.advanceEvent, self)
self.timer =nil
self.sequence =nil
self.nextEvent =nilendfunction Sequencer:advanceEvent(event)
self.timer:stop()
self.timer:reset()
self.sequence[self.nextEvent][2]()-- HERE WE ACTUALLY FIRE THE EVENTlocal lastTime = self.sequence[self.nextEvent][1]
self.nextEvent = self.nextEvent + 1local nextTime = self.sequence[self.nextEvent][1]if nextTime ~=9999thenlocal 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 ENDEDendendfunction Sequencer:pause()
self.timer:stop()endfunction Sequencer:resume()
self.timer:start()endfunction 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..
localfunction seq_001(event)-- START A TWEEN OR DO SOME OTHER THING HEREendlocalfunction seq_002(event)endlocalfunction seq_003(event)endlocal 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
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.
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.
Comments
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
Likes: antix
Likes: antix
Fragmenter - animated loop machine and IKONOMIKON - the memory game
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:
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
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975