AS3调试时出现错误1009
问题描述:
我正尝试在我的太空船游戏的屏幕上创建一个游戏,当玩家的盾牌达到0时,它会转到屏幕上的游戏并停止游戏。游戏在屏幕上工作,但我无法停止游戏。当玩家的盾牌达到0时,我试图将Ship设置为null,但是我得到了1009的错误。当公共函数fGameStart(evt:Event):void { “执行,有没有办法阻止这个功能在游戏结束时运行?任何帮助是极大的赞赏!AS3调试时出现错误1009
public class Engine extends MovieClip {
private var preloader: ThePreloader;
public function Engine() {
stage.addEventListener("gameSTART", fGameStart);
stage.addEventListener("gameOVER", fGameOver);
}
private var numStars: int = 80;
public static var enemyList: Array = new Array();
private var ourShip: Ship;
public function fGameStart(evt: Event): void {
ourShip = new Ship(stage);
ourShip.x = stage.stageWidth/2;
ourShip.y = stage.stageHeight/2;
ourShip.addEventListener("hit", shipHit, false, 0, true);
stage.addChild(ourShip);
for (var i: int = 0; i < numStars; i++) {
stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
function loop(e: Event): void {
if (Math.floor(Math.random() * 20) == 5) {
var enemy: Stinger = new Stinger(stage, ourShip);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
else if (Math.floor(Math.random() * 80) == 5) {
var enemy2: Stinger2 = new Stinger2(stage, ourShip);
enemy2.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemy2.addEventListener("killed", enemyKilled, false, 0, true);
enemyList.push(enemy2);
stage.addChild(enemy2);
}
}
}
public function fGameOver(e: Event) {
gotoAndStop(4);
ourShip = null;
}
}
答
是毫无意义的ourShip
变量设置为null
。它不会从stage
中删除DisplayObject
,或将其从内存中删除。事实上,你得到这个错误的原因是你将它设置为null
。
您需要做的是停止触发的loop
功能。
public function fGameOver(e: Event) {
gotoAndStop(4);
//ourShip = null;
stage.removeChild(ourShip);
removeEventListener(Event.ENTER_FRAME, loop);
}
还设置你弱引用听众可能会在这里一个坏主意
//addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
//why not:
addEventListener(Event.ENTER_FRAME, loop);
非常感谢,它的工作原理! –
如果函数“public function fGameStart(evt:Event):”包含其他游戏对象,我该如何删除整个函数?我试过 “removeEventListener(”gameSTART“,GameStart);”但它没有奏效。 –
你不能删除函数,它们在可能时被垃圾回收/闪存感觉就像它 –