2014-06-19 2 views
0

버튼이 한 번만 작동하는 데 문제가 있습니다. 단추는 사용자 지정 이벤트를 발생시킵니다. 커스텀 이벤트가 발생하고, DocumentClass에있는 리스너가 처음으로 그것을 선택하지만 그 이후에는 아무런 응답도하지 않습니다.버튼은 한 번만 작동합니다.

Click Here를 복제하려면, 다음 다음 메뉴 버튼을 오른쪽 상단 "게임 시작"을 클릭 한 다음 메인 메뉴를 클릭 한 다음 "게임 시작"을 클릭 한 후 메뉴 버튼을 오른쪽 상단에, 당신은 다시 메인 메뉴를 클릭 할 수 없습니다 통지 . 그게 내 문제 야. 그것은 계속 작동해야합니다.

DocumentClass는 하위 openScreen을 추가합니다. openScreen은 DocumentClass에 자식 playScreen을 추가하고 openScreen을 제거하기위한 이벤트를 발생시킵니다. playscreen은 playScreen의 메뉴 버튼을 클릭하면 하위 menuScreen을 추가합니다. ** 주 메뉴 버튼을 클릭하면 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은, 때문에 당신의 onMain 방법에 무비 클립 {

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) 
     { 

     } 
    } 

답변

2

확장 스테이지에서 제거한 다음 재생 화면의 새로운 인스턴스를 만듭니다. 그래서 당신의 청취자는 오래된 것에 붙어 있습니다. 또한 메모리 누수가 발생합니다.

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

그것은이이 문제가되는 것을 의미한다 (생성자의 재생 화면 코드를 꺼내 당신이 그것을 선언 할 때 인스턴스화하지 않는 것) 그 일을 버튼의 내 문제를 해결하고. 새로운 playScreen은 모든 게임 메커니즘이 playScreen에 포함되어 있기 때문에 새로운 게임을 만드는 내 방식이었습니다. 그 아이를 부르기 전에 그 아이를 원래대로 되돌릴 수있는 더 좋은 방법이 있습니까? – Jimbo145

+1

아마도 더 효율적인 방법 일 수 있지만 전체적으로 응용 프로그램에 따라 달라질 수 있습니다. 적절한 퍼포먼스가있는 경우 클리어링 및 인스턴스 다시 작성에는 아무런 문제가 없습니다. 이전 재생 화면을 메모리 (이벤트 리스너와 같은)로 유지하는 것이 아무것도 없는지 확인하십시오. – BadFeelingAboutThis

관련 문제