动作脚本 3.0。将计时器改造为倒数计时器
Action Script 3.0. Remake timer to countdown timer
我有一个计时器,它从 00:00
开始按照以下格式 mm:ss
计时。我需要制作从 30
秒
开始的倒数计时器
我的计时器看起来像:
public class Test extends Sprite {
var timer:Timer;
var txtTime:TextField;
var tmpTime:Number; //this will store the time when the game is started
}
function game(e:MouseEvent) {
timer = new Timer(0); //create a new timer that ticks every second.
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick
timer.addEventListener(TimerEvent.TIMER, resetTimer);
txtTime = new TextField();
txtTime.text = setNull(0);
var format:TextFormat = new TextFormat();
format.color = 0x000000;
format.size = 22;
format.bold = true;
txtTime.defaultTextFormat = format;
txtTime.x = 603;
txtTime.y = 143;
addChild(txtTime);
timer.reset();
tmpTime = flash.utils.getTimer();
timer.start();
}
function setNull(startTime:int):String
{
return "00:00";
}
private function tick(e:Event):void {
txtTime.text = showTimePassed(tmpTime);
}
function showTimePassed(startTime:int):String {
var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
var leadingZeroM:String = "";
var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds
if (seconds < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroS = "0";
}
if (seconds < 600) { //if seconds are less than two digits, add the leading zero
leadingZeroM = "0";
}
var minutes = Math.floor( (time / (60 * 1000) ) ); //60 seconds times 1000 miliseocnds gets the minutes
return leadingZeroM + minutes + ":" + leadingZeroS + seconds;
}
public function resetTimer(event:TimerEvent):void
{
var timer:Timer = event.target as Timer;
timer.reset();
timer.start();
}
function getTimePassed(startTime:int):Number
{
return (getTimer() - startTime) /1000;
}
我无法成功将它更改为 30 秒倒计时。
我尝试在下面声明计时器:timer = new Timer(1000, 30);
并更改 var time = 30 - getTimer();
而不是 var time = getTimer() - startTime;
但它不起作用。
你有想法吗?谢谢!
要创建倒计时,您可以像这样使用 Timer.currentCount
:
var time:int = 0;
var timer:Timer = new Timer(1000, 30)
timer.addEventListener(TimerEvent.TIMER, onTimer);
function onTimer(e:TimerEvent):void {
time = timer.repeatCount - timer.currentCount;
trace('00:' + (time < 10 ? '0' + time : time)); // gives : 00:29, 00:28, .., 00:01, 00:00
}
timer.start();
trace('00:' + (timer.repeatCount - time)); // gives : 00:30
希望能帮到你。
我有一个计时器,它从 00:00
开始按照以下格式 mm:ss
计时。我需要制作从 30
秒
我的计时器看起来像:
public class Test extends Sprite {
var timer:Timer;
var txtTime:TextField;
var tmpTime:Number; //this will store the time when the game is started
}
function game(e:MouseEvent) {
timer = new Timer(0); //create a new timer that ticks every second.
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick
timer.addEventListener(TimerEvent.TIMER, resetTimer);
txtTime = new TextField();
txtTime.text = setNull(0);
var format:TextFormat = new TextFormat();
format.color = 0x000000;
format.size = 22;
format.bold = true;
txtTime.defaultTextFormat = format;
txtTime.x = 603;
txtTime.y = 143;
addChild(txtTime);
timer.reset();
tmpTime = flash.utils.getTimer();
timer.start();
}
function setNull(startTime:int):String
{
return "00:00";
}
private function tick(e:Event):void {
txtTime.text = showTimePassed(tmpTime);
}
function showTimePassed(startTime:int):String {
var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
var leadingZeroM:String = "";
var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds
if (seconds < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroS = "0";
}
if (seconds < 600) { //if seconds are less than two digits, add the leading zero
leadingZeroM = "0";
}
var minutes = Math.floor( (time / (60 * 1000) ) ); //60 seconds times 1000 miliseocnds gets the minutes
return leadingZeroM + minutes + ":" + leadingZeroS + seconds;
}
public function resetTimer(event:TimerEvent):void
{
var timer:Timer = event.target as Timer;
timer.reset();
timer.start();
}
function getTimePassed(startTime:int):Number
{
return (getTimer() - startTime) /1000;
}
我无法成功将它更改为 30 秒倒计时。
我尝试在下面声明计时器:timer = new Timer(1000, 30);
并更改 var time = 30 - getTimer();
而不是 var time = getTimer() - startTime;
但它不起作用。
你有想法吗?谢谢!
要创建倒计时,您可以像这样使用 Timer.currentCount
:
var time:int = 0;
var timer:Timer = new Timer(1000, 30)
timer.addEventListener(TimerEvent.TIMER, onTimer);
function onTimer(e:TimerEvent):void {
time = timer.repeatCount - timer.currentCount;
trace('00:' + (time < 10 ? '0' + time : time)); // gives : 00:29, 00:28, .., 00:01, 00:00
}
timer.start();
trace('00:' + (timer.repeatCount - time)); // gives : 00:30
希望能帮到你。