我有一名學生正在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