Skip to content

Commit 069d2dc

Browse files
committed
Issue cocos2d#2416: Code separation
1 parent 81b04d6 commit 069d2dc

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

cocos2d/tilemap/CCTMXLayer.js

+3-31
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
354354
tile.anchorX = 0;
355355
tile.anchorY = 0;
356356
tile.opacity = this._opacity;
357-
tile._renderCmd._cachedParent = this._renderCmd;
358357

359358
var indexForZ = this._atlasIndexForExistantZ(z);
360359
this.addSpriteWithoutQuad(tile, indexForZ, z);
@@ -691,7 +690,7 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
691690
rect = cc.rectPixelsToPoints(rect);
692691

693692
var z = 0 | (pos.x + pos.y * this._layerSize.width);
694-
var tile = this._reusedTileWithRect(rect);
693+
var tile = this._renderCmd._reusedTileWithRect(rect);
695694
this._setupTileSprite(tile, pos, gid);
696695

697696
// optimization:
@@ -712,7 +711,7 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
712711
rect = cc.rectPixelsToPoints(rect);
713712

714713
var z = 0 | (pos.x + pos.y * this._layerSize.width);
715-
var tile = this._reusedTileWithRect(rect);
714+
var tile = this._renderCmd._reusedTileWithRect(rect);
716715
this._setupTileSprite(tile, pos, gid);
717716

718717
// get atlas index
@@ -746,7 +745,7 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
746745
rect.width / locScaleFactor, rect.height / locScaleFactor);
747746
var z = pos.x + pos.y * this._layerSize.width;
748747

749-
var tile = this._reusedTileWithRect(rect);
748+
var tile = this._renderCmd._reusedTileWithRect(rect);
750749
this._setupTileSprite(tile, pos, gid);
751750

752751
// get atlas index
@@ -832,33 +831,6 @@ cc.TMXLayer = cc.SpriteBatchNode.extend(/** @lends cc.TMXLayer# */{
832831
}
833832
},
834833

835-
_reusedTileWithRect:function (rect) {
836-
if(cc._renderType === cc._RENDER_TYPE_WEBGL){
837-
if (!this._reusedTile) {
838-
this._reusedTile = new cc.Sprite();
839-
this._reusedTile.initWithTexture(this.texture, rect, false);
840-
this._reusedTile.batchNode = this;
841-
} else {
842-
// XXX HACK: Needed because if "batch node" is nil,
843-
// then the Sprite'squad will be reset
844-
this._reusedTile.batchNode = null;
845-
846-
// Re-init the sprite
847-
this._reusedTile.setTextureRect(rect, false);
848-
849-
// restore the batch node
850-
this._reusedTile.batchNode = this;
851-
}
852-
} else {
853-
this._reusedTile = new cc.Sprite();
854-
this._reusedTile.initWithTexture(this._renderCmd._texture, rect, false);
855-
this._reusedTile.batchNode = this;
856-
this._reusedTile.parent = this;
857-
this._reusedTile._renderCmd._cachedParent = this._renderCmd;
858-
}
859-
return this._reusedTile;
860-
},
861-
862834
_vertexZForPos:function (pos) {
863835
var ret = 0;
864836
var maxVal = 0;

cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js

+10
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,14 @@
186186
var node = this._node;
187187
node.tileset.imageSize = this._originalTexture.getContentSizeInPixels();
188188
};
189+
190+
proto._reusedTileWithRect = function(rect){
191+
var node = this._node;
192+
node._reusedTile = new cc.Sprite();
193+
node._reusedTile.initWithTexture(node._renderCmd._texture, rect, false);
194+
node._reusedTile.batchNode = node;
195+
node._reusedTile.parent = node;
196+
node._reusedTile._renderCmd._cachedParent = node._renderCmd;
197+
return node._reusedTile;
198+
};
189199
})();

cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,25 @@
4343
// cons:
4444
// - difficult to scale / rotate / etc.
4545
this._textureAtlas.texture.setAliasTexParameters();
46-
}
46+
};
47+
48+
proto._reusedTileWithRect = function(rect){
49+
var node = this._node;
50+
if (!node._reusedTile) {
51+
node._reusedTile = new cc.Sprite();
52+
node._reusedTile.initWithTexture(node.texture, rect, false);
53+
node._reusedTile.batchNode = node;
54+
} else {
55+
// XXX HACK: Needed because if "batch node" is nil,
56+
// then the Sprite'squad will be reset
57+
node._reusedTile.batchNode = null;
58+
59+
// Re-init the sprite
60+
node._reusedTile.setTextureRect(rect, false);
61+
62+
// restore the batch node
63+
node._reusedTile.batchNode = node;
64+
}
65+
return node._reusedTile;
66+
};
4767
})();

0 commit comments

Comments
 (0)