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