Actionscript 3 AS3-删除VentListener(添加到\u阶段)
我在尝试删除Actionscript 3 AS3-删除VentListener(添加到\u阶段),actionscript-3,events,actionscript,event-handling,Actionscript 3,Events,Actionscript,Event Handling,我在尝试删除事件时遇到了一些问题。基本上,在这个游戏中,我开发了一个生命小于或等于零的游戏,游戏将结束并切换到屏幕上的游戏。但是,当我这样做时,它会返回此错误 ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at States/changeState() at
事件时遇到了一些问题。基本上,在这个游戏中,我开发了一个生命小于或等于零的游戏,游戏将结束并切换到屏幕上的游戏。但是,当我这样做时,它会返回此错误
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at States/changeState()
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
这就变成了
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Enemy/StayOnScreen()
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
我不完全清楚为什么——有人能澄清为什么会这样,以及我如何修复它吗
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;
public class AvoiderGame extends MovieClip
{
var theCallBackFunction:Function;
public static var enemyArray:Array;
public var enemy:Enemy
public var Background:gameBackground;
public var Lives:Number = 3;
public var avatar:Avatar;
public var gameTimer:Timer;
private var _collisionTest:CollisionTest;
private var numStars:int = 80;
private var fireTimer:Timer; //causes delay between fires
private var canFire:Boolean = true; //can you fire a laser
public function AvoiderGame(callBack)
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
theCallBackFunction = callBack;
}
private function init(e:Event):void
{
Background = new gameBackground();
addChild(Background);
enemyArray = new Array();
avatar = new Avatar(stage);
addChild(avatar);
avatar.x = stage.stageWidth / 2;
avatar.y = stage.stageHeight / 2;
_collisionTest = new CollisionTest();
gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1);
}
fireTimer = new Timer(1000, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
fireTimer.start();
}
public function onTick(timerEvent:TimerEvent):void
{
if (Math.random() < 0.1)
{
trace('array length: ', AvoiderGame.enemyArray.length);
enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
enemyArray.push(enemy);
addChild(enemy);
enemy.gotoAndStop("Enemy" + Math.round(1 + (5 - 1) * Math.random()))
}
canFire = avatar.UpdateAvatar(canFire);
if (canFire == false)
{
fireTimer.start();
}
avatar.StayOnScreen();
for each (var enemy:Enemy in enemyArray)
{
enemy.moveDown();
enemy.StayOnScreen();
if (_collisionTest.complex(enemy, avatar))
{
gameTimer.stop();
for each (var enemy:Enemy in enemyArray)
{
for(var i:int = 0; i < enemyArray.length; i++)
{
removeChild(enemyArray[i]);
enemyArray.splice(i, 1); //remove the i'th element as i'th element is the enemy containing the ID of hit enemy
}
}
Lives--;
trace('lives: ', Lives);
gameTimer.start();
}
}
if (Lives == 0)
{
removeEventListener(Event.ADDED_TO_STAGE, init)
theCallBackFunction(this, "over");
}
}
private function fireTimerHandler(e:TimerEvent) : void
{
//timer ran, we can fire again.
canFire = true;
}
}
}
包
{
导入flash.display.MovieClip;
导入flash.utils.Timer;
导入flash.events.Event;
导入flash.events.TimerEvent;
导入com.freeactionscript.CollisionTest;
导入flash.display.Stage;
公共类AvoiderGame扩展了MovieClip
{
var theCallBackFunction:函数;
publicstaticvar-enemyArray:Array;
公敌
公众背景:游戏背景;
公共var寿命:数量=3;
公共虚拟现实:虚拟现实;
公共变量gameTimer:计时器;
私人var_冲突测试:冲突测试;
私有变量numStars:int=80;
private var fireTimer:Timer;//导致两次激发之间的延迟
private var canFire:Boolean=true;//你能发射激光吗
公共函数AvoiderGame(回调)
{
this.addEventListener(Event.ADDED_TO_STAGE,init);
callbackfunction=回调;
}
私有函数init(e:事件):void
{
背景=新游戏背景();
addChild(背景);
enemyArray=新数组();
化身=新化身(舞台);
addChild(阿凡达);
avatar.x=stage.stageWidth/2;
avatar.y=stage.stageHeight/2;
_collisionTest=新的collisionTest();
游戏计时器=新计时器(25);
gameTimer.addEventListener(TimerEvent.TIMER,onTick);
gameTimer.start();
对于(变量i:int=0;i
//使用弱引用,以便在必要时处理侦听器
this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
private function init(e:Event):void {
//remove this now as we have added the object to the stage
this.removeEventListener(Event.ADDED_TO_STAGE, init);
//add a new listener that will fire when the object is removed from stage
this.addEventListener(Event.REMOVED_FROM_STAGE, reset, false, 0, true);
//注意。您正在将星星添加到舞台上,无法引用它们来删除它们
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1);
}
var starArray = [];
for (var i:int = 0; i < numStars; i++)
{
var newStar:Star = new Star(stage);
stage.addChildAt(newStar, 1);
starArray.push(newStar);
}
在onTick中的哪一个位置-您得到了错误?同样,在ADDED_TO_STAGE的处理程序中,您应该注销它。错误:Error#2094:事件分派递归溢出。flash.display::DisplayObjectContainer/removeChild()在States/changeState()在AvoiderGame/reset()在flash.display::DisplayObjectContainer/removeChild()在States/removeChild()在flash.utils::Timer/_timerDispatch()在flash.utils::Timer/tick()在flash这是自从我在reset函数中添加之后一直得到的结果。作为一般调试过程的一部分,如果一个bug不是很明显,请尝试注释所有内容,然后取消注释并测试代码块的每一行,以查看错误何时出现。关于您的错误,请尝试在执行任何其他操作之前停止两个计时器。
var starArray = [];
for (var i:int = 0; i < numStars; i++)
{
var newStar:Star = new Star(stage);
stage.addChildAt(newStar, 1);
starArray.push(newStar);
}
for (var i:int = starArray.length; i > 0; i--)
{
stage.removeChild(starArray[starArray.length-1]);
}
starArray = [];
starArray = null;