何时使用 CCSpriteBatchNode?

When to use CCSpriteBatchNode?(何时使用 CCSpriteBatchNode?)

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

问题描述

在 Cocos2d 中我将播放动画.动画大约有12帧,每一帧都比较大.其实每一帧的-hd版本还是蛮大的.

in Cocos2d I will be playing an animation. The animation has about 12 frames, and each frame is rather big. In fact, the -hd version of each frame is quite huge.

无论如何,首先,我通过使用 Zwoptex 将所有 12 帧放入纹理中来创建它.纹理约为 2048x2048.

Anyway, first, I created it by putting all 12 frames in a texture using Zwoptex. The texture is about 2048x2048.

这样我就可以使用该纹理为 CCSpriteBatchNode 中的 CCSprite 设置动画.

This is so I can animate a CCSprite in a CCSpriteBatchNode using that texture.

但我似乎收到了 2 级内存警告.

But I seem to be getting a level 2 memory warning.

现在我想起来了,我不认为 CCSpriteBatchNode 应该用于 one 精灵.我想它只有在你想绘制大量使用相同纹理的精灵时才有用.

Now that I think of it, I don't think CCSpriteBatchNode was supposed to be used for one sprite. I guess it was only useful if you wanted to draw lots of sprites that use the same texture.

所以我想知道:我应该逐帧动画精灵吗(没有巨大的纹理)?还是可以以不同的方式使用这种巨大的纹理?

So I want to know: Should I animate the sprite frame by frame (no huge texture)? Or is it possible to use that huge texture but in a different way?

推荐答案

CCSpriteBatchNode 是对的.CCSpriteBatchNode 就像一个批处理节点:如果它包含子节点,它将在 1 个单独的 OpenGL 调用中绘制它们(通常称为批量绘制"),在没有 CCSpriteBatchNode(在这种情况下)的情况下,所有批量绘制"将被称为多次作为孩子(精灵)的数量.

You are right about CCSpriteBatchNode. CCSpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call (often known as "batch draw"), in absence of CCSpriteBatchNode (in this case) all the "batch draw" will be called as many time as number of children (sprites).

CCSpriteBatchNode 可以引用一个且仅一个纹理(一个图像文件,一个纹理图集),即由 zwoptex 创建的精灵表.只有包含在该纹理中的 CCSprite 才能添加到 CCSpriteBatchNode.添加到 CCSpriteBatchNode 的所有 CCSprite 都在一个 OpenGL ES 绘制调用中绘制.如果 CCSprite 未添加到 CCSpriteBatchNode 中,则每个都需要 OpenGL ES 绘制调用,效率较低.

A CCSpriteBatchNode can reference one and only one texture (one image file, one texture atlas) i.e. sprite sheet created by zwoptex. Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode. All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call. If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.

根据您的场景,您不必使用 CCSpriteBatchNode,因为在任何给定时间只渲染一个纹理.

According to your scenario, you don't have to use CCSpriteBatchNode since there is only one texture rendered at any given time.

所以我想知道:我应该逐帧动画精灵吗(没有巨大的纹理)?还是可以以不同的方式使用这种巨大的纹理?

So I want to know: Should I animate the sprite frame by frame (no huge texture)? Or is it possible to use that huge texture but in a different way?

没关系.无论如何,您将加载 2048 x 2048 纹理.需要思考的问题是,为什么只有一个 2048 x 2048 纹理会给您 2 级警告?你加载了多少这样的纹理.BTW 2048 x 2048 只支持上面的 iPod3G/iPhone3GS(没问题).

It doesn't matter. You will be loading the 2048 x 2048 texture anyways. The issue to ponder is why only one 2048 x 2048 texture is giving you a Level 2 warning? how many such textures are you loading. BTW 2048 x 2048 is only supported on iPod3G/iPhone3GS above (which is fine).

如果您正在加载许多纹理(这对我来说似乎是正确的).您需要编写一些逻辑,以便在不需要时卸载纹理.请查看以下方法:

In case you are loading many textures (which seems true to me). You need to program some logic where you can unload the texture when they are not necessary. Do look at the following methods:

[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:fileName];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];

就动画而言,您可以创建 CCAnimation 并使用它,或者(取决于您的场景)您可以使用 setDisplayFrame(CCSpriteFrame frame);

As far as animation is concerned you can create CCAnimation and use that or (depending upon your scenario) you can use setDisplayFrame(CCSpriteFrame frame);

这篇关于何时使用 CCSpriteBatchNode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:何时使用 CCSpriteBatchNode?

基础教程推荐