我有这样的课:
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);
}