如何正确使用CCSpriteFrameCache和CCSpriteBatchNode? [英] How do I properly use CCSpriteFrameCache and CCSpriteBatchNode?

查看:127
本文介绍了如何正确使用CCSpriteFrameCache和CCSpriteBatchNode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加一个CCSpriteFrameCache或CCSpriteBatchNode到我的cocos2d应用程序时,我不明白我做什么。可以有人请解释以下几点(如果你可以解释几个,这将是有帮助的;请根据您回答的问题在答案前写上相应的信):

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):

[所有问题暗示着实现最佳性能和最低内存使用]

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

a)为每个图层创建spritesheet至关重要吗? (例如:Menu - 自己的spritesheet,GameLayer - 自己的spritesheet ...)

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

b1)所以, / strong>

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

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

然后通过调用

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

而不将它们添加到批处理节点?因为从我的理解,它的工作原理是这样的:

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

我添加我的spritesheet与所有的精灵在屏幕上。我的应用程序然后进入plist,寻找我想要显示的精灵的坐标,然后将它放在屏幕上。那么为什么要调用

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 addChild:mySprite];

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

推荐答案

a如果我不再需要它, )最好创建尽可能少的spriteheets(CCSpriteBatchNodes)。精灵批处理减少绘制调用。绘制调用很昂贵。仍然,每个批处理节点创建一个绘制调用。所以你想尽可能少使用,因为最终的目标是保持绘制调用尽可能低。

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.

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

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.

b1不能这样做,因为批处理节点渲染它的子节点。如果你添加sprite到任何其他节点,每个sprite绘制自己,这意味着每个sprite一个额外的绘制调用。和sprite批处理节点无关。

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.

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

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.

这篇关于如何正确使用CCSpriteFrameCache和CCSpriteBatchNode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆