Jump to content

End the game after 5 seconds of inactivity


Recommended Posts


I'm new to this forum and Phaser.io so I do apologize if this post isn't well.


How to end the game after 5 seconds of inactivity ? I've made something but I think it is really really bad for performance.

Each time the update function is called I check if the user didn't pushed "up, left, right, down" and after I check if we have exceed the time by doing (currentTime - beginningTime > 5000).

This code has 2 problems I'm trying to fire : 

- The performance is really bad because it is not necessary too check every time we call the update function

- In my condition I want to check that "no key has been pushed" and for now I just check if the user didn't pushed "up", "left", "right" or "down"


How to do that ?

Sorry for my english

var timeBeginning = new Date().getTime();function update() {    // input to move the ship    if (cursors.up.isDown) {        game.physics.arcade.accelerationFromRotation(ship.rotation, 200, ship.body.acceleration);    } else {        // stopper the acceleration         ship.body.acceleration.set(0);    }    if (cursors.left.isDown) {        ship.body.angularVelocity = -300;    } else if (cursors.right.isDown) {        ship.body.angularVelocity = 300;    } else {        // stop the rotation        ship.body.angularVelocity = 0;    }    if (!cursors.up.isDown && !cursors.left.isDown && !cursors.right.isDown && !cursors.down.isDown) {        if (new Date().getTime() - timeBeginning > 5000) {            end();        }    } else {        timeBeginning = new Date().getTime();    }}
Link to comment
Share on other sites

Yes, it's a good start, but how I check before that nothing has been pushed since 5000ms for example ? 

I would like to the same as here but using good fonctions from the phaser framework

if (!this.cursors.up.isDown && !this.cursors.left.isDown && !this.cursors.right.isDown && !this.cursors.down.isDown) {    if (new Date().getTime() - this.timeBeginning > 5000) {         this.end();    }} else {    this.timeBeginning = new Date().getTime();}
Link to comment
Share on other sites

You can set a variable as a flag to indicate if something has been pressed

var hasActionOccured = false;// on update check if anything is pressedif(this.cursors.up.isDown || this.cursors.left.isDown || this.cursors.right.isDown || this.cursors.down.inDown)hasActionOccured = true;

Then on your timer check the variable 

function createTimer(){var myTimer = game.time.events.add(5000, function () {        if(hasActionOccured){           removeTimer();           createTimer();           hasActionOccured = false; // resetting the variable        }        else {           gameOver();        }}, this, []);}function removeTimer() {game.time.events.remove(myTimer);}

If the variable is true it resets the timer, if not its game-over

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...