2011-11-30 66 views
2

嘿傢伙,所以我有一個工作的定時器,它會停止()時,我打電話,並啓動,但我不能讓它重置()。它只是從停止的地方開始。定時器將不會重置

代碼爲定時器

package 
{ 

    import flash.display.MovieClip; 
    import flash.display.Stage; 
    import flash.text.TextField; 
    import flash.events.Event; 
    import flash.utils.Timer; 
    import flash.events.TimerEvent; 


    public class Score extends MovieClip 
    { 
     public var second:Number = 0; 
     public var timer:Timer = new Timer(100); 
     private var stageRef:Stage; 
     private var endScore:displayFinalScore; 

     public function Score(stageRef:Stage) 
     { 
      x = 560.95; 
      y = 31.35; 
      this.stageRef = stageRef; 
      this.endScore = endScore; 

      timer.addEventListener(TimerEvent.TIMER, scoreTimer); 
      timer.reset(); 
      timer.start(); 
      updateDisplay(); 


     } 

     public function scoreTimer(evt:TimerEvent):void 
     { 

      second += 1; 
      updateDisplay(); 
     } 

     public function get10Points(points:Number) : void 
     { 
      second += points; 
      updateDisplay(); 
     } 

     public function finalScore() : void { 

      timer.stop(); 
     } 

     public function resetTimer(): void { 

      timer.reset(); 
      timer.start(); 
      updateDisplay(); 
     } 

     public function updateDisplay(): void 
     { 

      scoreDisplay.text = String("Score: " +second); 

     } 
    } 
} 

代碼,我把它做的事情:

private function sendGameOver(e:Event) : void 
{ 
    //Stop the stage and remove everything 
    stop(); 
    stage.removeChild(ourBoat); 
    stage.removeChild(score); 
    removeEventListener(Event.ENTER_FRAME, loop); 

    //Add the end score  
    addChild(endScore); 
    //Add the game over 
    addChild(endGame); 
    addChild(playAgain); 

    //Add the play again options 
    playAgain.addEventListener(MouseEvent.CLICK, newGame); 
    score.finalScore(); 


} 

public function newGame (e:MouseEvent) :void 
{ 
    score.resetTimer(); 
    gotoAndPlay(1); 
    removeChild(endGame); 
    removeChild(endScore); 
    removeChild(playAgain); 
    stage.addChild(ourBoat); 
    stage.addChild(score); 

    //Keep the boat within the desired boundaries 
    ourBoat.x = stage.stageWidth/2; 
    ourBoat.y = stage.stageHeight - 100; 

    addEventListener(Event.ENTER_FRAME, loop, false, 0, true); 
} 
+0

你是什麼意思,它開始的地方停止了? – sean

回答

5

在你resetTimer()功能你重置定時器(這工作得很好)。但事情是你沒有使用Timer本身來計算你的秒變量。所以,當你再次啓動定時器時,秒數將繼續停止。

你需要做的是重置seconds變量。

public function resetTimer(): void 
{ 
    seconds = 0; 
    timer.reset(); 
    timer.start(); 
    updateDisplay(); 
} 

請記住,在這種情況下,你的計時器只是你用來顯示自己的存儲在您的seconds變量秒的解釋的工具。所以操作定時器不會改變秒數和顯示。

+0

啊啊,謝謝你的作品,非常感謝! – BoneStarr