Aram Kocharyan
Aram Kocharyan

Reputation: 20431

Use same png for multiple CCSpriteBatchNode (cocos2d)

I have subclassed CCSprite into FMObject as the base class of my game objects. This class creates a b2Body and uses a CCSpriteBatchNode. Problem is, making multiple copies of say FMSpaceShip will make multiple CCSpriteBatchNode objects that all use the same png. Will this result in the png being loaded again and again, or will there be some optimisation here?

Upvotes: 0

Views: 473

Answers (1)

Loic Argelies
Loic Argelies

Reputation: 561

Yes, you can count on Cocos2D internal cache optimization there. The texture will get loaded only once in the CCTextureCache, and will be referenced by each of the batch nodes, using reference counting.

That said, there should be no need to create a batch node per sprite, if i am reading your description correctly. You should consider instead to create the batch node once externally to your CCSprite class, then have your sprites (or derived FMSpaceShips) use that single batch node for rendering.

In some cases, it is still useful to create several batch nodes - For instance when you want them to render at different zOrder (say, you have clouds sprites that always need to render behind your main space ship sprites).

Upvotes: 2

Related Questions