How to use CCSpriteFrameCache and CCSpriteBatchNode correctly?

I don’t understand what I do when I add CCSpriteFrameCache or CCSpriteBatchNode to my cocos2d application. Can someone explain the following points (it would be useful if you could explain a few, please write an appropriate letter before answering in accordance with the question you are answering):

[all questions imply achieving the best performance and least memory usage]

a) Is it important to create sprites for each layer? (For example: Menu - own sprite, GameLayer - own sprite ...)

b) Can someone explain why I have to add sprites to the bundled version of node, and what is a node package?

b1) So, why can't I just do something like:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"menusprites.plist"]; CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"menusprites.png"]; [self addChild:spriteSheet]; 

And then just add sprites to my layer, calling

 CCSprite *mySprite = [CCSprite spriteWithSpriteFrameName:@""]; [self addChild:mySprite]; 

without adding them to the node package? Because from what I understand, it works as follows:

I add my sprite with all the sprites on it to the screen. Then my application goes into plist and looks for the coordinates of the sprite that I want to display, and then puts it on the screen. So why should I call

 [spriteSheet addChild:mySprite]; 

?

c) How can I get rid of sprites for memory purposes when I no longer need it?

+8
objective-c ipad cocos2d-iphone ccsprite
source share
2 answers

a) It is best to create as few sprites as possible (CCSpriteBatchNodes). Sprite listing reduces the number of callbacks. Drawing calls is expensive. However, each node batch creates one callback. So you want to use as little as possible, because the ultimate goal is to keep draw appeals as low as possible.

b) CCSpriteBatchNode displays all its children at once, in one batch callback. To do this, you need to add sprites to the node package so that it can display them all together. Only sprites using the same texture as the node package can be added to the node package folder, because you can only pull from the same texture. Whenever the engine needs to switch from one texture to another, it issues a new draw call.

b1) You cannot do this because the node package displays its children. If you add sprites to any other node, each sprite draws itself, which means one additional call to call for sprite. And the node sprite package has nothing to do.

c) CCSpriteBatchNode is a regular node. You can remove it from the scene, like any other node. Texture and sprite frames are cached in the CCTextureCache and CCSpriteFrameCache singleton classes. If you want to remove textures and sprite frames from memory, you must do this through the cache classes.

+14
source share
  • a) no

  • b) batchNode increases your productivity when you need to draw a lot of sprites at the same time, in case of a small number of sprites (10, 20., etc.) I don’t think that you will notice that any performance increases. batchNode is much faster because opengl only needs to draw it in order to see all the content. otherwise opengl will draw all your objects separately. that is, if you have 500, 600, 700 sprites, draw() and visit() methods will be called for each. if all of them are placed in batchNode, it will be only one draw() call and one visit() call

  • c) you can manually flush cached data to free up memory by calling these methods:


 [CCTextureCache purgeSharedTextureCache]; [CCSpriteFrameCache purgeSharedSpriteFrameCache]; [CCAnimationCache purgeSharedAnimationCache]; 
+2
source share

All Articles