我有启动和停止按钮,但我无法弄清楚如何制作重置按钮并让时钟指针移回原位,请提供帮助。以下是我的代码
import flash.utils.Timer;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
var TimerOne: Timer = new Timer(1000);
myStartButton.addEventListener(MouseEvent.CLICK, startTimer);
myStopButton.addEventListener(MouseEvent.CLICK, stopTimer);
myResetButton.addEventListener(MouseEvent.CLICK, resetTimer);
TimerOne.addEventListener(TimerEvent.TIMER, moveHand);
//START BUTTON FUNCTION
function startTimer(e: MouseEvent): void
{
TimerOne.start();
trace("Timer started");
}
function moveHand(e: TimerEvent) : void
{
mySecondHand.rotation = mySecondHand.rotation + 6
myMinuteHand.rotation = myMinuteHand.rotation + 0.1;
}
var TimerTwo: Timer = new Timer(12000);
TimerTwo.addEventListener(TimerEvent.TIMER, moveHourHand);
function moveHourHand(e: TimerEvent) : void
{
myHourHand.rotation = myHourHand.rotation + 0.1;
}
//STOP BUTTON FUNCTION
function stopTimer(e: MouseEvent) : void
{
TimerOne.stop();
TimerTwo.stop();
trace("Timer stopped");
}
这是我试图找出重置计时器的地方
//RESET BUTTON FUNCTION
function resetTimer(e: MouseEvent) : void
{
TimerOne.reset();
TimerTwo.reset();
trace("Timer reset");
}
答案 0 :(得分:1)
如果说复位意味着将所有手柄(秒,分钟和小时)移动到主要位置,您应该这样做:
function resetTimer(e: MouseEvent) : void
{
mySecondHand.rotation = 0;
myMinuteHand.rotation = 0;
myHourHand.rotation = 0;
//You shouldn't do anything with your timer if you want it to continue counting.
//TimerOne.reset();
//TimerTwo.reset();
trace("Timer reset");
}
假设您的初始句柄旋转为0,则可以正常工作。如果不是,则应将旋转值保存到变量并在重置时使用它们。 这看起来像这样:
var TimerOne: Timer = new Timer(1000);
.
.
.
var sRotation:Number = mySecondHand.rotation;
var mRotation:Number = myMinuteHand.rotation;
var hRotation:Number = myHourHand.rotation;
.
.
.
function resetTimer(e: MouseEvent) : void
{
mySecondHand.rotation = sRotation;
myMinuteHand.rotation = mRotation;
myHourHand.rotation = hRotation;
}
我还建议只为所有句柄使用一个计时器(秒,分钟和小时)。我看到你每秒钟移动秒针(1000毫秒)和每小时处理12秒(12000毫秒)所以我建议你这样做:
var hCount:int = 0;
function moveHand(e: TimerEvent) : void
{
hCount++;
if(hCount == 12)
{
//The following is the same as this:
//myHourHand.rotation = myHourHand.rotation + 0.1;
myHourHand.rotation += 0.1;
hCount = 0;
}
mySecondHand.rotation += 6;
myMinuteHand.rotation += 0.1;
}