我的按钮只有一次工作有问题。该按钮触发自定义事件。触发自定义事件,我的DocumentClass上的监听器第一次接收它,但在此之后不会响应。
要复制 Click Here,然后单击“开始游戏”,然后单击菜单按钮右上角,然后单击主菜单,再单击“开始游戏”,然后单击右上角的菜单按钮,然后注意您无法再次单击主菜单。那是我的问题。它应该继续工作。
DocumentClass添加了子openScreen。 openScreen为DocumentClass触发事件以添加子playScreen并删除openScreen。当点击playScreen上的菜单按钮时,playscreen会添加子菜单屏幕。 **单击主菜单按钮时,将触发DocumentClass侦听的事件。触发的功能将删除playScreen并添加一个打开的屏幕。这一系列事件只有在**步骤挂起之前才能完全运行一次。
文档课
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 公共类MenuScreen扩展了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)
{
}
}
答案 0 :(得分:2)
看起来原因是因为在您的onMain
方法中,您从舞台中删除,然后创建一个全新的播放屏幕实例。所以你的听众与旧的听众联系在一起。这也会造成内存泄漏。
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.
}
应该看起来像这样:(取出构造函数中的播放屏幕代码,并在声明它时不要实例化它)
//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);
}