AS3 gotoAndStop导致对象自行删除

时间:2013-02-21 18:02:56

标签: actionscript-3 removechild

我有一个学生正在AS3的塔防游戏中工作,并且有一个困扰我的问题。他正在使用hitTestObject来改变movieClip正在移动的方向。 movieClip有自己的时间轴,其中包含对象所面向的不同方向的框架以及带有对象行为代码的链接.as文件。

当他调用gotoAndStop来更改movieClip的内部框架时,会触发删除的事件,但该对象会停留在屏幕上并且不再移动。

我的所有搜索都找到了有关删除对象的答案,但我还没有看到任何阻止对象自行删除的内容。

以下代码是由movieClip对象的.as类文件中的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行时,movieClip的帧会发生变化,然后代码会直接跳转到REMOVED事件触发的函数。

有没有人知道为什么REMOVED事件可能会被此代码触发?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

REMOVED事件是由MovieClip中的舞台或包含它的Sprite中的任何内容触发的,如果我没有弄错的话。特别是对于具有动画的MovieClip,每次都会删除并添加内容,例如,如果动画的某些部分在时间轴上或关键帧处结束。

仅在从舞台中删除容器本身时才调度Event.REMOVED_FROM_STAGE。也许那会引起你的困惑?我无法从您的代码示例中看到您正在侦听的事件类型。

答案 1 :(得分:0)

你在哪里添加remove-listener?

如果没有更多信息,我猜你正在听动画中的一个片段,而且它并不存在于所有帧上(或者甚至更可能是 - 实例被换成另一个,相同的,这可能会发生,这取决于你添加关键帧的顺序,月亮的对齐和电离层的波动。最简单的方法是删除所有关键帧,然后重新创建它们。然后永远不要使用flash pro再一次。)

相关问题