从舞台中删除一个功能

时间:2017-06-12 18:21:25

标签: actionscript-3 flash

我的代码存在很大问题

我有一个名为“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”也在同时完成。

希望有人可以帮助我,因为这个问题让我感到沮丧。

1 个答案:

答案 0 :(得分: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();