Skip to content

Commit 3707ff2

Browse files
author
SeanLin
committed
Merge pull request #749 from ShengxiangChen/spriteBug
fixed "SpriteBatchNodeReorderSameIndex" test case
2 parents e8dc4c3 + 2165ee7 commit 3707ff2

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

cocos2d/platform/CCMacro.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @constant
2929
* @type Number
3030
*/
31-
cc.INVALID_INDEX = 0xffffffff;
31+
cc.INVALID_INDEX = -1;
3232

3333
/**
3434
* PI is the ratio of a circle's circumference to its diameter.

cocos2d/sprite_nodes/CCSprite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @constant
3030
* @type Number
3131
*/
32-
cc.SPRITE_INDEX_NOT_INITIALIZED = "0xffffffff";
32+
cc.SPRITE_INDEX_NOT_INITIALIZED = -1;
3333

3434
/**
3535
* generate texture's cache for texture tint

cocos2d/sprite_nodes/CCSpriteBatchNode.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
****************************************************************************/
2626

2727

28-
2928
/**
3029
* @constant
3130
* @type Number
@@ -118,7 +117,6 @@ cc.SpriteBatchNode = cc.Node.extend(/** @lends cc.SpriteBatchNode# */{
118117
curIndex++;
119118
needNewIndex = false;
120119
}
121-
122120
for (var i = 0; i < pArray.length; i++) {
123121
var child = pArray[i];
124122
if (needNewIndex && child.getZOrder() >= 0) {
@@ -150,17 +148,21 @@ cc.SpriteBatchNode = cc.Node.extend(/** @lends cc.SpriteBatchNode# */{
150148
},
151149

152150
_swap:function (oldIndex, newIndex) {
153-
var quads = this._textureAtlas.getQuads();
154-
var tempItem = this._descendants[oldIndex];
155-
var tempIteQuad = quads[oldIndex];
156-
157-
//update the index of other swapped item
158-
this._descendants[newIndex].setAtlasIndex(oldIndex);
151+
if ((this._descendants.length >= 2) && newIndex < this._descendants.length) {
152+
if (oldIndex == -1) {
153+
oldIndex = this._descendants.length - 1;
154+
}
155+
var quads = this._textureAtlas.getQuads();
156+
var tempItem = this._descendants[oldIndex];
157+
var tempIteQuad = quads[oldIndex];
159158

160-
this._descendants[oldIndex] = this._descendants[newIndex];
161-
quads[oldIndex] = quads[newIndex];
162-
this._descendants[newIndex] = tempItem;
163-
quads[newIndex] = tempIteQuad;
159+
//update the index of other swapped item
160+
this._descendants[newIndex].setAtlasIndex(oldIndex);
161+
this._descendants[oldIndex] = this._descendants[newIndex];
162+
quads[oldIndex] = quads[newIndex];
163+
this._descendants[newIndex] = tempItem;
164+
quads[newIndex] = tempIteQuad;
165+
}
164166
},
165167

166168
// IMPORTANT XXX IMPORTNAT:
@@ -610,7 +612,7 @@ cc.SpriteBatchNode = cc.Node.extend(/** @lends cc.SpriteBatchNode# */{
610612
* @param {Number} dst
611613
*/
612614
setBlendFunc:function (src, dst) {
613-
if(arguments.length == 1)
615+
if (arguments.length == 1)
614616
this._blendFunc = src;
615617
else
616618
this._blendFunc = {src:src, dst:dst};
@@ -822,8 +824,7 @@ cc.SpriteBatchNode = cc.Node.extend(/** @lends cc.SpriteBatchNode# */{
822824
j = i - 1;
823825

824826
//continue moving element downwards while zOrder is smaller or when zOrder is the same but orderOfArrival is smaller
825-
while (j >= 0 && (tempItem.getZOrder() < this._children[j].getZOrder() ||
826-
(tempItem.getZOrder() == this._children[j].getZOrder() && tempItem.getOrderOfArrival() < this._children[j].getOrderOfArrival()))) {
827+
while (j >= 0 && (tempItem.getZOrder() < this._children[j].getZOrder() || (tempItem.getZOrder() == this._children[j].getZOrder() && tempItem.getOrderOfArrival() < this._children[j].getOrderOfArrival()))) {
827828
this._children[j + 1] = this._children[j];
828829
j--;
829830
}
@@ -838,11 +839,11 @@ cc.SpriteBatchNode = cc.Node.extend(/** @lends cc.SpriteBatchNode# */{
838839
var index = 0;
839840
//fast dispatch, give every child a new atlasIndex based on their relative zOrder (keep parent -> child relations intact)
840841
// and at the same time reorder descedants and the quads to the right index
841-
if (cc.renderContextType == cc.WEBGL) {
842-
for (i = 0; i < this._children.length; i++) {
843-
index = this._updateAtlasIndex(this._children[i], index);
844-
}
842+
//if (cc.renderContextType == cc.WEBGL) {
843+
for (i = 0; i < this._children.length; i++) {
844+
index = this._updateAtlasIndex(this._children[i], index);
845845
}
846+
//}
846847
}
847848

848849
this._reorderChildDirty = false;

0 commit comments

Comments
 (0)