如何在cocos2d中快速绘制背景? [英] How to draw a background fast in cocos2d?

查看:127
本文介绍了如何在cocos2d中快速绘制背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用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: &params];

如果我删除背景,我得到一个固体的60fps。

If I remove the background I get a solid 60fps.

我已经尝试将图像转换为PVRTC,并且提供了额外的fps或两个。我得到相同的帧率使用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];

这项功劳可用于 yaoligang在cocos2d论坛

这篇关于如何在cocos2d中快速绘制背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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