0

我有这样的课:

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,并且playscreen会被关闭,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);
    }
4

1 回答 1

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;
}
于 2012-08-23T17:28:06.337 回答