0

如果我在前几帧的时间轴上有一个按钮,但随后我将其删除...

如果按钮已从舞台上移除,我是否需要担心移除按钮 (MovieClip) 的事件侦听器?

即使我在时间轴上使用对象,我也在编写一个文档类。

4

1 回答 1

1

您可以使用 removedFromStage 事件来清除按钮实例上的任何事件侦听器:

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Button extends MovieClip
    {
        public function Button():void
        {
            addListeners();
        }

        private function addListeners():void
        {
            this.addEventListener(Event.ADDED_TO_STAGE, addedHandler);
            this.addEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
            this.addEventListener(MouseEvent.CLICK, clickHandler);
        }

        private function addedHandler(event:Event):void
        {
            trace("button added");
        }

        private function removedHandler(event:Event):void
        {
            trace("button removed");
            removeListeners();
        }

        private function clickHandler(event:MouseEvent):void
        {
            trace("button clicked");
        }

        private function removeListeners():void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, addedHandler);
            this.removeEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
            this.removeEventListener(MouseEvent.CLICK, clickHandler);

            trace("has added listener: " + this.hasEventListener(Event.ADDED_TO_STAGE)); 
            trace("has removed listener: " + this.hasEventListener(Event.REMOVED_FROM_STAGE));
            trace("has click listener: " + this.hasEventListener(MouseEvent.CLICK));
        }
    }
}
于 2012-05-03T21:05:16.320 回答