1

所以,我以编程方式将影片剪辑对象添加到主时间轴上。在这些影片剪辑中,我使用计时器事件,因此一旦添加到舞台上,它们就会移动。

所以当我的游戏结束时,我想从舞台上移除影片剪辑对象,我可以这样做,但它会导致错误,因为对象的听众仍然在那里。

我无法从主时间轴访问影片剪辑对象时间轴中的代码以删除侦听器,并且我不知道如何在删除侦听器时从影片剪辑时间轴中删除它们!

我希望这是有道理的!

帮助!

4

2 回答 2

1

您的 MovieClip 可以侦听 Event.REMOVED_FROM_STAGE 以编程方式删除您之前添加的所有事件侦听器。这是一些示例代码来展示它是如何完成的。

    // add this code to an ActionScript class that extends Sprite
    // call initClips() in the constructor

    private var _clipA:MovieClip;
    private var _clipB:MovieClip;
    private var _clipACount:int;
    private var _clipBCount:int;

    private function initClips():void
    {
        // create 2 movieclips and add some common event listeners
        _clipA = new MovieClip();
        _clipA.addEventListener(Event.ADDED_TO_STAGE, onClipAddedToStage);
        _clipA.addEventListener(Event.REMOVED_FROM_STAGE, onClipRemovedFromStage);
        _clipA.addEventListener(Event.ENTER_FRAME, onEnterFrameClipA);


        _clipB = new MovieClip();
        _clipB.addEventListener(Event.ADDED_TO_STAGE, onClipAddedToStage);
        _clipB.addEventListener(Event.REMOVED_FROM_STAGE, onClipRemovedFromStage);
        _clipB.addEventListener(Event.ENTER_FRAME, onEnterFrameClipB);

        _clipACount = 0;
        _clipBCount = 0;

        addChild(_clipA);
        addChild(_clipB);
    }



    private function onClipAddedToStage(event:Event):void
    {
        var clip:MovieClip = event.currentTarget as MovieClip;
        if(clip == _clipA)
        {
            // position movieclip to dummy values
            clip.x = 40;
            clip.y = 40;
        }
        else if(clip == _clipB)
        {
            clip.x = 200;
            clip.y = 100;
        }
    }

    private function onClipRemovedFromStage(event:Event):void
    {
        var clip:MovieClip = event.currentTarget as MovieClip;
        // good habit to null check in case "as" casting fails
        if(clip != null)
        {
            // remove event listeners for this movieclip instance
            clip.removeEventListener(Event.ADDED_TO_STAGE, onClipAddedToStage);
            clip.removeEventListener(Event.REMOVED_FROM_STAGE, onClipRemovedFromStage);

            // there is no penalty for trying to remove event listeners that were not added to this object instance
            clip.removeEventListener(Event.ENTER_FRAME, onEnterFrameClipA); 
            clip.removeEventListener(Event.ENTER_FRAME, onEnterFrameClipB);
        }
    }


    private function onEnterFrameClipA(event:Event):void
    {
        _clipACount++;
        trace("onEnterFrame for clipA " + _clipACount);

        if(_clipACount > 10)
        {
            // for demonstration purposes
            // remove self after a few frames pass
            removeChild(_clipA);
        }

    }   

    private function onEnterFrameClipB(event:Event):void
    {
        _clipBCount++;
        trace("onEnterFrame for clipB " + _clipBCount);

        if(_clipBCount > 20)
        {
            removeChild(_clipB);
        }
    }   
于 2014-06-05T08:08:58.473 回答
0

当从舞台上移除 DisplayObject 时,会有事件接收通知。只需为 Event.ADDED、Event.ADDED_TO_STAGE、Event.REMOVED 和 Event.REMOVE_FROM_STAGE 设置侦听器,即可为 MovieClip 实现一个实时循环。添加 MovieClips 时开始工作。移除 MovieClip 时移除侦听器、停止计时器等。

于 2012-12-20T10:47:46.587 回答