@@ -89,18 +89,6 @@ Phaser.Group = function (game, parent, name, useStage) {
8989 */
9090 this . cursor = null ;
9191
92- /**
93- * Helper for sort.
94- */
95- this . _sortIndex = '' ;
96-
97- /**
98- * Helper for sort.
99- */
100- this . _sortOrder = 0 ;
101-
102- this . _sortCache = [ ] ;
103-
10492} ;
10593
10694/**
@@ -373,8 +361,19 @@ Phaser.Group.prototype = {
373361
374362 } ,
375363
364+ swapIndex : function ( index1 , index2 ) {
365+
366+ var child1 = this . getAt ( index1 ) ;
367+ var child2 = this . getAt ( index2 ) ;
368+
369+ console . log ( 'swapIndex ' , index1 , ' with ' , index2 ) ;
370+
371+ this . swap ( child1 , child2 ) ;
372+
373+ } ,
374+
376375 /**
377- * Swaps the position of two children in this Group.
376+ * Swaps the position of two children in this Group. Both children must be in this Group.
378377 * You cannot swap a child with itself, or swap un-parented children, doing so will return false.
379378 *
380379 * @method Phaser.Group#swap
@@ -384,11 +383,8 @@ Phaser.Group.prototype = {
384383 */
385384 swap : function ( child1 , child2 ) {
386385
387- console . log ( 'starting swap' , child1 . name , 'with' , child2 . name ) ;
388-
389- if ( child1 === child2 || ! child1 . parent || ! child2 . parent )
386+ if ( child1 === child2 || ! child1 . parent || ! child2 . parent || child1 . group !== this || child2 . group !== this )
390387 {
391- console . log ( 'aborting' ) ;
392388 return false ;
393389 }
394390
@@ -398,17 +394,8 @@ Phaser.Group.prototype = {
398394 var child2Prev = child2 . _iPrev ;
399395 var child2Next = child2 . _iNext ;
400396
401- // var endNode = this._container.last._iNext;
402- var endNode = this . _container . last ;
397+ var endNode = this . _container . last . _iNext ;
403398 var currentNode = this . game . stage . _stage ;
404-
405- // console.log('start do while. start node: ', currentNode.name);
406- // console.log(typeof endNode);
407-
408- // if (endNode)
409- // {
410- // console.log('end node: ', endNode.name);
411- // }
412399
413400 do
414401 {
@@ -440,10 +427,6 @@ Phaser.Group.prototype = {
440427 if ( child1 . _iNext == child2 )
441428 {
442429 // This is a downward (A to B) neighbour swap
443- // console.log('downward A to B');
444- // this.childTest('1', child1);
445- // this.childTest('2', child2);
446-
447430 child1 . _iNext = child2Next ;
448431 child1 . _iPrev = child2 ;
449432 child2 . _iNext = child1 ;
@@ -467,10 +450,6 @@ Phaser.Group.prototype = {
467450 else if ( child2 . _iNext == child1 )
468451 {
469452 // This is an upward (B to A) neighbour swap
470- // console.log('upward B to A');
471- // this.childTest('1', child1);
472- // this.childTest('2', child2);
473-
474453 child1 . _iNext = child2 ;
475454 child1 . _iPrev = child2Prev ;
476455 child2 . _iNext = child1Next ;
@@ -494,11 +473,6 @@ Phaser.Group.prototype = {
494473 else
495474 {
496475 // Children are far apart
497- // console.log('far apart A to B');
498-
499- // this.childTest('1', child1);
500- // this.childTest('2', child2);
501-
502476 child1 . _iNext = child2Next ;
503477 child1 . _iPrev = child2Prev ;
504478 child2 . _iNext = child1Next ;
@@ -1024,84 +998,50 @@ Phaser.Group.prototype = {
1024998 if ( typeof index === 'undefined' ) { index = 'y' ; }
1025999 if ( typeof order === 'undefined' ) { order = Phaser . Group . SORT_ASCENDING ; }
10261000
1027- this . _sortIndex = index ;
1028- this . _sortOrder = order ;
1029- this . _sortCache = this . _container . children . slice ( ) ;
1030-
10311001 console . log ( '-vvv--------------------------------------------------------------------------------' ) ;
10321002
10331003 this . dump ( true ) ;
10341004
1035- for ( var i = 0 ; i < this . _sortCache . length ; i ++ )
1036- {
1037- console . log ( i + ' = ' + this . _sortCache [ i ] . name + ' at y: ' + this . _sortCache [ i ] . y ) ;
1038- }
1039-
1040- console . log ( '---------------------------------------------------------------------------------' ) ;
1041-
1042- this . _sortCache . sort ( this . sortHandler . bind ( this ) ) ;
1043-
1044- // Should do an array compare here, no need to rebuild the display list if the arrays are the same
1045-
1046- for ( var i = 0 ; i < this . _sortCache . length ; i ++ )
1047- {
1048- console . log ( i + ' = ' + this . _sortCache [ i ] . name + ' at y: ' + this . _sortCache [ i ] . y ) ;
1049- }
1050-
1051- for ( var i = 0 ; i < this . _sortCache . length ; i ++ )
1052- {
1053- // if (this._container.children[i] !== this._sortCache[i])
1054- // {
1055- // console.log('swapped:', this._container.children[i].name,'with',this._sortCache[i].name);
1056- this . swap ( this . _container . children [ i ] , this . _sortCache [ i ] ) ;
1057- // }
1058- }
1059-
1060- // Now put it back again
1061- this . _container . children = this . _sortCache . slice ( ) ;
1062-
1063- this . _container . updateTransform ( ) ;
1005+ var swapped ;
1006+ var temp ;
1007+
1008+ do {
1009+
1010+ swapped = false ;
1011+
1012+ for ( var i = 0 , len = this . _container . children . length - 1 ; i < len ; i ++ )
1013+ {
1014+ if ( order == Phaser . Group . SORT_ASCENDING )
1015+ {
1016+ if ( this . _container . children [ i ] [ index ] > this . _container . children [ i + 1 ] [ index ] )
1017+ {
1018+ this . swap ( this . getAt ( i ) , this . getAt ( i + 1 ) ) ;
1019+ temp = this . _container . children [ i ] ;
1020+ this . _container . children [ i ] = this . _container . children [ i + 1 ] ;
1021+ this . _container . children [ i + 1 ] = temp ;
1022+ swapped = true ;
1023+ }
1024+ }
1025+ else
1026+ {
1027+ if ( this . _container . children [ i ] [ index ] < this . _container . children [ i + 1 ] [ index ] )
1028+ {
1029+ this . swap ( this . getAt ( i ) , this . getAt ( i + 1 ) ) ;
1030+ temp = this . _container . children [ i ] ;
1031+ this . _container . children [ i ] = this . _container . children [ i + 1 ] ;
1032+ this . _container . children [ i + 1 ] = temp ;
1033+ swapped = true ;
1034+ }
1035+ }
1036+ }
1037+ } while ( swapped ) ;
10641038
10651039 this . dump ( true ) ;
10661040
10671041 console . log ( '-^^^--------------------------------------------------------------------------------' ) ;
10681042
10691043 } ,
10701044
1071- /**
1072- * Helper function for the sort process.
1073- *
1074- * @param {Basic } Obj1 The first object being sorted.
1075- * @param {Basic } Obj2 The second object being sorted.
1076- *
1077- * @return {number } An integer value: -1 (Obj1 before Obj2), 0 (same), or 1 (Obj1 after Obj2).
1078- */
1079- sortHandler : function ( obj1 , obj2 ) {
1080-
1081- if ( ! obj1 || ! obj2 )
1082- {
1083- // console.log('null objects in sort', obj1, obj2);
1084- return 0 ;
1085- }
1086-
1087- // number only test
1088- // return obj1[this._sortIndex] - obj2[this._sortIndex];
1089-
1090- if ( obj1 [ this . _sortIndex ] < obj2 [ this . _sortIndex ] )
1091- {
1092- // console.log('1 < 2');
1093- return this . _sortOrder ;
1094- }
1095- else if ( obj1 [ this . _sortIndex ] > obj2 [ this . _sortIndex ] )
1096- {
1097- // console.log('1 > 2');
1098- return - this . _sortOrder ;
1099- }
1100-
1101- return 0 ;
1102-
1103- } ,
1104-
11051045 /**
11061046 * Iterates over the children of the Group. When a child has a property matching key that equals the given value, it is considered as a match.
11071047 * Matched children can be sent to the optional callback, or simply returned or counted.
@@ -1441,13 +1381,11 @@ Phaser.Group.prototype = {
14411381 if ( full )
14421382 {
14431383 var testObject = this . game . stage . _stage . last . _iNext ;
1444- // var testObject = this.game.stage._stage.last;
14451384 var displayObject = this . game . stage . _stage ;
14461385 }
14471386 else
14481387 {
14491388 var testObject = this . _container . last . _iNext ;
1450- // var testObject = this._container.last;
14511389 var displayObject = this . _container ;
14521390 }
14531391
0 commit comments