Sign in to follow this  
BdR

Countdown seconds, how to stop reset restart timer?

Recommended Posts

I'm working on a game where the player must complete a level within 30 seconds. If the player succeeds the timer is stopped, a message displays, the next level starts and the timer must be reset to 30 seconds. This all happens within the same Phaser.state. The time display only needs to be updated each second, not in between, so I looked at this example and use a time-object and initialise it with .loop() and then .start() it. When the time runs out or when player wins the timer is stopped with .stop().

 

The problem is that timer.Start() doesn't seem to work after the timer.Stop() was called.

 

If looked at .pause() and resume() but I think(?) then it can potentially continue mid-second in the next level (time is very important in this game) not sure though.. Here's what I've got so far, I've isolated the code for the clock update in an example test program, see code below.




var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });

var clocktext; // bitmaptext
var clocktimer; // Phaser.timer
var clockseconds; // integer

// -------------------------------------
// PHASER GAME FUNCTIONS
// -------------------------------------
function preload() {
    game.load.bitmapFont('myfont', 'myfont.png', 'myfont.xml');
};

function create() {
    // add texts
   clocktext = game.add.bitmapText(160, 160, 'myfont', '---', 40);

   game.add.bitmapText(160, 160+80, 'myfont', 'Press R to reset timer', 40);
   game.add.bitmapText(160, 160+120, 'myfont', 'Press S to stop timer', 40);

   // timer object, note timer won't start running yet
   clocktimer = game.time.create(false);
   clocktimer.loop(Phaser.Timer.SECOND, updateDisplay, this);

   // handle keyboard keys R and S
   game.input.keyboard.onDownCallback = HandleKeyDown;
   clockseconds = 0;
}

function HandleKeyDown(e) {
   if (e.keyCode == 82) { initClock() }; // R = reset/init
   if (e.keyCode == 83) { stopClock() }; // S = stop/pause
}

function stopClock() {
   clocktimer.stop();
   clocktext.text = "Stop at " + clockseconds + " seconds left";
   console.log('stopClock - timer stopped');
}

function initClock() {
   clockseconds = 5+1; // set countdown seconds, +1 because initial display will also decrease with 1
   updateDisplay(); // initial display
   clocktimer.start();
}

function updateDisplay() {
   // count down seconds
   clockseconds = clockseconds - 1;
   console.log('updateClock - seconds left: '+clockseconds);

   // check if time is up
   if (clockseconds <= 0) {
      // ohnoes!
      stopClock();
      console.log('updateClock - time is up');
      clocktext.text = 'time is up!';
   } else {
      // update display
      var minutes = Math.floor(clockseconds / 60);
      var seconds = (clockseconds - minutes * 60);
      clocktext.text = "time " + (("0"+minutes).substr(-2) + ":" + ("0"+seconds).substr(-2));
   };
}


btw there's also this thread but that only explains how to start a timer, not how to stop and restart it.


Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.