diff --git a/examples/wip/group lifespan.js b/examples/wip/group lifespan.js new file mode 100644 index 00000000..0691f4f8 --- /dev/null +++ b/examples/wip/group lifespan.js @@ -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); +} diff --git a/examples/wip/group1.js b/examples/wip/group1.js new file mode 100644 index 00000000..90ee89af --- /dev/null +++ b/examples/wip/group1.js @@ -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); +} diff --git a/src/core/Group.js b/src/core/Group.js index ae8d7fd6..b4f4dca7 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -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. diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 6354af25..c0427e0d 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -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; + } } diff --git a/src/physics/Body.js b/src/physics/Body.js index f19542ff..8ab9b97b 100644 --- a/src/physics/Body.js +++ b/src/physics/Body.js @@ -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; */ },