Reputation: 47
var moveTimer:Timer = new Timer(1);
moveTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void
{
//code
}
moveTimer.start();
moveTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone)
function timerDone(e:TimerEvent):void
{
upKey = false;
}
Hey Guys, so this is my code. I have some very simplistic AI in my game and I'm trying to utilize a timer in order for the enemy to move forward for about 2-3 seconds and then stop. To do this I'm using the variable upKey as a boolean which is set to true, but when the timer finishes it gets set to false and upon it being set to false, there is an if statement that will reduce the enemy's speed to 0.
This is my first time using a timer and the enemies dont really stop... they kind of just keep going until they wander off the screen. Am I doing this correctly or is it a problem elsewhere in my code? Also, is there a better more effiecient way to use a timer?
Thanks, James.
Upvotes: 2
Views: 665
Reputation: 15955
From the code you cite, the timer constructor does not specify repeatCount
indicating it should repeat indefinitely. For the timerDone()
handler to be called, you must specify a repeat count.
Also, note that a delay below 20-milliseconds is not recommended.
Timer constructor parameters: Timer(delay:Number, repeatCount:int = 0)
delay:Number
— The delay between timer events, in milliseconds. A delay lower than 20 milliseconds is not recommended. Timer frequency is limited to 60 frames per second, meaning a delay lower than 16.6 milliseconds causes runtime problems.
repeatCount:int
(default = 0) — Specifies the number of repetitions. If zero, the timer repeats indefinitely, up to a maximum of 24.86 days (int.MAX_VALUE + 1
). If nonzero, the timer runs the specified number of times and then stops.
Timers are not recommended for animated content. Instead, use Event.ENTER_FRAME
to manipulate frame-based animation.
One approach would be to use timers to trigger state changes to your game model:
/** timer */
var timer:Timer;
/** whether enemies are advancing */
var advance:Boolean = false;
// start timer at 5-seconds intervals
timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
// animation controlled by Event.ENTER_FRAME
addEventListener(Event.ENTER_FRAME, frameHandler);
In your timer handler, you can adjust the timer delay depending on the state of your game.
/** timer handler */
function timerHandler(event:TimerEvent):void
{
// stop the current timer
timer.stop();
// depending on the current enemy state
switch (advance)
{
// if true, stop advancing and wait 5-seconds
case true:
trace("Stop advancing, wait 5-seconds");
timer.delay = 5000;
break;
// if false, advance for 2-seconds
case false:
trace("Advance for next 2-seconds");
timer.delay = 2000;
break;
}
// invert advance state.
advance = !advance;
// restart timer
timer.start();
}
Likewise on enter frame, control animation of your enemy based on game state:
/** frame handler, advancing enemy if 'advance' is true */
function frameHandler(event:Event):void
{
if (advance) { /** move enemy forward */ }
}
This alternates state of your enemies, outputting:
Advance for next 2-seconds
Stop advancing, wait 5-seconds
Advance for next 2-seconds
Stop advancing, wait 5-seconds
Advance for next 2-seconds
Upvotes: 1