我正在使用 cocos2d 在我的 iPad 上玩一个小游戏,我遇到了一些性能问题.我有一个 512x512 的图像平铺作为我的背景.这给了我大约 40fps 和 20 个精灵(在 CCSpriteBatchNode
中),背景代码是这样的:
I'm toying with a small game on my iPad using cocos2d and I've run into some performance worries. I have a 512x512 image tiled as my background. That gives me around 40fps with 20 sprites (in a CCSpriteBatchNode
), the code for the background is this:
CCSprite *background;
background = [CCSprite spriteWithFile:@"oak.png" rect : CGRectMake(0,
0,
size.width,
size.height)];
background.position = ccp( size.width /2 , size.height/2 );
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[background.texture setTexParameters: ¶ms];
如果我移除背景,我会得到稳定的 60fps.
If I remove the background I get a solid 60fps.
我已尝试将图像转换为 PVRTC,但确实提供了额外的一两帧.我使用 1024x768 图像而不是平铺版本获得相同的帧速率.
I've tried converting the image to PVRTC and that did give an extra fps or two. I get identical framerates using a 1024x768 image instead of the tiled version.
因为我的背景将保持轴对齐、未缩放且通常是静态的.我认为应该有比将它作为常规 CCSprite
更快的方法来绘制它?
Since my background will remain axis aligned, unscaled and generally static. I figure there should be a faster way to draw it than having it as a regular CCSprite
?
原来 cocos2d 以神秘的方式移动.将背景添加到否则为空的包装 CCSprite
可使帧率恢复到 60:
Turns out cocos2d moves in mysterious ways. Adding the background to an otherwise empty wrapping CCSprite
gets the framerate back up to 60:
CCSprite *spback = [(CCSprite*)[CCSprite alloc] init];
[self addChild:spback];
CCSprite *sp = [CCSprite spriteWithFile:@"Background.png"];
sp.position = ccp(1024/2, 768/2);
[spback addChild:sp];
这要归功于 cocos2d 论坛上的 yaoligang.
Credits for this goes to yaoligang on the cocos2d forums.
这篇关于cocos2d如何快速绘制背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!