Skip to content

Commit

Permalink
Added in Game.focusLoss and focusGain methods and onBlur and onFocus …
Browse files Browse the repository at this point in the history
…signals to listen for. Made small time related fix and tested on iOS6 and iOS7 home screen button + lock button and all working fine.
  • Loading branch information
photonstorm committed Mar 12, 2014
1 parent 84741f0 commit 1966bfc
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 66 deletions.
72 changes: 44 additions & 28 deletions examples/p2 physics/impact events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,95 @@ var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: p
function preload() {

game.load.image('stars', 'assets/misc/starfield.jpg');
game.load.image('ship', 'assets/sprites/thrust_ship2.png');
game.load.spritesheet('ship', 'assets/sprites/humstar.png', 32, 32);
game.load.image('ball', 'assets/sprites/shinyball.png');
game.load.spritesheet('diamonds', 'assets/sprites/diamonds32x24x5.png', 32, 24);

}

var ship;
var starfield;
var diamonds;
var cursors;

function create() {

game.world.setBounds(0, 0, 1920, 1200);
game.world.setBounds(0, 0, 1600, 1200);

game.physics.startSystem(Phaser.Physics.P2JS);
game.physics.p2.defaultRestitution = 0.8;
game.physics.p2.defaultRestitution = 0.9;

// starfield = game.add.tileSprite(0, 0, 800, 600, 'stars');
// starfield.fixedToCamera = true;
starfield = game.add.tileSprite(0, 0, 800, 600, 'stars');
starfield.fixedToCamera = true;

diamonds = game.add.group();
// diamonds.enableBody = true;
// diamonds.physicsBodyType = Phaser.Physics.P2JS;
balls = game.add.group();
balls.enableBody = true;
balls.physicsBodyType = Phaser.Physics.P2JS;

for (var i = 0; i < 50; i++)
{
// var d = diamonds.create(game.world.randomX, game.world.randomY, 'diamonds', game.rnd.integerInRange(0, 5));
// game.physics.p2.enable(d, true);

var d = diamonds.create(game.world.randomX, game.world.randomY, 'ball');
game.physics.p2.enable(d);
d.body.setCircle(16);
var ball = balls.create(game.world.randomX, game.world.randomY, 'ball');
ball.body.setCircle(16);
}

ship = game.add.sprite(200, 200, 'ship');
ship = game.add.sprite(200, 200, 'ship');
ship.scale.set(2);
ship.smoothed = false;
ship.animations.add('fly', [0,1,2,3,4,5], 10, true);
ship.play('fly');

// Create our physics body - a 28px radius circle. Set the 'false' parameter below to 'true' to enable debugging
game.physics.p2.enable(ship, false);
ship.body.setCircle(28);

game.physics.p2.enable(ship, true);
game.camera.follow(ship);

game.camera.follow(ship);
ship.body.onBeginContact.add(hitBall, this);

cursors = game.input.keyboard.createCursorKeys();

}

function hitBall(body) {

// body.sprite.kill();

}

function update() {

ship.body.setZeroVelocity();

if (cursors.left.isDown)
{
ship.body.rotateLeft(100);
ship.body.moveLeft(200);
}
else if (cursors.right.isDown)
{
ship.body.rotateRight(100);
}
else
{
ship.body.setZeroRotation();
ship.body.moveRight(200);
}

if (cursors.up.isDown)
{
ship.body.thrust(400);
ship.body.moveUp(200);
}
else if (cursors.down.isDown)
{
ship.body.reverse(400);
ship.body.moveDown(200);
}

// starfield.tilePosition.add(ship.body.velocity.x / 4, ship.body.velocity.y / 4);
if (!game.camera.atLimit.x)
{
starfield.tilePosition.x += (ship.body.velocity.x * 16) * game.time.physicsElapsed;
}

if (!game.camera.atLimit.y)
{
starfield.tilePosition.y += (ship.body.velocity.y * 16) * game.time.physicsElapsed;
}

}

function render() {

game.debug.text('World bodies: ' + game.physics.p2.total, 32, 32);

}
10 changes: 9 additions & 1 deletion examples/p2 physics/thrust.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ function update() {
ship.body.reverse(400);
}

starfield.tilePosition.add(ship.body.velocity.x, ship.body.velocity.y);
if (!game.camera.atLimit.x)
{
starfield.tilePosition.x += (ship.body.velocity.x * 16) * game.time.physicsElapsed;
}

if (!game.camera.atLimit.y)
{
starfield.tilePosition.y += (ship.body.velocity.y * 16) * game.time.physicsElapsed;
}

}

Expand Down
2 changes: 2 additions & 0 deletions examples/wip/misc/pausetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create:

function create() {

console.log('game started at ' + this.time.now);

game.onPause.add(onGamePause, this);
game.onResume.add(onGameResume, this);

Expand Down
57 changes: 57 additions & 0 deletions examples/wip/pausetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

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

var log = '';
var t;

function create() {

log += 'game started at ' + this.time.now + '\n';
// log += 'pagehide? ' + window['onpagehide'] + '\n';
// log += 'blur? ' + window['blur'] + '\n';

console.log('game started at ' + this.time.now);

game.stage.backgroundColor = '#2d2d2d';

game.onPause.add(onGamePause, this);
game.onResume.add(onGameResume, this);

t = game.add.text(32, 32, log, { font: '14px Arial', fill: '#ffffff' });

// window.unload = function() { console.log('unload'); };

}

function onGamePause(event) {

log += 'game paused at ' + this.time.now + '\n';
log += 'event ' + event.type + '\n';

console.log('game paused at ' + this.time.now);

}

function onGameResume(event) {

log += 'game un-paused at ' + this.time.now + '\n';
log += 'was paused for ' + game.time.pauseDuration + '\n';
log += 'event ' + event.type + '\n';

console.log('game un-paused at ' + this.time.now);
console.log('was paused for ' + game.time.pauseDuration);

}

function render() {

// console.log('tick');

t.text = log;

game.debug.text('now: ' + game.time.now, 500, 32);

// game.debug.text(log, 32, 64);
// game.debug.text('paused: ' + game.paused, 32, 64);

}
70 changes: 62 additions & 8 deletions src/core/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
*/
this.stepCount = 0;

/**
* @property {Phaser.Signal} onPause - This event is fired when the game pauses.
*/
this.onPause = null;

/**
* @property {Phaser.Signal} onResume - This event is fired when the game resumes from a paused state.
*/
this.onResume = null;

/**
* @property {Phaser.Signal} onBlur - This event is fired when the game no longer has focus (typically on page hide).
*/
this.onBlur = null;

/**
* @property {Phaser.Signal} onFocus - This event is fired when the game has focus (typically on page show).
*/
this.onFocus = null;

/**
* @property {boolean} _paused - Is game paused?
* @private
Expand Down Expand Up @@ -398,6 +418,8 @@ Phaser.Game.prototype = {

this.onPause = new Phaser.Signal();
this.onResume = new Phaser.Signal();
this.onBlur = new Phaser.Signal();
this.onFocus = new Phaser.Signal();

this.isBooted = true;

Expand Down Expand Up @@ -573,15 +595,15 @@ Phaser.Game.prototype = {
*/
update: function (time) {

this.time.update(time);

if (!this._paused && !this.pendingStep)
{
if (this.stepping)
{
this.pendingStep = true;
}

this.time.update(time);

this.debug.preUpdate();
this.state.preUpdate();
this.plugins.preUpdate();
Expand Down Expand Up @@ -682,17 +704,18 @@ Phaser.Game.prototype = {
* Called by the Stage visibility handler.
*
* @method Phaser.Game#gamePaused
* @param {object} event - The DOM event that caused the game to pause, if any.
* @protected
*/
gamePaused: function (time) {
gamePaused: function (event) {

// If the game is already paused it was done via game code, so don't re-pause it
if (!this._paused)
{
this._paused = true;
this.time.gamePaused(time);
this.time.gamePaused();
this.sound.setMute();
this.onPause.dispatch(this);
this.onPause.dispatch(event);
}

},
Expand All @@ -701,20 +724,51 @@ Phaser.Game.prototype = {
* Called by the Stage visibility handler.
*
* @method Phaser.Game#gameResumed
* @param {object} event - The DOM event that caused the game to pause, if any.
* @protected
*/
gameResumed: function (time) {
gameResumed: function (event) {

// Game is paused, but wasn't paused via code, so resume it
if (this._paused && !this._codePaused)
{
this._paused = false;
this.time.gameResumed(time);
this.time.gameResumed();
this.input.reset();
this.sound.unsetMute();
this.onResume.dispatch(this);
this.onResume.dispatch(event);
}

},

/**
* Called by the Stage visibility handler.
*
* @method Phaser.Game#focusLoss
* @param {object} event - The DOM event that caused the game to pause, if any.
* @protected
*/
focusLoss: function (event) {

this.onBlur.dispatch(event);

this.gamePaused(event);

},

/**
* Called by the Stage visibility handler.
*
* @method Phaser.Game#focusGain
* @param {object} event - The DOM event that caused the game to pause, if any.
* @protected
*/
focusGain: function (event) {

this.onFocus.dispatch(event);

this.gameResumed(event);

}

};
Expand Down
3 changes: 0 additions & 3 deletions src/core/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,14 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
if (this.physicsBodyType === Phaser.Physics.ARCADE)
{
this.game.physics.arcade.enable(child);
// child.body = new Phaser.Physics.Arcade.Body(child);
}
else if (this.physicsBodyType === Phaser.Physics.NINJA && this.game.physics.ninja)
{
this.game.physics.ninja.enable(child);
// child.body = new Phaser.Physics.Ninja.Body(this.game.physics.ninja, child, 1);
}
else if (this.physicsBodyType === Phaser.Physics.P2JS && this.game.physics.p2)
{
this.game.physics.p2.enable(child);
// child.body = new Phaser.Physics.P2.Body(this.game, child, x, y, 1);
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/core/Stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,8 @@ Phaser.Stage.prototype.checkVisibility = function () {
document.addEventListener(this._hiddenVar, this._onChange, false);
}

if (window['onpagehide'])
{
window.onpagehide = this._onChange;
window.onpageshow = this._onChange;
}
window.onpagehide = this._onChange;
window.onpageshow = this._onChange;

window.onblur = this._onChange;
window.onfocus = this._onChange;
Expand All @@ -315,23 +312,23 @@ Phaser.Stage.prototype.visibilityChange = function (event) {
{
if (event.type === 'pagehide' || event.type === 'blur')
{
this.game.gamePaused(event.timeStamp);
this.game.focusLoss(event);
}
else if (event.type === 'pageshow' || event.type === 'focus')
{
this.game.gameResumed(event.timeStamp);
this.game.focusGain(event);
}

return;
}

if (document.hidden || document.mozHidden || document.msHidden || document.webkitHidden)
{
this.game.gamePaused(event.timeStamp);
this.game.gamePaused(event);
}
else
{
this.game.gameResumed(event.timeStamp);
this.game.gameResumed(event);
}

}
Expand Down
Loading

0 comments on commit 1966bfc

Please sign in to comment.