From 15fe5ed6c8524a572521086deb119c348aad0e5a Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 10 Sep 2013 12:46:14 +0100 Subject: [PATCH] Fixed the Bring to Top bug and also fixed Group/World.swap when the child was a tail node. Also improved Group.dump significantly. Fixed Phaser.Utils namespace clash too. --- examples/bring to top.php | 6 +-- examples/bringToTop3.php | 94 +++++++++++++++++++++++++++++++++++++++ src/core/Group.js | 51 ++++++++++++++++----- src/core/World.js | 6 +-- src/gameobjects/Sprite.js | 2 +- src/loader/Cache.js | 8 +++- src/utils/Debug.js | 4 -- src/utils/Utils.js | 37 +++++++++++++++ 8 files changed, 182 insertions(+), 26 deletions(-) create mode 100644 examples/bringToTop3.php diff --git a/examples/bring to top.php b/examples/bring to top.php index bbf52967..c405cf91 100644 --- a/examples/bring to top.php +++ b/examples/bring to top.php @@ -30,14 +30,14 @@ function create() { // This returns an array of all the image keys in the cache - var images = game.cache.getImageKeys() + var images = game.cache.getImageKeys(); // Now let's create some random sprites and enable them all for drag and 'bring to top' for (var i = 0; i < 20; i++) { - var tempSprite = game.add.sprite(game.world.randomX, game.world.randomY, game.rnd.pick(images)); + var img = game.rnd.pick(images); + var tempSprite = game.add.sprite(game.world.randomX, game.world.randomY, img); tempSprite.inputEnabled = true; - // tempSprite.input.start(i, false, true); tempSprite.input.enableDrag(false, true); } diff --git a/examples/bringToTop3.php b/examples/bringToTop3.php new file mode 100644 index 00000000..34796a8b --- /dev/null +++ b/examples/bringToTop3.php @@ -0,0 +1,94 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/core/Group.js b/src/core/Group.js index 3d5a95bf..5e35f821 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -6,10 +6,18 @@ Phaser.Group = function (game, parent, name) { this.name = name || 'group'; this._container = new PIXI.DisplayObjectContainer(); + this._container.name = this.name; if (parent) { - parent.addChild(this._container); + if (parent instanceof Phaser.Group) + { + parent._container.addChild(this._container); + } + else + { + parent.addChild(this._container); + } } else { @@ -84,9 +92,9 @@ Phaser.Group.prototype = { var child2Next = child2._iNext; var endNode = this._container.last._iNext; - var currentNode = this._container.first; + var currentNode = this.game.stage._stage; - do + do { if (currentNode !== child1 && currentNode !== child2) { @@ -161,9 +169,10 @@ Phaser.Group.prototype = { bringToTop: function (child) { - if (child !== this._container.last) + if (child.group === this) { - this.swap(child, this._container.last); + this.remove(child); + this.add(child); } return child; @@ -610,6 +619,7 @@ Phaser.Group.prototype = { child.events.onRemovedFromGroup.dispatch(child, this); this._container.removeChild(child); + child.group = null; }, @@ -654,15 +664,31 @@ Phaser.Group.prototype = { }, - dump: function () { + dump: function (full) { - console.log("\nNode\t\t|\t\tNext\t\t|\t\tPrev\t\t|\t\tFirst\t\t|\t\tLast"); - console.log("\t\t\t|\t\t\t\t\t|\t\t\t\t\t|\t\t\t\t\t|"); + if (typeof full == 'undefined') + { + full = false; + } - var displayObject = this._container; + var spacing = 20; + var output = "\n" + Phaser.Utils.pad('Node', spacing) + "|" + Phaser.Utils.pad('Next', spacing) + "|" + Phaser.Utils.pad('Previous', spacing) + "|" + Phaser.Utils.pad('First', spacing) + "|" + Phaser.Utils.pad('Last', spacing); - var testObject = displayObject.last._iNext; - displayObject = displayObject.first; + console.log(output); + + var output = Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing); + console.log(output); + + if (full) + { + var testObject = this.game.stage._stage.last._iNext; + var displayObject = this.game.stage._stage; + } + else + { + var testObject = this._container.last._iNext; + var displayObject = this._container; + } do { @@ -712,7 +738,8 @@ Phaser.Group.prototype = { nameLast = '-'; } - console.log(name + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast); + var output = Phaser.Utils.pad(name, spacing) + "|" + Phaser.Utils.pad(nameNext, spacing) + "|" + Phaser.Utils.pad(namePrev, spacing) + "|" + Phaser.Utils.pad(nameFirst, spacing) + "|" + Phaser.Utils.pad(nameLast, spacing); + console.log(output); displayObject = displayObject._iNext; diff --git a/src/core/World.js b/src/core/World.js index 8bb58355..3ffd97cc 100644 --- a/src/core/World.js +++ b/src/core/World.js @@ -86,10 +86,8 @@ Phaser.World.prototype = { bringToTop: function (child) { - if (child !== this.game.stage._stage.last) - { - this.swapChildren(child, this.game.stage._stage.last); - } + this.remove(child); + this.add(child); return child; diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 09e72ce8..eca811c4 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -2,7 +2,7 @@ Phaser.Sprite = function (game, x, y, key, frame) { x = x || 0; y = y || 0; - key = key || null; // if null we ought to set to the phaser logo or something :) + key = key || null; frame = frame || null; this.game = game; diff --git a/src/loader/Cache.js b/src/loader/Cache.js index 4e71f7d5..d883b43a 100644 --- a/src/loader/Cache.js +++ b/src/loader/Cache.js @@ -391,8 +391,12 @@ Phaser.Cache.prototype = { var output = []; - for (var item in array) { - output.push(item); + for (var item in array) + { + if (item !== '__default') + { + output.push(item); + } } return output; diff --git a/src/utils/Debug.js b/src/utils/Debug.js index 600e4298..146a4cec 100644 --- a/src/utils/Debug.js +++ b/src/utils/Debug.js @@ -5,10 +5,6 @@ * @module Phaser */ -Phaser.Utils = { - // Until we have a proper entry-point -} - /** * A collection of methods for displaying debug information about game objects. * diff --git a/src/utils/Utils.js b/src/utils/Utils.js index 9504a033..73e704c4 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -1,5 +1,42 @@ + Phaser.Utils = { + /** + * Javascript string pad + * http://www.webtoolkit.info/ + * pad = the string to pad it out with (defaults to a space) + * dir = 1 (left), 2 (right), 3 (both) + **/ + pad: function (str, len, pad, dir) { + + if (typeof(len) == "undefined") { var len = 0; } + if (typeof(pad) == "undefined") { var pad = ' '; } + if (typeof(dir) == "undefined") { var dir = 3; } + + if (len + 1 >= str.length) + { + switch (dir) + { + case 1: + str = Array(len + 1 - str.length).join(pad) + str; + break; + + case 3: + var right = Math.ceil((padlen = len - str.length) / 2); + var left = padlen - right; + str = Array(left+1).join(pad) + str + Array(right+1).join(pad); + break; + + default: + str = str + Array(len + 1 - str.length).join(pad); + break; + } + } + + return str; + + }, + // This is a slightly modified version of jQuery.isPlainObject isPlainObject: function (obj) {