-2

我有两个名为 Guest 和 Guest2 的课程。我想知道是否可以从 Guest2 类中删除 Guest 类中的事件监听器。下面是完整的代码。注意:两个类的代码完全相同

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

        public class Guest extends MovieClip
        {
            var walkSpeed:Number = 5;
            var oldPosX;
            var oldPosY;

            var myGlow:GlowFilter = new GlowFilter();

            public function Guest()
            {   
                addEventListener(MouseEvent.MOUSE_OVER, addGlow);
            }

            function addGlow(event:MouseEvent):void
            {
                filters = [myGlow];
                addEventListener(MouseEvent.MOUSE_OUT, removeGlow);
                addEventListener(MouseEvent.CLICK, ready);

            }

            function removeGlow(event:MouseEvent):void
            {
                filters = [];
            }

            function ready(event:MouseEvent):void
            {
                filters = [myGlow];
                stage.addEventListener(MouseEvent.MOUSE_DOWN, walk);
                removeEventListener(MouseEvent.MOUSE_OUT, removeGlow);
                **MovieClip(root).Guest02.addEventListener(MouseEvent.CLICK, walkTo);**
            }

            function walk(event:MouseEvent):void
            {
                oldPosX = parent.mouseX;
                oldPosY = parent.mouseY;
                rotation = Math.atan2(oldPosY - y,oldPosX - x) / Math.PI * 180;
                filters = [];
                stage.removeEventListener(MouseEvent.MOUSE_DOWN, walk);
                stage.addEventListener(Event.ENTER_FRAME, loop);
            }

            function loop(event:Event):void
            {
                var dx:Number = oldPosX - x;
                var dy:Number = oldPosY - y;
                var distance:Number = Math.sqrt((dx*dx)+(dy*dy));
                if (distance<walkSpeed)
                {
                    // if you are near the target, snap to it
                    x = oldPosX;
                    y = oldPosY;
                    removeEventListener(Event.ENTER_FRAME, loop);

                }
                else
                {
                    x = x+Math.cos(rotation/180*Math.PI)*walkSpeed;
                    y = y+Math.sin(rotation/180*Math.PI)*walkSpeed;
                }

            }

            **function walkTo(event:MouseEvent):void
            {
                _Guest02.removeEventListener(MouseEvent.CLICK, ready);
            }**

        }
    } 
4

1 回答 1

0

正如 Jake King 已经指出的那样,您可能希望从面向对象编程的角度重新审视您的代码。如果你说这两个类共享完全相同的功能,你为什么不设置一个类Guest 并创建两个实例,例如guest1 和guest2。

至于您关于事件处理程序的问题,您需要将侦听器存储在一个公共实例变量中,然后可以从另一个实例访问(不好的样式),或者编写一个允许删除侦听器的专用公共函数,而侦听器变量保持私有:

public class Guest extends MovieClip
{
    private var overListener;

    public function Guest()
    {   
        overListener = addEventListener(MouseEvent.MOUSE_OVER, addGlow);
    }

    public function removeOverListener() 
    {
        if (overListener) {
            removeEventListener(MouseEvent.MOUSE_OVER, overListener);
        }
    }

    ...
}

如果 guest1 和 guest2 都是 Guest 的实例,并且 guest1 存储了对 guest2 的引用,那么您可以从 guest1 中删除 guest2 中的侦听器,如下所示:

guest2.removeOverListener();
于 2012-12-28T14:10:34.003 回答