有没有办法延迟我的功能3秒。除了等待3秒钟然后执行一个函数之外,我希望它等待3秒钟,然后执行3秒钟前完成的函数。actionscript 3.0如何设置时间延迟
我可能没有什么意义在于最后一句,但这里有一个例子:
例。走,然后一个跟随者你做了完全相同的事情,除了延迟3秒。
在此先感谢。
有没有办法延迟我的功能3秒。除了等待3秒钟然后执行一个函数之外,我希望它等待3秒钟,然后执行3秒钟前完成的函数。actionscript 3.0如何设置时间延迟
我可能没有什么意义在于最后一句,但这里有一个例子:
例。走,然后一个跟随者你做了完全相同的事情,除了延迟3秒。
在此先感谢。
AS3中的函数是第一类成员,这意味着它们可以作为参数传递。你可以设置一个延迟的一种方法是通过定义,像这样一个“延迟”功能:
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");});
,你需要延迟需要被“包裹”像这样的匿名功能,因为.addEventListener
方法预计要传递的功能一个只有一个参数的函数:Event
对象。
(你还可以指定你想传递给延迟功能匿名函数中的参数,虽然)。
3秒后触发后会发生一个功能?
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
}
需要注意的是匿名的听众只能在侦听器函数内被删除,一旦它被调用。 –
这是一个很好的观点 - 应该摆脱事件监听器:'delayedFunctionCall(2000,function(e:Event){e.currentTarget.removeEventListener(e.type,arguments.callee); walkRight();}); ' – xdl
,随着时间的推移,你可以调用除函数之外的其他东西。 delayedFunctionCall(2000,function(e:Event){e.currentTarget.removeEventListener(e.type,arguments.callee); trace('walking right');}); –