(我正在AS3和Adobe AIR for iOS SDK中工作)。新實例不重置其定時器和孩子
該程序有兩個類:第一個是Program.as
這是什麼FLA文件鏈接到。在Program.as
有一個功能啓動程序和另一個重新啓動程序。第二類是我的Main.as
類,它調用Program.as
的finishNow();
函數重新啓動程序。
它在第一次運行時運行良好。問題是,幾乎只要它重新啓動,它似乎保持自己重新啓動。它也提供了很多ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
錯誤。我也注意到,當程序重新啓動時,諸如TIMERS之類的一些函數不會再從0開始?我真的很難過,因爲邏輯似乎沒問題,但痕跡說不然。
將跡線放入Program.as
表示finishNow();
函數在第一次運行後被重複調用。問題在於programRestartTimer
未在新實例上重置。通過調用programRestartTimer
上的stop();
函數暫時修復此問題。從持續顯示的Error #2025
中,我懷疑在第一次運行中顯示未被刪除的Childs(或類似的 - 如其他定時器也未重置)導致此問題。這表明,或者程序不是創建一個全新的實例,或者它不可能用AS3?
Program.as:
package {
import flash.display.MovieClip;
public class Program extends MovieClip {
var runMain:Main;
public function Program() {
startNow();
}
function startNow() {
runMain = new Main(this);
addChild(runMain);
}
function finishNow() {
removeChild(runMain);
runMain = new Main(this);
addChild(runMain);
}
}
}
Main.as:
package {
import flash.display.Sprite;
public class Main extends Sprite
{
public var program:Program;
var programRestartTimer:Timer = new Timer(8 * 1000);
public function Main(stageHolderTemp) {
program = stageHolderTemp;
trace(program);
someFunctionsThatDrawGraphics();
moreFunctions();
}
function callFinishFunction():void { // this is called at the end of the animation
programRestartTimer.start();
programRestartTimer.addEventListener(TimerEvent.TIMER, restartProgram);
}
function restartProgram(e:TimerEvent):void {
programRestartTimer.stop(); // this line is a temporary "fix" to stop the program from constantly restarting
// it doesn't actually fix the full problem
program.finishNow();
}
}
}
你應該先在'restartProgram()'中刪除你的定時器事件監聽器,並像下面這樣實例化你的定時器:'var programRestartTimer:Timer = new Timer(8 * 1000,1);'這樣每個定時器只會發送一個計時器事件。這有幫助嗎? – duTr 2013-03-18 02:51:26