闪光AS3的KeyboardEvent不触发 [英] Flash AS3 KeyboardEvent not firing

查看:235
本文介绍了闪光AS3的KeyboardEvent不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类这样的:

 公共类GameOverScreen扩展影片剪辑{
    公共职能GameOverScreen(useMouseControl:布尔){
        如果(useMouseControl){
            Mouse.show();
            restartButton.addEventListener(MouseEvent.CLICK,onClickRestart);
        }
        其他{
            this.addEventListener(KeyboardEvent.KEY_DOWN,onPushSpace);
        }
    }

    公共职能onClickRestart(的MouseEvent:MouseEvent)方法:无效{
        则dispatchEvent(新NavigationEvent(NavigationEvent.RESTART));
    }

    公共职能onPushSpace(KeyboardEvent的:KeyboardEvent的):无效{
        跟踪(KeyboardEvent的);
        则dispatchEvent(新NavigationEvent(NavigationEvent.RESTART));
    } ...
 

这是一场游戏的结束画面。 (惊喜!)我希望它重新开始我的比赛,如果我按向下的空间按钮,或者点击restartButton在屏幕上。正如你可以看到屏幕获取一个布尔值在构造函数中,至极决定我们使用键盘或鼠标来控制游戏。它可以与鼠标,但与关键,我必须点击重启按钮(至极是在屏幕上),直到它什么都不做,然后单击后,我按一下按钮我获得播放屏幕,但我的KeyListener是somewhy还在工作,如果我按任何键,重新启动游戏。

我的主类的观点是:如果玩家死亡,他获得了gameOverScreen,并播放屏幕将被解雇,在gameOverScreen也得到一个监听器,它监听称为RESTART事件,如果事件被分派,一新的播放屏幕被创建,并在驳回了比赛。

 公共类躲避扩展影片剪辑{....
     公共职能onAvatarDeath(avatarEvent:AvatarEvent):无效{

        VAR finalScore:数= playScreen.getFinalScore();
        VAR finalTime:数= playScreen.getFinalTime();

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

        播放屏幕= NULL;
    }

    公共职能restartGame():无效{
        播放屏幕=新的播放屏幕(useMouseControl);
        playScreen.addEventListener(AvatarEvent.DEAD,onAvatarDeath);
        的addChild(播放屏幕);

        gameOverScreen = NULL;
    }

    公共职能onRequestRestart(navigationEvent:NavigationEvent):无效{
        restartGame();
    }
 

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

更新

我onAddToStage功能

 公共职能onAddToStage(事件:事件):无效{
    stage.focus =这一点;
this.addEventListener(KeyboardEvent.KEY_DOWN,onPushSpace);
    }
 

解决方案

试着增加你的关键监听器在舞台:

  stage.addEventListener(KeyboardEvent.KEY_DOWN,onPushSpace);
 

否则,当前类需要成为市场关注焦点,这就是为什么只能直到你点击它。一定要删除,当你的游戏在画面,虽然这样做了听众。

另外,您可以通过code给你的游戏结束画面的焦点,当它加载(在构造函数中):

 公共职能GameOverScreen(useMouseControl:布尔){
    this.addEventListener(Event.ADDED_TO_STAGE,addedToStage,假,0,真正的);

    如果(useMouseControl){
        Mouse.show();
        restartButton.addEventListener(MouseEvent.CLICK,onClickRestart,假,0,真正的);
    }
    其他{
        this.addEventListener(KeyboardEvent.KEY_DOWN,onPushSpace,假,0,真正的);
    }
}

私有函数addedToStage(五:事件):无效{
    stage.focus =这一点;
    stage.stageFocusRect = FALSE; //确保没有愚蠢的黄色矩形
}
 

另外一个小秘密 - 我注意到你没有从显示列表中删除你的游戏屏幕上,一旦它的完成。你要做到这一点,使之真正消失(和删除您重新启动事件侦听器)。

 公共职能restartGame():无效{
    播放屏幕=新的播放屏幕(useMouseControl);
    playScreen.addEventListener(AvatarEvent.DEAD,onAvatarDeath);
    的addChild(播放屏幕);

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

I have a class like this:

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));
    }...

It's an ending screen of a game. (surprise!) I want it to restart my game if I push down the space button, or click on the restartButton on the screen. As you can see the screen gets a boolean value in the constructor, wich decide that we're using keyboard or mouse to control the game. It works well with mouse, but with the key, i have to click on the restart button (wich is on the screen), till it does nothing, and after clicking it, and I push a button I get the playScreen, but my keylistener is somewhy still in work, and if i push any key, it restarts the game.

The point of my main class is: if the player dies, he get a gameOverScreen, and the playscreen will be dismissed, the gameOverScreen also gets a listener, it listens for an event called RESTART, if the event is dispatched, a new playScreen is created, and the game over dismissed.

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();
    }

I hope it's understandable, if not please note it what is not clean. Thanks

UPDATE

my onAddToStage function

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

解决方案

Try adding your key listener to the stage:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onPushSpace);

Otherwise your current class needs to be in focus, which is why only works until you've clicked it. Be sure to remove that listener when your game over screen is done though.

Alternatively you could give your game over screen focus through code when it loads (in the constructor):

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
}

Also a little tip - I notice your not removing your game over screen from the display list once it's finished. You'll want to do that to make it truly go away (and remove your restart event listener).

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

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

这篇关于闪光AS3的KeyboardEvent不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆