Reputation: 133
I'm having an issue with my buttons only working once. The button fires a custom event. The custom event fires, and a listener on my DocumentClass picks it up the first time, but doesn't respond any time after that.
To Replicate Click Here, then click "Start Game", then the menu button top right, then click main menu, then click "Start Game", then the menu button top right, then notice you cannot click main menu again. That is my problem. It should continue to work.
DocumentClass adds child openScreen. openScreen fires event for DocumentClass to add child playScreen and remove openScreen. playscreen adds child menuScreen when the menu button on playScreen is clicked. **When the main menu button is clicked, it fires an event that is listened for by the DocumentClass. The function that is triggered removes the playScreen and adds an open Screen. This series of events only works once fully before hanging up when the ** step.
Document class
public class DocumentClass extends MovieClip
{
public var playScreen:PlayScreen = new PlayScreen();
public var openScreen:OpenScreen = new OpenScreen();
public var started:Boolean = false;
public function DocumentClass()
{
openScreen.addEventListener( GameEvent.STAR, OnPlayClick);
playScreen.addEventListener( GameEvent.NG, NewGame);
playScreen.menuScreen.addEventListener( GameEvent.NG, NewGame, true, 0, true);
playScreen.menuScreen.addEventListener( GameEvent.MM, onMain);
addChild(openScreen)
}
public function OnPlayClick(gameEvent:GameEvent):void{
trace("start")
addChildAt(playScreen,0)
var tweenX:Tween = new Tween(openScreen, "x", None.easeIn, 0, -480, .5, true);
tweenX.addEventListener(TweenEvent.MOTION_FINISH, onTweenDone);
}
public function NewGame(gameEvent:GameEvent):void{
removeChild(playScreen)
playScreen = new PlayScreen();
addChild(playScreen)
playScreen.begin()
}
public function onMain(gameEvent:GameEvent):void{
playScreen.removeChild(playScreen.menuScreen)
this.removeChild(playScreen)
//openScreen = new OpenScreen();
openScreen.x = 0
addChild(openScreen)
playScreen = new PlayScreen();
MenuScreen public class MenuScreen extends MovieClip {
public function MenuScreen()
{
backButton.buttonMode = true
backButton.addEventListener( MouseEvent.CLICK, OnBackClick );
exitButton.buttonMode = true
exitButton.addEventListener( MouseEvent.CLICK, OnExitClick );
restartButton.buttonMode = true
restartButton.addEventListener( MouseEvent.CLICK, OnRestartClick );
mainButton.buttonMode = true
mainButton.addEventListener( MouseEvent.CLICK, OnMainClick );
//NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onSystemKey);
backButton.AnswerText.text = "Return to Game"
restartButton.AnswerText.text = "Restart"
mainButton.AnswerText.text = "Main Menu"
exitButton.AnswerText.text = "Exit"
}
public function OnBackClick(myEvent:MouseEvent):void{
this.dispatchEvent( new GameEvent( GameEvent.BAC ))
}
public function OnExitClick(myEvent:MouseEvent):void{
//NativeApplication.nativeApplication.exit();
}
public function OnRestartClick(myEvent:MouseEvent):void{
this.dispatchEvent( new GameEvent( GameEvent.NG ))
trace("restrt click")
}
public function OnMainClick(myEvent:MouseEvent):void{
this.dispatchEvent( new GameEvent( GameEvent.MM ))
trace("main click")
}
protected function onSystemKey(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.BACK)
{
e.preventDefault();
this.dispatchEvent( new GameEvent( GameEvent.BAC ))
}
else if(e.keyCode == Keyboard.HOME)
{
//handle the button press here.
}
else if(e.keyCode == Keyboard.MENU)
{
//handle the button press here.
}
}
}
OpenScreen
public class OpenScreen extends MovieClip
{
public var hs:String
public function OpenScreen()
{
startButton.buttonMode = true
startButton.addEventListener( MouseEvent.CLICK, OnStartClick );
exitButton.buttonMode = true
exitButton.addEventListener( MouseEvent.CLICK, OnExitClick );
//NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onSystemKey);
readObject()
highScoreText.text = String(hs)
trace(highScoreText.text)
startButton.AnswerText.text = "Start"
//restartButton.AnswerText.text = "Restart"
//mainButton.AnswerText.text = "Main Menu"
exitButton.AnswerText.text = "Exit"
}
public function OnStartClick(myEvent:MouseEvent):void{
trace(this.hasEventListener( GameEvent.STAR ))
this.dispatchEvent( new GameEvent( GameEvent.STAR ))
}
public function OnExitClick(myEvent:MouseEvent):void{
//NativeApplication.nativeApplication.exit();
}
protected function onSystemKey(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.BACK)
{
//NativeApplication.nativeApplication.exit();
}
else if(e.keyCode == Keyboard.HOME)
{
//handle the button press here.
}
else if(e.keyCode == Keyboard.MENU)
{
}
}
Upvotes: 0
Views: 227
Reputation: 14406
It looks like the reason is because on your onMain
method, you remove from the stage and then create a brand new instance of the play screen. so your listeners are attached to the old one. That also creates a memory leak.
public function onMain(gameEvent:GameEvent):void{
playScreen.removeChild(playScreen.menuScreen)
this.removeChild(playScreen)
//openScreen = new OpenScreen();
openScreen.x = 0
addChild(openScreen)
playScreen = new PlayScreen(); //RIGHT HERE - this is creating a whole new play screen, your listeners are attached to the old one.
}
Should probably look like this: (take out the play screen code in the constructor and don't instantiate it when your declaring it)
//Create a function that kill your current play screen
public function clearPlayScreen():void {
if(!playScreen) return; //don't do anything if there isn't a play screen
playScreen.removeChild(playScreen.menuScreen)
this.removeChild(playScreen);
//remove your listeners so the old play screen can be garbage collected - other they will all stay in memory causing a memory leak
playScreen.removeEventListener( GameEvent.NG, NewGame);
playScreen.menuScreen.removeEventListener( GameEvent.NG, NewGame, true);
playScreen.menuScreen.removeEventListener( GameEvent.MM, onMain);
playScreen = null;
}
public function NewGame(gameEvent:GameEvent):void{
clearPlayScreen(); //kill the old play screen if it exists
playScreen = new PlayScreen();
addChild(playScreen);
//add the listeners to the new play screen
playScreen.addEventListener( GameEvent.NG, NewGame);
playScreen.menuScreen.addEventListener( GameEvent.NG, NewGame, true, 0, true);
playScreen.menuScreen.addEventListener( GameEvent.MM, onMain);
playScreen.begin();
}
public function onMain(gameEvent:GameEvent):void{
clearPlayScreen();
//openScreen = new OpenScreen();
openScreen.x = 0;
addChild(openScreen);
}
Upvotes: 2