ActionScript 3按钮CLICK事件未触发

时间:2013-08-27 07:05:45

标签: actionscript-3 flash button mouseevent event-listener

所以现在,在我的Flash游戏中不知疲倦地使用我的重启按钮后,该按钮仍然不会注册点击。

以下是按钮的代码: 为什么我认为问题出在MouseEvent.CLICK上,因为trace()标志没有出现。

package 
{
    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.ui.Mouse;
    /**
      * ...
      * @author Andrew Xia
      */
    public class RestartButton extends SimpleButton
    {
        public function RestartButton(setX:Number, setY:Number)
        {
            this.x = setX;
            this.y = setY;
            addEventListener(MouseEvent.CLICK, onClick);
        }

        public function onClick(event:MouseEvent):void
        {
                        trace("HELLO!");
            dispatchEvent( new NavigationEvent( NavigationEvent.RESTART ));
        }
    }
 }

以下是我将按钮添加到屏幕的代码:

  package 
  {
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.ui.Mouse;
    /**
     * ...
     * @author Andrew Xia
     */
    public class GameOverScreen extends MovieClip 
    {
        public var restartButton:RestartButton;
        public var scoreLabel:TextField;
        public var scoreBox:TextField;

        public function GameOverScreen()
        {
            Mouse.show();
            restartButton = new RestartButton(0, 108);
            addChild(restartButton);

            var textFormat = new TextFormat();
            textFormat.size = 54;

            scoreLabel = new TextField();
            scoreLabel.x = -185;
            scoreLabel.y = -36.75;
            scoreLabel.height = 73.5;
            scoreLabel.width = 185;
            scoreLabel.text = "SCORE: ";
            scoreLabel.textColor = 0xFFFFFF;
            scoreLabel.setTextFormat(textFormat);
            addChild(scoreLabel);

            scoreBox = new TextField();
            scoreBox.x = 0;
            scoreBox.y = -36.75;
            scoreBox.height = 73.5;
            scoreBox.width = 143;
            scoreBox.text = (String)(Main.playerScore);
            scoreBox.textColor = 0xFFFFFF;
            scoreBox.setTextFormat(textFormat);
            addChild(scoreBox);
        }
    }
}

请帮助,这个问题一直让我发疯,我确信这可能只是我的一个非常愚蠢的错误,但如果你们能帮助我,我会非常感激。

1 个答案:

答案 0 :(得分:0)

我没有看到你为任何按钮状态添加任何DisplayObject。如果没有可点击的内容 - 点击将永远不会发生。

尝试将其作为RestartButton类:

package 
{
    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.ui.Mouse;

    public class RestartButton extends SimpleButton
    {
        public function RestartButton(setX:Number, setY:Number)
        {
           super(GRAPHICS_FOR_UP, OVER, DOWN, HIT); // the object can repeat

           this.x = setX;
           this.y = setY;
           addEventListener(MouseEvent.CLICK, onClick);
        }

        public function onClick(event:MouseEvent):void
        {
                    trace("HELLO!");
            dispatchEvent( new NavigationEvent( NavigationEvent.RESTART ));
        }
    }
}

如果由于某种原因你不想添加图形,只需用

添加命中区域
super(null, null, null, HIT_GRAPHICS);

现在您可以点击按钮。