简体   繁体   中英

How to implement a good timer in Phaser JS

Hi I'm currently working on a timer for my app that I will port to android phones using phonegap its about a ball game with 10 second timer

Currently im using timer++ increments

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);
    }

im using timer to add to total timer and timer2 to set the actual timer to 11 before the game ends

however when the game lags the timer go so incredible fast ruining my game so how do i implement in in such a way that even when the device lags the timer is still functioning properly?

It looks like your timer is done not by timing events, but rather manipulated by the update function. This means that it is affected by the frame rate. I would suggest doing something like this:

game.time.events.add(/*time in ms*/, /*func to be called after timer ends*/, this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM