Flash AS3 KeyboardEvent未触发

时间:2012-08-23 17:16:29

标签: actionscript-3 flash events keylistener dismiss

我有一个这样的课程:

public class GameOverScreen extends MovieClip {
    public function GameOverScreen(useMouseControl:Boolean) {
        if(useMouseControl){
            Mouse.show();
            restartButton.addEventListener(MouseEvent.CLICK, onClickRestart);
        }
        else{
            this.addEventListener(KeyboardEvent.KEY_DOWN, onPushSpace);
        }
    }

    public function onClickRestart(mouseEvent:MouseEvent):void{
        dispatchEvent(new NavigationEvent(NavigationEvent.RESTART));
    }

    public function onPushSpace(keyboardEvent:KeyboardEvent):void{
        trace(keyboardEvent);
        dispatchEvent(new NavigationEvent(NavigationEvent.RESTART));
    }...

这是游戏的结束屏幕。 (惊喜!)如果我按下空格按钮,或者点击屏幕上的restartButton,我希望它重新启动我的游戏。正如您所看到的,屏幕在构造函数中获得了一个布尔值,我们决定使用键盘或鼠标来控制游戏。它适用于鼠标,但是使用键,我必须点击重启按钮(它在屏幕上),直到它什么都不做,然后点击它,然后我按下一个按钮我得到playScreen,但我的keylistener仍然在工作,如果我按任意键,它会重新启动游戏。

我的主要课程的重点是:如果玩家死了,他会得到一个gameOverScreen,并且playcreen会被解雇,gameOverScreen也会获得一个监听器,它会监听一个名为RESTART的事件,如果调度该事件,新的playScreen被创建,游戏结束了。

public class Avoider extends MovieClip { ....
     public function onAvatarDeath(avatarEvent:AvatarEvent):void {

        var finalScore:Number = playScreen.getFinalScore();
        var finalTime:Number = playScreen.getFinalTime();

        gameOverScreen = new GameOverScreen(useMouseControl);
        gameOverScreen.addEventListener(NavigationEvent.RESTART, onRequestRestart);
        gameOverScreen.setFinalScore(finalScore);
        gameOverScreen.setFinalTime(finalTime);
        addChild(gameOverScreen);

        playScreen = null;
    }

    public function restartGame():void {
        playScreen = new PlayScreen(useMouseControl);
        playScreen.addEventListener(AvatarEvent.DEAD, onAvatarDeath);
        addChild(playScreen);

        gameOverScreen = null;
    }

    public function onRequestRestart(navigationEvent:NavigationEvent):void {
        restartGame();
    }

我希望这是可以理解的,如果不是,请注意它不干净。 感谢

更新

我的onAddToStage功能

public function onAddToStage(event: Event):void{
    stage.focus = this;
this.addEventListener(KeyboardEvent.KEY_DOWN, onPushSpace);
    }

1 个答案:

答案 0 :(得分:2)

尝试将关键监听器添加到舞台:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onPushSpace);

否则你当前的课程需要成为焦点,这就是为什么只有你点击它才有效。确保在屏幕上的游戏完成后删除该侦听器。

或者,您可以在加载时(在构造函数中)通过代码将游戏放在屏幕焦点上:

public function GameOverScreen(useMouseControl:Boolean) {
    this.addEventListener(Event.ADDED_TO_STAGE,addedToStage,false,0,true);

    if(useMouseControl){
        Mouse.show();
        restartButton.addEventListener(MouseEvent.CLICK, onClickRestart, false, 0, true);
    }
    else{
        this.addEventListener(KeyboardEvent.KEY_DOWN, onPushSpace, false, 0, true);
    }
}

private function addedToStage(e:Event):void {
    stage.focus = this;
    stage.stageFocusRect = false;  //make sure there's no dumb yellow rectangle
}

还有一点提示 - 我注意到你的游戏一旦完成就不会从屏幕上移除你的游戏。你需要这样做才能让它真正消失(并删除你的重启事件监听器)。

public function restartGame():void {
    playScreen = new PlayScreen(useMouseControl);
    playScreen.addEventListener(AvatarEvent.DEAD, onAvatarDeath);
    addChild(playScreen);

    gameOverScreen.removeEventListener(NavigationEvent.RESTART, onRequestRestart);
    removeChild(gameOverScreen);
    gameOverScreen = null;
}