我正在创建 Flash 游戏,但我遇到了奇怪的问题。当我在菜单中时计时器开始计数,但游戏没有开始。在菜单中,我有“播放”按钮,单击它后添加计时器,但它显示程序运行了多长时间(从当前时间开始计数)。
这是启动的主要功能
public function MemoryGame()
{
startMemoryGame.addEventListener(MouseEvent.CLICK, startPlay);
}
这是开始游戏的按钮:
function startPlay(e:MouseEvent):void
{
startMemoryGame();
}
这是我添加 Timer 和其他对象的函数。
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()
我有同样的问题,在我点击“startPlay”按钮后它添加了当前计时器(例如:00:12)只是计时器停止了。
我尝试使用timer.reset();
,但同样的问题,所以我没有更多的想法有什么问题。而且我不明白如果我以前不使用任何功能,它为什么会开始计时。请问你能帮帮我吗?非常感谢。