actionscript 3.0 how to set time delay
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.)
Admin
Updated on October 20, 2020Comments
-
Admin over 3 years
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.