Groups now update their children across preUpdate, update and postUpdate.

Sprite.exists = false removes Body from world.
This commit is contained in:
photonstorm
2014-02-13 12:26:39 +00:00
parent 312ec462bc
commit bdb8908fee
5 changed files with 187 additions and 16 deletions
+59
View File
@@ -0,0 +1,59 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
}
var group1;
var group2;
var cursors;
function create() {
group1 = game.add.group();
group2 = game.add.group();
// Now let's create some random sprites and enable them all for drag and 'bring to top'
var t;
for (var i = 0; i < 10; i++)
{
t = group1.create(game.world.randomX, game.world.randomY, 'atari1');
t.lifespan = 3000 + (Math.random() * 3000);
group2.create(game.world.randomX, game.world.randomY, 'sonic');
}
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
if (cursors.left.isDown)
{
group1.x -= 4;
}
else if (cursors.right.isDown)
{
group1.x += 4;
}
if (cursors.up.isDown)
{
group2.y -= 4;
}
else if (cursors.down.isDown)
{
group2.y += 4;
}
}
function render() {
// game.debug.renderInputInfo(32, 32);
}
+56
View File
@@ -0,0 +1,56 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari1', 'assets/sprites/atari130xe.png');
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
}
var group1;
var group2;
var cursors;
function create() {
group1 = game.add.group();
group2 = game.add.group();
// Now let's create some random sprites and enable them all for drag and 'bring to top'
for (var i = 0; i < 10; i++)
{
group1.create(game.world.randomX, game.world.randomY, 'atari1');
group2.create(game.world.randomX, game.world.randomY, 'sonic');
}
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
if (cursors.left.isDown)
{
group1.x -= 4;
}
else if (cursors.right.isDown)
{
group1.x += 4;
}
if (cursors.up.isDown)
{
group2.y -= 4;
}
else if (cursors.down.isDown)
{
group2.y += 4;
}
}
function render() {
// game.debug.renderInputInfo(32, 32);
}
+52 -4
View File
@@ -33,7 +33,7 @@ Phaser.Group = function (game, parent, name, addToStage) {
PIXI.DisplayObjectContainer.call(this);
if (typeof addToStage === 'undefined')
if (typeof addToStage === 'undefined' || addToStage === false)
{
if (parent)
{
@@ -70,7 +70,6 @@ Phaser.Group = function (game, parent, name, addToStage) {
/**
* @property {Phaser.Group|Phaser.Sprite} parent - The parent of this Group.
*/
// this.group = null;
/**
* @property {Phaser.Point} scale - The scale of the Group container.
@@ -125,8 +124,6 @@ Phaser.Group.SORT_ASCENDING = -1;
*/
Phaser.Group.SORT_DESCENDING = 1;
// PIXI.DisplayObjectContainer.prototype.addChildAt = function(child, index)
/**
* Adds an existing object to this Group. The object can be an instance of Phaser.Sprite, Phaser.Button or any other display object.
* The child is automatically added to the top of the Group, so renders on-top of everything else within the Group. If you need to control
@@ -704,6 +701,57 @@ Phaser.Group.prototype.callAll = function (method, context) {
}
/**
* The core preUpdate - as called by World.
* @method Phaser.Group#preUpdate
* @protected
*/
Phaser.Group.prototype.preUpdate = function () {
for (var i = 0, len = this.children.length; i < len; i++)
{
if (this.children[i]['preUpdate'])
{
this.children[i].preUpdate();
}
}
}
/**
* The core update - as called by World.
* @method Phaser.Group#update
* @protected
*/
Phaser.Group.prototype.update = function () {
for (var i = 0, len = this.children.length; i < len; i++)
{
if (this.children[i]['update'])
{
this.children[i].update();
}
}
}
/**
* The core postUpdate - as called by World.
* @method Phaser.Group#postUpdate
* @protected
*/
Phaser.Group.prototype.postUpdate = function () {
for (var i = 0, len = this.children.length; i < len; i++)
{
if (this.children[i]['postUpdate'])
{
this.children[i].postUpdate();
}
}
}
/**
* Allows you to call your own function on each member of this Group. You must pass the callback and context in which it will run.
* After the checkExists parameter you can add as many parameters as you like, which will all be passed to the callback along with the child.
+11 -2
View File
@@ -574,7 +574,7 @@ Phaser.Sprite.prototype.reset = function(x, y, health) {
if (this.body)
{
this.body.reset(false);
this.body.reset(x, y, false, false);
}
return this;
@@ -861,8 +861,12 @@ Object.defineProperty(Phaser.Sprite.prototype, "physicsEnabled", {
});
/**
* Sprite.exists controls if the core game loop and physics update this Sprite or not.
* When you set Sprite.exists to false it will remove its Body from the physics world (if it has one) and also set Sprite.visible to false.
* Setting Sprite.exists to true will re-add the Body to the physics world (if it has a body) and set Sprite.visible to true.
*
* @name Phaser.Sprite#exists
* @property {boolean} exists - Sprite.exists controls if the core game loop and physics update this Sprite or not. Set to false to remove from the update and physics world.
* @property {boolean} exists - If the Sprite is processed by the core game update and physics.
*/
Object.defineProperty(Phaser.Sprite.prototype, "exists", {
@@ -884,6 +888,8 @@ Object.defineProperty(Phaser.Sprite.prototype, "exists", {
{
this.body.addToWorld();
}
this.visible = true;
}
else
{
@@ -894,6 +900,9 @@ Object.defineProperty(Phaser.Sprite.prototype, "exists", {
{
this.body.removeFromWorld();
}
this.visible = false;
}
}
+9 -10
View File
@@ -285,10 +285,12 @@ Phaser.Physics.Body.prototype = {
* Resets the Body force, velocity (linear and angular) and rotation. Optionally resets damping and mass.
*
* @method Phaser.Physics.Body#reset
* @param {number} x - The new x position of the Body.
* @param {number} y - The new x position of the Body.
* @param {boolean} [resetDamping=false] - Resets the linear and angular damping.
* @param {boolean} [resetMass=false] - Sets the Body mass back to 1.
*/
reset: function (resetDamping, resetMass) {
reset: function (x, y, resetDamping, resetMass) {
if (typeof resetDamping === 'undefined') { resetDamping = false; }
if (typeof resetMass === 'undefined') { resetMass = false; }
@@ -307,6 +309,9 @@ Phaser.Physics.Body.prototype = {
this.mass = 1;
}
this.x = x;
this.y = y;
},
/**
@@ -344,21 +349,15 @@ Phaser.Physics.Body.prototype = {
*/
destroy: function () {
this.sprite = null;
if (this.data.world === this.game.physics)
{
this.game.physics.removeBody(this.data);
}
this.removeFromWorld();
this.clearShapes();
this.sprite = null;
/*
this.collideCallback = null;
this.collideCallbackContext = null;
this.customSeparateCallback = null;
this.customSeparateContext = null;
*/
},