cmoney0927 Posted June 9, 2014 Share Posted June 9, 2014 Basically I'm making a basic racing game where I have the timer start at the beginning of the game, but I can't get it to stop when the racecar collides with the finish line. Any help would be appreciated. Link to comment Share on other sites More sharing options...
lewster32 Posted June 9, 2014 Share Posted June 9, 2014 Better instead of a timer to make note of the time difference between when the race started and when it ended, as you can then guarantee your time to be accurate in the case of missed frames etc; something like so:// Create the visual representation of the timevar timerText = game.add.text(10, 10, '0', { font: "32px Arial", fill: "#ffffff" });// Initialise two variables to hold start and end timevar raceStartTime = 0;var raceEndTime = 0;// Call this function when the race beginsfunction raceStart() { raceStartTime = game.time.now;}// Call this function when the race endsfunction raceEnd() { raceEndTime = game.time.now;}function update() { // Update the current race time only if the race has started but not ended if (raceStartTime > 0 && raceEndTime === 0) { // Round the value and divide by 1000 to get whole seconds, and ensure it's a string timerText.text = (Math.round((game.time.now - raceStartTime) / 1000)).toString(10); }}If you want to have more accuracy on the numbers (as you probably do in a racing game) you can change the rounding calculation to this:timerText.text = Phaser.Math.roundTo((game.time.now - raceStartTime) / 1000), 2);Which will use Phaser.Math.roundTo to round the value to two decimal places. Link to comment Share on other sites More sharing options...
lewster32 Posted June 9, 2014 Share Posted June 9, 2014 Of course if you do insist on using a timer, simply calling timer.stop() should do the trick! Link to comment Share on other sites More sharing options...
Recommended Posts