Clemzd Posted December 21, 2014 Share Posted December 21, 2014 Hi,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 englishvar 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 More sharing options...
MichaelD Posted December 21, 2014 Share Posted December 21, 2014 You can add a timerfunction createTimer(){var myTimer = game.time.events.add(5000, function () { gameOver();}, this, []);}And then when user presses something, remove it and re-add itfunction removeTimer() { game.time.events.remove(myTimer);} Link to comment Share on other sites More sharing options...
Clemzd Posted December 21, 2014 Author Share Posted December 21, 2014 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 frameworkif (!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 More sharing options...
MichaelD Posted December 22, 2014 Share Posted December 22, 2014 You can set a variable as a flag to indicate if something has been pressedvar 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 More sharing options...
WitheringTreant Posted December 22, 2014 Share Posted December 22, 2014 How about something like var inactivity_time = 5000; when player control button is pressed:inactivity_time = this.game.time.now + 5000; In the update loop, you check for:if (inactivity_time < this.game.time.now) {end game} lewster32 1 Link to comment Share on other sites More sharing options...
lewster32 Posted December 22, 2014 Share Posted December 22, 2014 WitheringTreant's method will probably be the best way to do this, though if using a recent version of Phaser make sure you use game.time.time as game.time.now will not always be reliable. Link to comment Share on other sites More sharing options...
Recommended Posts