【问题标题】:Remove a function from the stage从舞台上移除一个函数
【发布时间】:2017-06-12 18:21:25
【问题描述】:

我的代码有一个大问题

我有一个名为“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 秒后下一个执行等等。如果用户移出场景/帧,您希望能够停止该链吗?
  • @BadFeelingAboutThis 函数“timer011”、“timer012”等是为了在我的网格中的不同单元格之间建立不同的连接。它们需要基于计时器执行。例如:timer011 在 100ms 后执行,timer012 在 3000ms 后执行,直到结束。当用户想去另一个场景时,我希望它们停止。
  • 您需要保存对初始 Timer 对象的引用,并在不再需要时停止并销毁它。另外,我通常创建一个字段 destroyed:Boolean 并使用 if (destroyed) return; 启动方法,所以当我不再需要某些内容并且无法确保其立即处理(如延迟调用、侦听器等),然后我设置 destroyed = true; 以防止这些方法在错误的时间执行。
  • @Organis 你能举个实际的例子吗?我曾尝试为其设置标志,但没有成功!

标签: actionscript-3 flash


【解决方案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!有什么办法可以解决吗?
  • 抱歉,是的,将 currentFuncObj 初始化为 null 应该可以解决这个问题。我已经更新了答案。
  • 它工作正常,非常感谢 :-) 我可以再问一个与 Sprite 类型变量相关的问题吗?
猜你喜欢
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多