Javascript 如何在Phaser JS中实现一个好的计时器
嗨,我目前正在为我的应用程序开发一个计时器,我将使用phonegap将其移植到android手机上,这是一个关于10秒计时器的球赛 当前im使用定时器++增量Javascript 如何在Phaser JS中实现一个好的计时器,javascript,android,cordova,timer,phaser-framework,Javascript,Android,Cordova,Timer,Phaser Framework,嗨,我目前正在为我的应用程序开发一个计时器,我将使用phonegap将其移植到android手机上,这是一个关于10秒计时器的球赛 当前im使用定时器++增量 createLevel: function(lvl) { // create levels manually // TODO: import from level editor switch(lvl) { case 1: { timer2 = 11;
createLevel: function(lvl) {
// create levels manually
// TODO: import from level editor
switch(lvl) {
case 1: {
timer2 = 11;
walls.create((320-128)/2, (480-32)/2, 'element-w').body.immovable = true;
break;
}
case 2: {
timer2 = 11;
walls.create code here
break;
}
case 3: {
timer2 = 11;
walls.create code here
break;
}
case 4: {
timer2 = 11;
walls.create code here
break;
}
case 5: {
timer2 = 11;
walls.create code here
break;
}
case 6:{
timer2 = 11;
walls.create code here
break;
}
case 7:{
timer2 = 11;
walls.create code here
break;
}
case 8:
{
timer2 = 11;
walls.create code here
break;
}
case 9:
{
timer2 = 11;
walls.create code here
break;
}
case 10:
{
timer2 = 11;
walls.create code here
break;
}
default: {
break;
}
}
},
updateCounter: function() {
timer++;
timer2--;
timerText.content = "Time: "+timer2;
totalTimeText.content = "Total time: "+(totalTimer+timer);
if(timer2==-1)
{
//this.game.add.text(320/3, 480/2, "GAMEOVER", { font: "36px Cambria", fill: "#ffff00" });
this.game.paused =! this.game.paused;
this.buttonContinue = this.add.button(0, 0, 'gameover', this.mainM, this);
totalTimer=0;
timer=-2;
level=1;
//this.buttonContinue = this.add.button(0, 0, 'screen-howtoplay', this.game.state.start('MainMenu');, this);
}
在游戏结束前,我用定时器加上总定时器,用定时器2将实际定时器设置为11
然而,当比赛落后时,计时器跑得太快了,毁了我的比赛
那么,我如何实现这样一种方式,即即使设备延迟,计时器仍能正常工作?看起来计时器不是通过计时事件来完成的,而是通过更新功能来操作的。这意味着它受帧速率的影响。我建议你这样做:
game.time.events.add(/*时间单位为ms*/,在计时器结束后调用/*func*/,this)代码>确保计时器不依赖于帧速率。我不确定您何时/何地调用updateCounter,但如果您希望它以特定频率被调用,您将遇到麻烦。在现代web浏览器中,您可以访问window.performance.now()
,这是一个非常精确的毫秒时钟,它不依赖于帧速率。我想你也可以在phonegap中使用它,但我不确定。