如何正确使用CCSpriteFrameCache和CCSpriteBatchNode?

时间:2021-12-13 00:47:08

I do not understand what I do exactly when I add a CCSpriteFrameCache or CCSpriteBatchNode to my cocos2d application. Can somebody please explain the following points (it would be helpful if you could explain a few; please write the corresponding letter in front of your answer according to which question you are answering):

当我向我的cocos2d应用程序添加CCSpriteFrameCache或CCSpriteBatchNode时,我不明白我做了什么。有人可以解释以下几点(如果你能解释几个会有所帮助;请根据你回答的问题在你的答案前写下相应的字母):

[all questions imply the achievement of best performance and lowest memory-use]

[所有问题都意味着实现最佳性能和最低内存使用率]

a) Is it crucial to create spritesheets for every single layer ? (For example: Menu - own spritesheet, GameLayer - own spritesheet...)

a)为每一层创建spritesheets是否至关重要? (例如:菜单 - 自己的spritesheet,GameLayer - 自己的spritesheet ......)

b) Can somebody explain why I have to add sprites to the batch node, and what a batch node generally is ?

b)有人可以解释为什么我必须向批处理节点添加精灵,以及批处理节点通常是什么?

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

b1)那么,为什么我不能做以下事情:

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

And then just add sprites to my layer by calling

然后通过调用将sprite添加到我的图层

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

without adding them to the batch node ? Because from what I understand it works like this :

没有将它们添加到批处理节点?因为根据我的理解,它的工作原理如下:

I add my spritesheet with all the sprites on it to the screen. My app then goes into the plist and looks for the coordinates of the sprite I want to display and then places it on the screen. So why should I call

我将我的spritesheet与其上的所有精灵添加到屏幕上。我的应用程序然后进入plist并查找我想要显示的精灵的坐标,然后将其放在屏幕上。那我为什么要打电话呢

[spriteSheet addChild:mySprite];

?

c) How do I then get rid of the spritesheet for memory purposes when I do not need it anymore ?

c)当我不再需要它时,如何摆脱spritesheet用于记忆目的?

2 个解决方案

#1


14  

a) It is best to create as few spritesheets (CCSpriteBatchNodes) as is possible. Sprite batching reduces draw calls. Draw calls are expensive. Still, every batch node creates one draw call. So you want to use as few as possible because the ultimate goal is to keep draw calls as low as possible.

a)最好尽可能少地创建spritesheets(CCSpriteBatchNodes)。 Sprite批处理减少了绘制调用。划线电话费用昂贵。仍然,每个批处理节点创建一个绘图调用。因此,您希望尽可能少地使用,因为最终目标是尽可能降低绘制调用。

b) The CCSpriteBatchNode renders all of its children in one go, in one batched draw call. That's why you need to add sprites to the batch node so it can render them all together. Only sprites using the same texture as the batch node can be added to a batch node, because you can only batch draw from the same texture. Whenever the engine has to switch from one texture to another, it issues a new draw call.

b)CCSpriteBatchNode在一次批量绘制调用中一次性渲染其所有子节点。这就是为什么你需要为批处理节点添加精灵以便它们可以一起渲染它们的原因。只有与批处理节点使用相同纹理的精灵才能添加到批处理节点,因为您只能从同一纹理批量绘制。每当引擎必须从一个纹理切换到另一个纹理时,它就会发出一个新的绘制调用。

b1) You can't do this because the batch node renders its children. If you add the sprites to any other node, each sprite draws itself, which means one additional draw call per sprite. And the sprite batch node has nothing to do.

b1)您无法执行此操作,因为批处理节点呈现其子节点。如果将精灵添加到任何其他节点,则每个精灵都会自行绘制,这意味着每个精灵会进行一次额外的绘制调用。精灵批处理节点无关。

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

c)CCSpriteBatchNode只是一个常规节点。您可以像任何其他节点一样将其从场景中删除。纹理和子画面帧缓存在CCTextureCache和CCSpriteFrameCache单例类中。如果要从内存中删除纹理和子画面帧,则必须通过缓存类来完成。

#2


2  

  • a) no

  • b) batchNode increases your performance when you need to draw many sprites at the same time, in case of small number of sprites (10, 20. etc.) i dont think that you will notice any performance increasing. batchNode is much faster because opengl should draw only it to see all content. in other case opengl will draw all your objects separately. that is - if you will have 500, 600, 700 sprites, the draw() and visit() methods will be called for each one. if they all will be placed to the batchNode, it will be only one draw() call and one visit() call

    b)batchNode会在你需要同时绘制多个精灵的情况下提高你的性能,如果有少量精灵(10,20等),我不认为你会注意到任何性能提升。 batchNode要快得多,因为opengl应该只绘制它以查看所有内容。在其他情况下,opengl将分别绘制所有对象。也就是说 - 如果你有500,600,700个精灵,那么将为每个精灵调用draw()和visit()方法。如果它们都将被放置到batchNode,它将只有一个draw()调用和一个visit()调用

  • c) you can purge cached data manually to force memory freeing by calling these methods:

    c)您可以通过调用以下方法手动清除缓存数据以强制释放内存:


[CCTextureCache purgeSharedTextureCache];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
[CCAnimationCache purgeSharedAnimationCache];

#1


14  

a) It is best to create as few spritesheets (CCSpriteBatchNodes) as is possible. Sprite batching reduces draw calls. Draw calls are expensive. Still, every batch node creates one draw call. So you want to use as few as possible because the ultimate goal is to keep draw calls as low as possible.

a)最好尽可能少地创建spritesheets(CCSpriteBatchNodes)。 Sprite批处理减少了绘制调用。划线电话费用昂贵。仍然,每个批处理节点创建一个绘图调用。因此,您希望尽可能少地使用,因为最终目标是尽可能降低绘制调用。

b) The CCSpriteBatchNode renders all of its children in one go, in one batched draw call. That's why you need to add sprites to the batch node so it can render them all together. Only sprites using the same texture as the batch node can be added to a batch node, because you can only batch draw from the same texture. Whenever the engine has to switch from one texture to another, it issues a new draw call.

b)CCSpriteBatchNode在一次批量绘制调用中一次性渲染其所有子节点。这就是为什么你需要为批处理节点添加精灵以便它们可以一起渲染它们的原因。只有与批处理节点使用相同纹理的精灵才能添加到批处理节点,因为您只能从同一纹理批量绘制。每当引擎必须从一个纹理切换到另一个纹理时,它就会发出一个新的绘制调用。

b1) You can't do this because the batch node renders its children. If you add the sprites to any other node, each sprite draws itself, which means one additional draw call per sprite. And the sprite batch node has nothing to do.

b1)您无法执行此操作,因为批处理节点呈现其子节点。如果将精灵添加到任何其他节点,则每个精灵都会自行绘制,这意味着每个精灵会进行一次额外的绘制调用。精灵批处理节点无关。

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

c)CCSpriteBatchNode只是一个常规节点。您可以像任何其他节点一样将其从场景中删除。纹理和子画面帧缓存在CCTextureCache和CCSpriteFrameCache单例类中。如果要从内存中删除纹理和子画面帧,则必须通过缓存类来完成。

#2


2  

  • a) no

  • b) batchNode increases your performance when you need to draw many sprites at the same time, in case of small number of sprites (10, 20. etc.) i dont think that you will notice any performance increasing. batchNode is much faster because opengl should draw only it to see all content. in other case opengl will draw all your objects separately. that is - if you will have 500, 600, 700 sprites, the draw() and visit() methods will be called for each one. if they all will be placed to the batchNode, it will be only one draw() call and one visit() call

    b)batchNode会在你需要同时绘制多个精灵的情况下提高你的性能,如果有少量精灵(10,20等),我不认为你会注意到任何性能提升。 batchNode要快得多,因为opengl应该只绘制它以查看所有内容。在其他情况下,opengl将分别绘制所有对象。也就是说 - 如果你有500,600,700个精灵,那么将为每个精灵调用draw()和visit()方法。如果它们都将被放置到batchNode,它将只有一个draw()调用和一个visit()调用

  • c) you can purge cached data manually to force memory freeing by calling these methods:

    c)您可以通过调用以下方法手动清除缓存数据以强制释放内存:


[CCTextureCache purgeSharedTextureCache];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
[CCAnimationCache purgeSharedAnimationCache];