javascript - 如何在 Phaser JS 中实现一个好的定时器

标签 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

但是,当游戏滞后时,计时器的速度会快得令人难以置信,从而毁掉了我的游戏 那么我该如何实现,即使设备滞后,计时器仍能正常运行?

最佳答案

看起来你的计时器不是由计时事件完成的,而是由更新函数操纵的。这意味着它受到帧速率的影响。我建议做这样的事情:

game.time.events.add(/*以毫秒为单位的时间*/,/*定时器结束后调用的函数*/, this);

关于javascript - 如何在 Phaser JS 中实现一个好的定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27098323/

相关文章:

javascript - 用于匹配军事(24 小时)格式时间的正则表达式

javascript - 如何使用 javascript 和 html 显示特定时区的内容

java - Xamarin Studio 中的 Android - 错误 : package com. google.android.gms.tagmanager 不存在

sqlite - 无法从JavaScript打开sqlite数据库

android - 从 CordovaPlugin 打开一个 Activity

cordova - 如何通过 Wi-Fi 在设备之间同步 PouchDB?

javascript - 这个主要的测试功能是如何工作的?

javascript - C3折线图: compress or break interval between distant points

android - 准备 React Native 发布

android - 如何从Android应用程序的浏览器中读取内容?