Skip to content

Commit 2165ee7

Browse files
fixed SpriteBatchNode bug
1 parent fa7270a commit 2165ee7

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
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

+18-16
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};
@@ -838,9 +840,9 @@ cc.SpriteBatchNode = cc.Node.extend(/** @lends cc.SpriteBatchNode# */{
838840
//fast dispatch, give every child a new atlasIndex based on their relative zOrder (keep parent -> child relations intact)
839841
// and at the same time reorder descedants and the quads to the right index
840842
//if (cc.renderContextType == cc.WEBGL) {
841-
for (i = 0; i < this._children.length; i++) {
842-
index = this._updateAtlasIndex(this._children[i], index);
843-
}
843+
for (i = 0; i < this._children.length; i++) {
844+
index = this._updateAtlasIndex(this._children[i], index);
845+
}
844846
//}
845847
}
846848

0 commit comments

Comments
 (0)