From 4ed20e0f776fe9db10463a9a6fab10198571b57b Mon Sep 17 00:00:00 2001 From: photonstorm Date: Thu, 6 Feb 2014 13:15:45 +0000 Subject: [PATCH] Removed all intances of Sprite.group from Group and replaced with the already existing parent property. --- README.md | 4 ++ examples/wip/pixi1.js | 8 ++-- src/core/Group.js | 66 ++++++++----------------------- src/gameobjects/Sprite.js | 21 +++++----- src/pixi/display/DisplayObject.js | 3 ++ 5 files changed, 40 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index a4cc4d201..f4453f920 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ Significant API changes: * Upgraded to Pixi.js 1.4.4 * Group now extends PIXI.DisplayObjectContainer, rather than owning a _container property, which makes life a whole lot easier re: nesting. +* Removed Sprite.group property. You can use Sprite.parent for all similar needs now. +* PIXI.Point is now aliased to Phaser.Point - saves on code duplication and works exactly the same. +* PIXI.Rectangle is now aliased to Phaser.Rectangle - saves on code duplication and works exactly the same. +* PIXI.Circle is now aliased to Phaser.Circle - saves on code duplication and works exactly the same. New features: diff --git a/examples/wip/pixi1.js b/examples/wip/pixi1.js index bf8abf8ef..210adfc12 100644 --- a/examples/wip/pixi1.js +++ b/examples/wip/pixi1.js @@ -1,5 +1,5 @@ -var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render }); +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); function preload() { @@ -16,9 +16,9 @@ function create() { sprite = game.add.sprite(0, 0, 'pic'); - g = game.add.group(); + g = game.add.group(null, 'billy'); - g.create(0, 0, 'pic'); + sprite2 = g.create(0, 0, 'pic'); g.y = 200; g.rotation = 0.1; @@ -47,4 +47,6 @@ function update() { function render() { + game.debug.renderText(sprite.position.y, 32, 32); + } diff --git a/src/core/Group.js b/src/core/Group.js index 17a2b4607..d135488c7 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -68,9 +68,9 @@ Phaser.Group = function (game, parent, name, useStage) { this.exists = true; /** - * @property {Phaser.Group} group - The parent Group of this Group, if a child of another. + * @property {Phaser.Group|Phaser.Sprite} parent - The parent of this Group. */ - this.group = null; + // this.group = null; /** * @property {Phaser.Point} scale - The scale of the Group container. @@ -140,10 +140,8 @@ Phaser.Group.SORT_DESCENDING = 1; */ Phaser.Group.prototype.add = function (child) { - if (child.group !== this) + if (child.parent !== this) { - child.group = this; - this.addChild(child); if (child.events) @@ -172,10 +170,8 @@ Phaser.Group.prototype.add = function (child) { */ Phaser.Group.prototype.addAt = function (child, index) { - if (child.group !== this) + if (child.parent !== this) { - child.group = this; - this.addChildAt(child, index); if (child.events) @@ -224,7 +220,6 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists) { var child = new Phaser.Sprite(this.game, x, y, key, frame); - child.group = this; child.exists = exists; child.visible = exists; child.alive = exists; @@ -262,25 +257,7 @@ Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists) for (var i = 0; i < quantity; i++) { - var child = new Phaser.Sprite(this.game, 0, 0, key, frame); - - child.group = this; - child.exists = exists; - child.visible = exists; - child.alive = exists; - - this.addChild(child); - - if (child.events) - { - child.events.onAddedToGroup.dispatch(child, this); - } - - if (this.cursor === null) - { - this.cursor = child; - } - + this.create(0, 0, key, frame, exists); } } @@ -343,12 +320,7 @@ Phaser.Group.prototype.previous = function () { */ Phaser.Group.prototype.swap = function (child1, child2) { - if (child1 === child2 || !child1.parent || !child2.parent || child1.group !== this || child2.group !== this) - { - return false; - } - - this.swapChildren(child1, child2); + return this.swapChildren(child1, child2); } @@ -361,7 +333,7 @@ Phaser.Group.prototype.swap = function (child1, child2) { */ Phaser.Group.prototype.bringToTop = function (child) { - if (child.group === this) + if (child.parent === this) { this.remove(child); this.add(child); @@ -473,8 +445,6 @@ Phaser.Group.prototype.setProperty = function (child, key, value, operation) { else if (operation == 3) { child[key[0]][key[1]][key[2]][key[3]] *= value; } else if (operation == 4) { child[key[0]][key[1]][key[2]][key[3]] /= value; } } - - // TODO - Deep property scane } @@ -996,9 +966,9 @@ Phaser.Group.prototype.getRandom = function (startIndex, length) { */ Phaser.Group.prototype.remove = function (child) { - if (child.group !== this) + if (this.children.length === 0) { - return false; + return; } if (child.events) @@ -1006,19 +976,13 @@ Phaser.Group.prototype.remove = function (child) { child.events.onRemovedFromGroup.dispatch(child, this); } - // Check it's actually in the container - if (child.parent === this) - { - this.removeChild(child); - } + this.removeChild(child); if (this.cursor === child) { this.next(); } - child.group = null; - return true; } @@ -1042,6 +1006,7 @@ Phaser.Group.prototype.removeAll = function () { { this.children[0].events.onRemovedFromGroup.dispatch(this.children[0], this); } + this.removeChild(this.children[0]); } while (this.children.length > 0); @@ -1071,9 +1036,12 @@ Phaser.Group.prototype.removeBetween = function (startIndex, endIndex) { for (var i = startIndex; i < endIndex; i++) { - var child = this.children[i]; - child.events.onRemovedFromGroup.dispatch(child, this); - this.removeChild(child); + if (this.children[i].events) + { + this.children[i].events.onRemovedFromGroup.dispatch(this.children[i], this); + } + + this.removeChild(this.children[i]); if (this.cursor === child) { diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 1e7ea7ae3..27f27c83d 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -45,9 +45,9 @@ Phaser.Sprite = function (game, x, y, key, frame) { this.alive = true; /** - * @property {Phaser.Group} group - The parent Group of this Sprite. This is usually set after Sprite instantiation by the parent. + * @property {Phaser.Group|Phaser.Sprite} parent - The parent of this Sprite. */ - this.group = null; + // this.group = null; /** * @property {string} name - The user defined name given to this Sprite. @@ -169,7 +169,10 @@ Phaser.Sprite = function (game, x, y, key, frame) { * * @property {Phaser.Point} anchor - The anchor around which rotation and scaling takes place. */ - this.anchor = new Phaser.Point(); + // this.anchor = new Phaser.Point(); + + // this.position.x = x; + // this.position.y = y; /** * @property {number} x - The x coordinate in world space of this Sprite. @@ -181,8 +184,6 @@ Phaser.Sprite = function (game, x, y, key, frame) { */ this.y = y; - this.position.x = x; - this.position.y = y; /** * @property {Phaser.Point} world - The world coordinates of this Sprite. This differs from the x/y coordinates which are relative to the Sprites container. @@ -201,7 +202,7 @@ Phaser.Sprite = function (game, x, y, key, frame) { /** * @property {Phaser.Point} scale - The scale of the Sprite when rendered. By default it's set to 1 (no scale). You can modify it via scale.x or scale.y or scale.setTo(x, y). A value of 1 means no change to the scale, 0.5 means "half the size", 2 means "twice the size", etc. */ - this.scale = new Phaser.Point(1, 1); + // this.scale = new Phaser.Point(1, 1); /** * @property {object} _cache - A mini cache for storing all of the calculated values. @@ -381,7 +382,7 @@ Phaser.Sprite = function (game, x, y, key, frame) { this.updateBounds(); /** - * @property {PIXI.Point} pivot - The pivot point of the displayObject that it rotates around. + * @property {Phaser.Point} pivot - The pivot point of the displayObject that it rotates around. */ }; @@ -400,7 +401,7 @@ Phaser.Sprite.prototype.preUpdate = function() { if (this._cache.fresh) { - this.world.setTo(this.parent.position.x + this.x, this.parent.position.y + this.y); + this.world.setTo(this.parent.position.x + this.position.x, this.parent.position.y + this.position.y); this.worldTransform[2] = this.world.x; this.worldTransform[5] = this.world.y; this._cache.fresh = false; @@ -871,9 +872,9 @@ Phaser.Sprite.prototype.destroy = function() { this.filters = null; } - if (this.group) + if (this.parent) { - this.group.remove(this); + this.parent.remove(this); } if (this.input) diff --git a/src/pixi/display/DisplayObject.js b/src/pixi/display/DisplayObject.js index 09f33cd92..c1c82429c 100644 --- a/src/pixi/display/DisplayObject.js +++ b/src/pixi/display/DisplayObject.js @@ -196,6 +196,9 @@ PIXI.DisplayObject = function() */ this._mask = null; + this.x = this.position.x; + this.y = this.position.y; + /* * MOUSE Callbacks */