Reputation: 21
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.
Upvotes: 2
Views: 25113
Reputation: 163
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
}
Upvotes: 0
Reputation: 1110
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.)
Upvotes: 8