2012-03-20 89 views
-1

我有一個記憶遊戲程序,當計時器用完時,我希望它進入第3幀,顯示「遊戲失敗」頁面。gotoandstop問題actionscript 3

我已經設置了所有設置,除非遊戲耗盡時間,框架看起來與原始框架重疊,而不是轉到完全獨立的頁面。

任何人都可以幫助我嗎?

這裏是我的代碼:

package { 
import flash.display.Sprite; 
import flash.events.MouseEvent; 
import flash.events.TimerEvent; 
import flash.utils.Timer; 
import flash.display.MovieClip; 
import flash.text.TextField; 

public class MemoryGame extends MovieClip{ 

    private var firstTile:cards; 
    private var secondTile:cards; 
    private var pauseTimer:Timer; 
    private var score:int; 
    private var cardCount:int; 
    var seconds:Number; 
    var minutes:Number; 


    var numberDeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6); 


    public function MemoryGame(){ 

     //TIMER FUNCTION 
     var levelTimer:Timer = new Timer(1000, 180); 
     levelTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler); 
     levelTimer.addEventListener(TimerEvent.TIMER, timerHandler); 

     // LEVEL FUNCTION 
     easyBtn.addEventListener(MouseEvent.CLICK, easyButtonClicked); 
     medBtn.addEventListener(MouseEvent.CLICK, medButtonClicked); 
     hardBtn.addEventListener(MouseEvent.CLICK, hardButtonClicked); 

     score = 0; 
     txtScore.text=""+score; 

     //Level button events 
     function easyButtonClicked(e:MouseEvent):void{ 
      removeChild(levelText); 
      trace("easy button clicked!"); 
      seconds = 0; 
      minutes = 1; 
      txtTime.text = "1:00"; 
      levelTimer.start(); 
      setupTiles(); 
     } 

     function medButtonClicked(e:MouseEvent):void{ 
      removeChild(levelText); 
      trace("medium button clicked!"); 
      seconds = 30; 
      minutes = 0; 
      txtTime.text = "0:30"; 
      levelTimer.start(); 
      setupTiles(); 
     } 

     function hardButtonClicked(e:MouseEvent):void{ 
      removeChild(levelText); 
      trace("hard button clicked!"); 
      seconds = 15; 
      minutes = 0; 
      txtTime.text = "0:15"; 
      levelTimer.start(); 
      setupTiles(); 
     } 


     //Timer handlers 
     function timerHandler(e:TimerEvent):void { 
      if (seconds > 00) { 
      seconds -=1; 
      } 

      else { 
       if (minutes > 0) {minutes -=1;seconds = 59;} 
     } 
       txtTime.text = minutes+":"+(seconds >= 10 ? seconds : "0"+seconds); 
      } 

     function timerCompleteHandler(e:TimerEvent):void { 
      e.target.reset(); 
      e.target.stop(); 
      trace("game over!"); 
     } 



     //Tiles set up 
     function setupTiles(){ 
     for(x=1; x<=4; x++) { 
      for (y=1; y<=3; y++){ 
       var randomCard = Math.floor(Math.random()*numberDeck.length); 
       var tile:cards = new cards(); 
       tile.card = numberDeck[randomCard]; 
       numberDeck.splice(randomCard,1); 
       tile.gotoAndStop(9); 
       tile.x = (x-1) * 150; 
       tile.y = (y-1) * 200; 
       tile.addEventListener(MouseEvent.CLICK,tileClicked); 
       addChild(tile); 
       cardCount = cardCount + 1 
      } 
     } 
    } 
    } 




    public function tileClicked(event:MouseEvent) { 
     var clicked:cards = (event.currentTarget as cards); 
     if (firstTile == null){ 
      firstTile = clicked; 
      firstTile.gotoAndStop(clicked.card); 
     } 
     else if (secondTile == null && firstTile != clicked){ 
      secondTile = clicked; 
      secondTile.gotoAndStop(clicked.card); 
      if (firstTile.card == secondTile.card){ 
       pauseTimer = new Timer(1000, 1); 
       pauseTimer.addEventListener(TimerEvent.TIMER_COMPLETE,removeCards); 
       pauseTimer.start(); 

      } 
      else { 
       pauseTimer = new Timer(1000, 1); 
       pauseTimer.addEventListener(TimerEvent.TIMER_COMPLETE,resetCards); 
       pauseTimer.start(); 
      } 
     } 

     if (seconds == 0){ 
      this.gotoAndStop(2); 
      pauseTimer.stop(); 
      //levelTimer.stop(); 
     } 
    } 


    public function resetCards(event:TimerEvent) { 
     firstTile.gotoAndStop(9); 
     secondTile.gotoAndStop(9); 
     firstTile = null; 
     secondTile = null; 
     pauseTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,resetCards); 
     score = score - 2; 
     txtScore.text=""+score; 
    } 


    public function removeCards(event:TimerEvent){ 
     removeChild(firstTile); 
     removeChild(secondTile); 
     firstTile = null; 
     secondTile = null; 
     pauseTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,removeCards); 
     score = score + 10; 
     txtScore.text=""+score; 
     cardCount = cardCount - 2; 
     trace("Cardcount: " + cardCount); 

     if (cardCount == 0){ 
      this.gotoAndStop(2); 
      txtFinalScore.text=" "+score; 
      pauseTimer.stop();   
      } 
       } 
} 
} 

太謝謝你了!

回答

0

當您使用addChild(object)添加對象時,它不會與時間軸上的關鍵幀相關聯。

所以你需要做的是不是跳到第2幀,removeChild(object)或object.visible = false所有你不想要的孩子和addChild(對象)你的'超時'資產。

良好的職業道德是創建destroy()函數,以刪除和清空任何不需要的資產。通過這種方式,您可以輕鬆刪除不需要的項目並釋放內存。

+0

好吧,它工作!現在,當我點擊「重播」按鈕時,如何重置遊戲?只需添加所有的孩子並返回一個框架? – DommyCastles 2012-03-20 07:20:58

+0

我建議嘗試忘記跳到幀。嘗試使用執行某些任務的函數來設置代碼,例如設置新遊戲,結束遊戲,開始下一個級別等。 – crooksy88 2012-03-20 09:53:05