phaser/examples/wip/timer2.js
photonstorm 86374d4437 Phaser.Timer will no longer resume if it was previously paused and the game loses focus and then resumes (fixes #383)
Phaser.Timer.stop has a new parameter: clearEvents (default true), if true all the events in Timer will be cleared, otherwise they will remain (fixes #383)
2014-02-26 20:12:17 +00:00

42 lines
No EOL
926 B
JavaScript

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create});
var counter = 0
var explicitlyPaused = false
function create() {
game.stage.backgroundColor = '#6688ee';
timer = game.time.create(false);
timer.repeat(Phaser.Timer.SECOND, 10, step, this);
game.onPause.add(handleGamePaused)
game.onResume.add(handleGameResumed)
//prevent init tick error
setTimeout(function(){
timer.start()
}, 250)
}
function handleGamePaused(){
console.log('game paused')
}
function handleGameResumed(){
console.log('game resumed')
}
function step(){
counter++
console.log('timer step', counter)
if(counter == 3){
pauseTimer()
}
if(counter > 3){
console.log('this should never happen! Paused since step #3')
}
}
function pauseTimer(){
if(explicitlyPaused) return
console.log('pause timer triggered')
explicitlyPaused = true
timer.pause()
}