Question

I have function where a series of buttons are created, tween for a second to their position in quick succession and then the timer that triggers the tween ends.

I experimented with the delay/time settings on the tween and couldn't get the desired effect without a timer class since I want each new button to begin it's tween before the previous one finishes.

I almost have it looking how I want now using a timer, but the delay at the start is still a problem. Is there a quick way to skip the delay for the first fire of the timer, or will I need to go the long route and create additional timer/tween/functions to simulate this effect?

Was it helpful?

Solution

One solution would be to just call the method without the whole timer thing. Take a look at this example:

// Here, we create the timer
var t:Timer = new Timer(200);
t.addEventListener(TimerEvent.TIMER, onTimer);

Let's say that some event happened, like a button press. Upon pressing the button a space-ship in a game starts to shoot, but you want the first bullet to fly out right away, and not wait for the 200ms delay:

t.start(); // make sure t is a class member, not a local variable
shoot();

This way the timer will start, but the first action will happen right away ;)
Just for clarity, the timer handler could look something like this:

private function onTimer(e:TimerEvent):void 
{
    shoot();
}

As you see, there is no need to put the implementation of shooting right into the handler function to preserve it's re-usability and better maintainability.

Simply as a cool thing to know, you can actually change the delay of your timer on run-time.

Here's a small example:

// Creating the timer with 2 handlers
var t:Timer = new Timer(200);
t.addEventListener(TimerEvent.TIMER, onTimerSetNewDelay);
t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();

The implementation of onTimer() doesn't matter, but onTimerSetNewDelay can be interesting:

private function onTimerSetNewDelay(e:TimerEvent):void 
{
    t.delay = YOUR_NEW_DELAY_VALUE;
    t.removeEventListener(TimerEvent.TIMER, onTimerSetNewDelay); // removing the listener so it doesn't set the same delay over and over again.
}

OTHER TIPS

what about calling the function using onTimer(null);?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top