0

i want to reload a gun. When R pressed. I want it to wait 3 seconds before loading the gun. Is there a simple way to do this?

example:

if (event.keyCode == Keyboard.R)
        {
            //wait(4 seconds)

                           // start reloading
        }
Crook
  • 317
  • 1
  • 3
  • 12

2 Answers2

1
var reloadTimer = new Timer(4000,1); //add a 4 second timer
reloadTimer.addEventListener(TimerEvent.TIMER_COMPLETE,reload);

function handleKeyPress(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.R)
    {
        reloadTimer.reset() //in case it was still going (otherwise add check for if already reloading)
        reloadTimer.start();
    }
}

When the key is pressed, it will start the 4 second timer. At the end of the 4 seconds, the reload function will execute. You should put your code for reloading in there.

Trex
  • 108
  • 9
0

Simplest way is to use setTimeout:

if (event.keyCode == Keyboard.R)
{
    setTimeout( reload, 3000 );
}

...

function reload():void
{
    // do reload
}

It internally uses the Timer/TimerEvent classes. Neither methods are all that accurate and you should probably be using a main game timer that you calculate on each frame.

Michael
  • 3,776
  • 1
  • 16
  • 27