我有一名学生正在AS3中进行塔防游戏,并且有一个难题让我难住。他正在使用hitTestObject来改变movieClip移动的方向。 movieClip拥有自己的时间线,其中包含面向对象所面向的不同方向的帧,以及包含对象行为代码的链接.as文件。AS3 gotoAndStop导致对象自行删除
当他调用gotoAndStop来更改movieClip的内部框架时,将移除的事件被触发,但对象停留在屏幕上并不再移动。
我所有的搜索都能找到关于删除对象的答案,但我还没有看到任何有关防止删除对象的问题。
下面的代码是通过在。作为类文件MovieClip对象的ENTER_FRAME事件触发的循环:
private function eFrame(event:Event):void
{
if (_root.isPaused == false)
{
//MOVING THE ENEMY
this.x += speed * xDir;
this.y -= speed * yDir;
if (health <= 0)
{
_root.currency += 4;
this.parent.removeChild(this);
}
if (this.x > 770)
{
this.parent.removeChild(this);
_root.health -= 10;
_root.gotHit = true;
}
//checking if touching any invisible markers
for (var i:int=0; i<_root.upHolder.numChildren; i++)
{
//the process is very similar to the main guy's testing with other elements
var upMarker:DisplayObject = _root.upHolder.getChildAt(i);
if (hitTestObject(upMarker))
{
yDir = 1;
xDir = 0;
this.gotoAndStop(3);
}
}
for (i=0; i<_root.downHolder.numChildren; i++)
{
//the process is very similar to the main guy's testing with other elements
var downMarker:DisplayObject = _root.downHolder.getChildAt(i);
if (hitTestObject(downMarker))
{
yDir = -1;
xDir = 0;
this.gotoAndStop(7);
}
}
for (i=0; i<_root.rightHolder.numChildren; i++)
{
//the process is very similar to the main guy's testing with other elements
var rightMarker:DisplayObject = _root.rightHolder.getChildAt(i);
if (hitTestObject(rightMarker))
{
yDir = 0;
xDir = 1;
this.gotoAndStop(6);
}
}
for (i=0; i<_root.leftHolder.numChildren; i++)
{
//the process is very similar to the main guy's testing with other elements
var leftMarker:DisplayObject = _root.leftHolder.getChildAt(i);
if (hitTestObject(leftMarker))
{
yDir = 0;
xDir = -1;
this.gotoAndStop(2);
}
}
}
}
private function remove(event:Event):void
{
trace("remove");
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.enemiesLeft -= 1;
}
}
当gotoAndStop行执行,的动画片段改变该帧,然后将代码跳到直接传递给由REMOVED事件触发的函数。
有没有人有一个想法,为什么REMOVED事件可能由此代码触发?
谢谢你的帮助。
谢谢。将我的事件监听器从REMOVED更改为REMOVED_FROM_STAGE解决了这个问题。 – wrTechTeacher 2013-02-22 16:54:29