简体   繁体   中英

actionscript 3.0 how to set time delay

Is there a way to delay 3 seconds off of my function. Except instead of waiting 3 seconds and then executing a function, i want it to wait 3 seconds and then execute a function it would've done 3 seconds ago.

I probably didn't make sense in that last sentence but here's an example :

Ex. walking and then a follower you have does that exact same thing you did except delayed 3 seconds.

thanks in advance.

Functions in AS3 are first class members, which means they can be passed around as arguments. One way you can set a delay is by defining a 'delaying' function like so:

function delayedFunctionCall(delay:int, func:Function) {
    trace('going to execute the function you passed me in', delay, 'milliseconds');
    var timer:Timer = new Timer(delay, 1);
    timer.addEventListener(TimerEvent.TIMER, func);
    timer.start();
}


function walkRight() {
    trace('walking right');
}

function saySomething(to_say:String) {
    trace('person says: ', to_say);
}

//Call this function like so:

delayedFunctionCall(2000, function(e:Event) {walkRight();});

delayedFunctionCall(3000, function(e:Event) {saySomething("hi there");});

The function that you need delayed needs to be 'wrapped' with an anonymous function like this because .addEventListener methods expects to be passed a function with just one parameter: the Event object.

(You can still specify the arguments you want to pass to the delayed function within the anonymous function, though.)

A function will happen after 3 seconds after triggered?

var timer:Timer
function example():void{
var.addEventlistener(Whatever.Event, any_function);}

function any_function(event:Whatever){
    timer = new Timer(3000, 1);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE, func);
    timer.start();
}

function func(event:TimerEvent){
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, func);
todo here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM