2012-02-06 132 views
0

嗨,我需要幫助來停止使用3.0的Flash遊戲中的計時器,我需要這個暫停遊戲,因爲我使用getTimer()函數移動一些目標,我可以使目標停止移動當遊戲暫停時,但由於getTimer()繼續運行,當我取消暫停遊戲時,屏幕上的目標消失(它們的位置變化太快)。有沒有停止計時器的方法或者像getTimer那樣平滑移動目標的更好方法? her5e是我的代碼:AS 3.0停止計時器

//animates targets 
     private function MoveTarget(e:Event) { 

      if (!MovieClip(parent).pauseGame) { 
       //get time passed 
       timePassed = getTimer() - lastTime; 
       lastTime += timePassed; 
      } 

      //move the target 
      this.x += dx * timePassed/1000; 

      //check to see if the target is offscreen 
      switch (targetType) { 
       case "small": 
        if (dx > 0 && this.x > 771) {  //left->right target 
         deleteTarget(false); 
        } else if (dx < 0 && this.x < -26) { //left<-right target 
         deleteTarget(false); 
        } 
        break; 
       case "medium": 
        if (dx > 0 && this.x > 790) {  //left->right target 
         deleteTarget(false); 
        } else if (dx < 0 && this.x < -40) { //left<-right target 
         deleteTarget(false); 
        } 
        break; 
       case "big": 
        if (dx > 0 && this.x > 800) {  //left->right target 
         deleteTarget(false); 
        } else if (dx < 0 && this.x < -50) { //left<-right target 
         deleteTarget(false); 
        } 
        break; 
      } 

     } 

回答

1

這裏有一個簡單的解決方案,但我想你應該瞭解什麼是時間步長,它是遊戲編程的關鍵概念,見Fix Your Timestep!

if (!MovieClip(parent).pauseGame) { 
    //get time passed 
    timePassed = getTimer() - lastTime; 
    lastTime += timePassed; 
} 
else 
{ 
    lastTime = getTimer(); 
    return; 
} 
+0

真棒!謝謝,我不知道爲什麼我沒有想到這一點,你也解決了另一個問題,我實例化新的目標(我需要檢查爲什麼),偉大= D再次感謝! – ershin69 2012-02-06 15:37:11