从舞台中删除功能
Remove a function from the stage
我的代码有一个大问题
我有一个函数叫做 "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"也在同一时间完成。
希望有人能帮助我,因为这个问题让我很沮丧。
目前,您每次添加功能时都会创建一个全新的计时器。由于事件侦听器,该计时器将保留在内存中,并且由于它被封装在函数中,您没有简单的方法再次引用它来停止它们。
更好的方法是只创建一个全局引用的计时器,这样您就可以在需要时停止它。
您可以通过以下方式完成此操作:
//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();
我的代码有一个大问题
我有一个函数叫做 "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"也在同一时间完成。
希望有人能帮助我,因为这个问题让我很沮丧。
目前,您每次添加功能时都会创建一个全新的计时器。由于事件侦听器,该计时器将保留在内存中,并且由于它被封装在函数中,您没有简单的方法再次引用它来停止它们。
更好的方法是只创建一个全局引用的计时器,这样您就可以在需要时停止它。
您可以通过以下方式完成此操作:
//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();