Lazar Georgiev
Lazar Georgiev

Reputation: 15

Actionscript, how to add a time delay for an object

I have stocked on a problem with adding a time delay for an object. The main idea is that i want to change the level of the game to the next when the player hit a specific object. But i do not want this to happen immediately, so i want to add a delay of 3 sec.

onClipEvent(enterframe) {

    if (_root.char.hitTest(this)) {
        //add dealy for the next 2 lines.
        unloadMovie(this);
        _root.gotoAndStop("StageL2");
    }
}

Upvotes: 0

Views: 51

Answers (2)

Eric Mahieu
Eric Mahieu

Reputation: 327

I would add a counter, based upon your framerate (say you have 24fps, 3 seconds is 72frames):

var hit = false;
var counter;
onClipEvent(enterframe) {
    if (_root.char.hitTest(this)) {
        hit = true;
    }
    if(!hit) {
        waitcounter = 0;
    } else {
        waitcounter++;
    }
    if(waitcounter >= 72) {
        unloadMovie(this);
        _root.gotoAndStop("StageL2");
    }
}

I've also added an extra routine to make the hitTest trigger a variable, otherwise the hit should be true for 72 frames (and if you have moving objects, there's a really small chance that's true).

Don't forget to reset your variables when you go to the next stage.

Upvotes: 0

Shaeldon
Shaeldon

Reputation: 883

You can do this by using a Timer

Like this:

var myInterval:Number;

function myMethode():Void
       {
           trace("Executed myMethode() after 3 Seconds")
           clearInterval(myInterval);
       }

myInterval = setInterval(this, "myMethode", 3000);

Upvotes: 1

Related Questions