我正在创建flash游戏,我遇到了奇怪的问题。计时器开始计数时,我在菜单,但游戏没有开始。在菜单中,我有按钮"播放",点击后它添加定时器,但它显示程序运行多长时间(从当前时间开始计数)。
main函数
public function MemoryGame()
{
startMemoryGame.addEventListener(MouseEvent.CLICK, startPlay);
}
这是开始游戏的按钮:
function startPlay(e:MouseEvent):void
{
startMemoryGame();
}
这是我的函数,其中计时器和其他对象添加。
function startMemoryGame():void
{
timer = new Timer(1000); //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();
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = "#E50041";
format.size = 22;
txtTime.border = true;
txtTime.borderColor = 0xFFFFFF;
//format.bold = true;
//txtTime.x = 250;
txtTime.width/2;
var stageCenter_x:Number = stage.stageWidth/2;
var stageCenter_y:Number = stage.stageHeight/2;
var textCenter_x:Number = txtTime.width/2;
var textCenter_y:Number = txtTime.height/2;
txtTime.x = stageCenter_x - textCenter_x;
txtTime.y = 55;
txtTime.autoSize = TextFieldAutoSize.CENTER;
txtTime.defaultTextFormat = format;
message_txt.autoSize = TextFieldAutoSize.CENTER;
message_txt.defaultTextFormat = format;
//here Timer starts
txtTime.text = showTimePassed(0);
addChild(txtTime);
tmpTime = timer.currentCount;
timer.start();
_cards = new Array();
_totalMatches = 18;
_currentMatches = 0;
createCards();
}
private function tick(e:Event):void {
txtTime.text = showTimePassed(timer.currentCount - tmpTime);
}
function showTimePassed(startTime:int):String {
var leadingZeroMS:String = ""; //how many leading 0's to put in front of the miliseconds
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 miliseconds = (time % 1000); // modulus (%) gives you the remainder after dividing,
if (miliseconds < 10) { //if less than two digits, add a leading 0
leadingZeroMS = "0";
}
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";
}
var minutes = Math.floor((time / (60 * 1000) ) );
if (minutes < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroM = "0";
}
//60 seconds times 1000 miliseocnds gets the minutes
return leadingZeroM + minutes + ":" + leadingZeroS + seconds ;
}
什么是奇怪的,如果我删除命令timer.start()
我有同样的问题,我点击"开始播放"按钮后,它添加当前计时器(例如:00:12)只是计时器停止。
我试图使用timer.reset();
,但同样的问题,所以我没有更多的想法是什么错了。我不明白为什么如果我之前没有用过任何函数,它就开始计时了。你能帮我一下吗?非常感谢。
在showTimePassed
中有这样一行:
var time = getTimer() - startTime;
getTimer()
以毫秒为单位获得程序启动后经过的时间(参见文档)
startTime
应该是你开始游戏的时间,单位是毫秒。
因此,如果在startMemoryGame
中使用定时器,只需将getTimer()
的值存储在成员变量中,并将该变量作为参数传递给showTimePassed
。或者更改showTimePassed
的代码以满足您的需要。
修改一下:
private function tick(e:Event):void {
txtTime.text = showTimePassed(timer.currentCount - tmpTime);
}
:
private function tick(e:Event):void {
txtTime.text = showTimePassed(tmpTime);
}
这:tmpTime = timer.currentCount;
tmpTime = getTimer();
它应该给你正确的时间,如果你没有使用tmpTime
在其他地方。否则就声明另一个变量。
说明:showTimePassed
只是输出程序启动后经过的时间,但它从该时间中减去了参数startTime
。输入0将给出启动程序后的时间。我建议的更改存储自tmpTime
中程序执行以来的时间,当你开始游戏时,并将其传递给showTimePassed
。
启动程序。getTime()将给出~0
五秒钟后,你按下开始键。getTime()将是~5000,存储在tmpTime 中showTimePassed在1秒后调用tmpTime(5000)。getTimer()的值是6000,减去startTime的值是1000,这是你按下开始键后的一秒。
这里有一些关于成员变量的信息:https://en.wikipedia.org/wiki/Member_variable