Flash Actionscript 3打开/关闭菜单按钮

时间:2010-12-25 08:10:04

标签: flash actionscript-3 oop class

这可能很简单,但我无法弄明白。看看代码,我将这个按钮类(CategoryButton)的2个实例移动到舞台上。正如您所看到的,当您单击按钮时,它应该转到右侧播放(3),将其自身记录为selectedBtn。单击第二个按钮时,应重新启用第一个按钮的侦听器,删除自身的侦听器并将其自身设置为新选择的按钮。不幸的是,这段代码并没有这样做。当我点击第一个按钮时,它将按钮移动到第3帧,并删除其听众。我单击第二个按钮,它会执行相同的操作,但它不会再次为第一个按钮添加侦听器。

我是AS3的新手,非常感谢任何帮助,谢谢。

package classes {
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import flash.display.Sprite;

    public class CategoryButton extends MovieClip{

        //this stores the currently selected button
        public var selectedBtn:Object;

        public function CategoryButton() {
            // listen for over
            addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);

        }

        protected function rolloverToggle(event:MouseEvent):void {
            gotoAndStop(2);
            removeEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
            addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
            addEventListener(MouseEvent.CLICK, buttonToggle);
        }

        protected function rolloutToggle(event:MouseEvent):void {
            gotoAndStop(1);
            removeEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
            addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
        }

        protected function buttonToggle(event:MouseEvent):void {
            if(selectedBtn) {
                trace(selectedBtn.name)
                selectedBtn.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
                selectedBtn.gotoAndStop(1);

                event.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
                event.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
                event.currentTarget.removeEventListener(MouseEvent.CLICK, buttonToggle);

                event.currentTarget.gotoAndStop(3);

                selectedBtn = event.currentTarget;
            }else {

                selectedBtn = event.currentTarget;
                trace(selectedBtn.name);
                selectedBtn.removeEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
                selectedBtn.removeEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
                selectedBtn.removeEventListener(MouseEvent.CLICK, buttonToggle);

                selectedBtn.gotoAndStop(3);
            }
        }

    }

}

2 个答案:

答案 0 :(得分:1)

我自己并不熟悉AS3,但是当您尝试对selectedBtn对象if(selectedBtn)进行布尔测试时,可能会出现问题。为什么不使用诸如“is_selected_button”之类的成员变量来跟踪按钮状态,然后在if语句中测试该变量,而不是针对Object进行测试。

答案 1 :(得分:1)

我如何修理它:

  1. 单击按钮A(A设置为selectedBtn,禁用侦听器,状态更改为第3帧)
  2. 单击按钮B(selectedBtn的侦听器重置,状态更改,selectedBtn设置为按钮B)
  3. 这就是问题所在,A的听众设置如下:

    addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
    

    这导致Button B再次活跃起来。解决方法是简单地将侦听器更改为currentTarget以及gotoAndStop,用于over和out状态。

    event.currentTarget.gotoAndStop(1);
    event.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
    
    
    package classes {
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    
    public class CategoryButton extends MovieClip{
    
        //this stores the currently selected button
        public static var selectedBtn:Object;
    
        public function CategoryButton() {
            // listen for over
            addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
            trace("called");
    
        }
    
        protected function rolloverToggle(event:MouseEvent):void {
            event.currentTarget.gotoAndStop(2);
            event.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
            event.currentTarget.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
            event.currentTarget.addEventListener(MouseEvent.CLICK, buttonToggle);
        }
    
        protected function rolloutToggle(event:MouseEvent):void {
            event.currentTarget.gotoAndStop(1);
            event.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
            event.currentTarget.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
        }
    
        protected function buttonToggle(event:MouseEvent):void {
            if(selectedBtn) {
                trace(selectedBtn.name);
                selectedBtn.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
                selectedBtn.gotoAndStop(1);
    
                event.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
                event.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
                event.currentTarget.removeEventListener(MouseEvent.CLICK, buttonToggle);
    
                event.currentTarget.gotoAndStop(3);
    
                selectedBtn = event.currentTarget;
            }else {
    
                selectedBtn = event.currentTarget;
                selectedBtn.removeEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
                selectedBtn.removeEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
                selectedBtn.removeEventListener(MouseEvent.CLICK, buttonToggle);
    
                selectedBtn.gotoAndStop(3);
    
            }
        }
    
    }
    

    }