I have a question about TIMER in AS3
i have a zombie object on my stage, i want him to come and attack the HERO.
what i want to do is:
thank you :)
here's the code
if (zombie.x>hero.x+50)
{
zombie.x-=5;
zombie.scaleX=-1;
if(zombie.x<hero.x+100){
zombie.gotoAndStop("attack");
//so that the zombie attacks when the hero is in range
}
}
you can do something like this :
var timer:Timer = new Timer(5000);//that's 5 second
if (zombie.x>hero.x+50)
{
zombie.x-=5;
zombie.scaleX=-1;
if(zombie.x<hero.x+100){
attack();
}
}
function attack( ) : void
{
// attack the first time
zombie.gotoAndStop("attack");
//than launch the timer
timer.addEventListener(TimerEvent.TIMER, repeatAttack );
timer.start();
}
//will be called every 5000 ms == 5 sec
function repeatAttack ( event : TimerEvent ) : void
{
zombie.gotoAndStop("attack");
}
//if you want to stop the attack you can use this function for example
function stopAttack() : void
{
timer.removeEventListener(TimerEvent.TIMER, repeatAttack );
timer.stop();//stop timer
timer.reset();//resetCount to zero
}
I hope this will help you solve your issue