Archive for January, 2014

AS3 Starling Timer vs. DelayedCall

As a long-time AS3 coder, I’m very used to using AS3’s Timer class. For my Starling game tutorial, I even extended it and created the GameTimer class where I added the ability to pause the timer and some other things. As I’ve been working more on my game based on my tutorial engine, I’ve realized that I’ve made a huge mistake in the way I coded the Timers. Namely, in Starling… well… The Scenario Let’s say you’re implementing an enemy spawn timer into your game. You’ve got a number of enemies in the wave (say, 5) and a set amount of time (say, 1500ms) that you want to wait in between spawning each enemy. Without Starling, you would be using some variation of AS3’s Timer class, and your implementation would look something like the following: 1 2 3 4 5 6 7 8 9 10 11 // create a new Timer instance that has a 1500ms delay and repeats 5 times var timer:Timer = new Timer(1500, 5);   // a new enemy spawns every time onTimerTick gets called timer.addEventListener(TimerEvent.TIMER, onTimerTick);   // to keep track of when the whole enemy wave is done spawning timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);   // when ready to start the next wave… timer.start(); While this would work perfectly fine in your Starling game, Starling has a secret Timer class built-in called DelayedCall. I call it “secret” because it exists in the starling.animation package (our spawn timer isn’t animated why would we use something from animation?) and doesn’t immediately jump out at you when looking over the API docs that this secret class can be used as a timer, but it’s exactly what the class is written for. So first, why would you not want to just use Timer? Let’s use a band analogy. You’re the guitarist in […]