2017-06-12 64 views
1

我有一个很大的问题,我的代码从舞台

我已经调用的函数“delayCallFuntions”删除功能:

function delayCallFuntions(delay: int, func: Function) { 
    var timer: Timer = new Timer(delay, 1); 
    timer.addEventListener(TimerEvent.TIMER, func); 
    timer.start(); 
} 

而且我用这个功能类似下面做2点之间的连接在我的屏幕上:

delayCallFuntions(1, function (e: Event) {timer011(wireColor);}); 

和功能 “timer011” 正在连接:

function timer011(firstColor: int): void { 
wireColor = firstColor; 
//GRID is a class 
//Path A to B 
var PathAB: Grid; 
PathAB = new Grid(4, 5, 20, 17, canvas, wireColor); 
this.addChild(PathAB); 

}

我的问题是: 我有几个的这些功能,如“timer012”,“timer013”,......,他们需要执行一个接一个。 当我离开这个场景再回来时,这些功能仍然有些正在工作,而我需要他们从头开始并逐个去做。

例如:当我回来时,“timer011”正在启动,而“timer016”也在同时完成。

希望有人能帮助我,因为这个问题让我沮丧。

+0

你能解释一下你的程序试图完成什么。听起来你只是想排队延迟链中的一些功能?所以一个人执行,然后1秒钟后,下一个等等。你想要能够停止,如果用户移出场景/框架? – BadFeelingAboutThis

+0

@BadFeeling关于此函数“timer011”,“timer012”等是为了让我的网格中不同单元格之间的不同连接。他们需要根据计时器执行。例如:timer011在100ms后执行,timer012在3000ms后执行直到结束。我希望当用户想要去其他场景时停止它们。 – mazName

+0

您需要保存对初始** Timer **对象的引用,并在不再需要它时将其停止并销毁它。另外,我通常会创建一个字段** destroy:Boolean **并使用** if(destroy)return; **启动方法,所以当我不再需要某些内容并且无法确保其立即处置(如延迟调用,听众等),然后我设置** destroy = true; **来防止这些方法在错误的时间执行。 – Organis

回答

1

目前,您每次添加功能时都会创建一个全新的计时器。由于事件监听器的原因,该计时器将留在内存中,并且由于它封装在函数中,因此再次引用它来阻止它们并不容易。

什么是更好的方法,是创建一个全局引用的定时器,以便在需要时停止它。

这是一种方式,你可以做到这一点:

//create an array that will hold all the functions you are planning on calling 
var delayedFuncs:Array = []; 

//this var will be used to store the current function that will be called next 
var currentFuncObj:Object = null; //set it to null so it clears the value when you return to this frame 

//create a single, global timer reference for everything 
//don't initialize it here though 
//if you revisit this frame, you don't want to create a whole new timer, but keep using the previous one 
var funcTimer:Timer; 

//if the timer has already been created (you've been to this frame before), stop it 
if (funcTimer) { 
    funcTimer.stop(); 
}else { 
//if you haven't been to this frame before, create the timer and add the listener 
    funcTimer = new Timer(1,1); 
    funcTimer.addEventListener(TimerEvent.TIMER, nextFunc, false, 0, true); 
} 

//this function adds items to your queue. I've added the ability to also include parameters 
function delayCallFunctions(delay:int, func:Function, ... funcParams):void { 
    //add an object to the array that stores the function, delay, and any parameters to pass to that function 
    delayedFuncs.push({delay: delay, func: func, params: funcParams}); 

    //if the timer hasn't started running yet, start it since we've added something 
    if(!funcTimer.running) nextFunc(); 
} 

//this function runs when the timer completes 
function nextFunc(e:Event = null):void { 

    //if we have an existing function to call, call it 
    if (currentFuncObj){ 
     //invoke the function with the parameters 
     currentFuncObj.func.apply(null, currentFuncObj.params); 
    } 

    //if there are still items in the array, grab the next one 
    if(delayedFuncs.length > 0){ 
     //array.shift grabs the first element in the array and removes it from the array 
     currentFuncObj = delayedFuncs.shift(); 

     //reset the timer 
     funcTimer.reset(); 
     //set the appropriate delay 
     funcTimer.delay = currentFuncObj.delay; 
     //start the timer again 
     funcTimer.start(); 
    } 
} 

所以,现在,你会做使用:

delayCallFunctions(3000, trace, "hello", "world", "I'll be traced 3 seconds from now"); 
delayCallFunctions(2000, trace, "I'll be called 2 seconds after the last one"); 

,或者用特定代码:

delayCallFuntions(1000, timer011, wireColor); 

现在在任何时候(比如说你点击一个按钮去改变场景),你可以停止全局定时器。

funcTimer.stop(); 
+0

它工作正常,但当我回到现场时,我的一切都将重新开始!现在,通过这个代码,最后的延迟将会保留,当我回到前一个场景时,第二个延迟将与第一个延迟同时开始,因为它已经被推到阵列,延迟将不会再次!有什么方法可以修复它吗? – mazName

+0

对不起,是的,将'currentFuncObj'初始化为null应该可以解决这个问题。我已经更新了答案。 – BadFeelingAboutThis

+0

它正常工作,非常感谢你:-)我可以问另一个与Sprite类型的变量有关的问题吗? – mazName