Bon*_*arr 2 flash actionscript-3
嘿伙计,所以我有一个工作的Timer,当我调用它并启动时会停止(),但我不能让它重置().它只是从它停止的地方开始.
计时器的代码
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我称之为做事的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
在你的resetTimer()
功能中,你正在重置你的计时器(这很好用).但问题是你没有使用Timer本身来计算你的秒变量.因此,当您再次启动计时器时,秒将继续从中断处继续.
您需要做的是重置seconds
变量.
public function resetTimer(): void
{
seconds = 0;
timer.reset();
timer.start();
updateDisplay();
}
Run Code Online (Sandbox Code Playgroud)
请记住,在这种情况下,您的计时器只是一个工具,用于显示您对seconds
变量中存储的秒数的解释.因此操纵定时器不会改变秒数和显示.
归档时间: |
|
查看次数: |
5292 次 |
最近记录: |