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.

This commit is contained in:
Richard Davey
2013-09-10 12:46:14 +01:00
parent 609ea7ec8f
commit 15fe5ed6c8
8 changed files with 182 additions and 26 deletions
+3 -3
View File
@@ -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);
}
+94
View File
@@ -0,0 +1,94 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create });
function preload() {
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
game.load.image('card', 'assets/sprites/mana_card.png');
}
var group;
var s;
var a;
var b;
var c;
var d;
var e;
var f;
var g;
var h;
var i;
var group2;
var a2;
var b2;
var c2;
function create() {
group = game.add.group(null, 'group1');
a = group.create(70, 200, 'sonic');
b = group.create(140, 200, 'sonic');
c = group.create(210, 200, 'sonic');
d = group.create(280, 200, 'sonic');
e = group.create(350, 200, 'sonic');
f = group.create(420, 200, 'sonic');
g = group.create(50, 300, 'sonic');
h = group.create(80, 300, 'sonic');
i = group.create(110, 300, 'sonic');
a.name = 'A';
b.name = 'B';
c.name = 'C';
d.name = 'D';
e.name = 'E';
f.name = 'F';
g.name = 'g';
h.name = 'h';
i.name = 'i';
/*
group2 = game.add.group(group, 'group2');
a2 = group2.create(500, 200, 'card');
b2 = group2.create(550, 200, 'card');
c2 = group2.create(600, 200, 'card');
a2.name = 'A2';
b2.name = 'B2';
c2.name = 'C2';
*/
// console.log(group2._container.parent);
group.dump(true);
game.input.onUp.add(runChange, this);
}
function runChange () {
group.swap(a, i);
group.dump(true);
}
})();
</script>
</body>
</html>
+39 -12
View File
@@ -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;
+2 -4
View File
@@ -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;
+1 -1
View File
@@ -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;
+6 -2
View File
@@ -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;
-4
View File
@@ -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.
*
+37
View File
@@ -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) {