如何在Phaser JS中实现一个好的计时器

时间:2014-11-24 05:22:16

标签: javascript android cordova timer phaser-framework

您好我正在为我的应用程序设计一个计时器,我将使用phonegap将其移植到Android手机上,这是关于10秒计时器的球赛

目前我正在使用timer ++增量

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

然而,当游戏落后于计时器时,会如此令人难以置信地快速破坏我的游戏 那么我如何以这样的方式实现,即使设备滞后计时器仍然正常运行?

1 个答案:

答案 0 :(得分:0)

看起来您的计时器不是通过计时事件来完成的,而是通过更新功能来操作的。这意味着它受帧速率的影响。我建议做这样的事情:

manager.AddToRole(user.Id, "Staff");