【问题标题】:Cocos2d performance issue on simple code简单代码上的 Cocos2d 性能问题
【发布时间】:2012-03-30 07:43:31
【问题描述】:

我正在编写一个瓷砖基础游戏,在我将瓷砖纹理和构建纹理加载到屏幕上后,我的帧速率从 60 下降到了 30。经过几天的谷歌搜索和尝试不同的解决方案,我仍然无法找出原因。

隔离和理解问题。我创建了一个简单的项目,它在加载程序时在随机位置显示 100 个精灵,之后在 HellowWorld 层中什么也不做:

// on "init" you need to initialize your instance
-(id) init
{
if( (self=[super init])) {

    CCTexture2D *vlo_Texture = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@"Icon-72.png"]];

    CCSprite *vlo_Temp[100];
    NSInteger vli_X, vli_Y;
    for (NSInteger vli_Counter=0; vli_Counter<100; vli_Counter++)
    {
        vli_X = arc4random()%480;
        vli_Y = arc4random()%320;
        vlo_Temp[vli_Counter] = [CCSprite spriteWithTexture:vlo_Texture]; //[CCSprite spriteWithFile:@"Icon-72.png"];
        vlo_Temp[vli_Counter].position = ccp(vli_X, vli_Y);
        [self addChild:vlo_Temp[vli_Counter]];
    }

}
return self;
}

上面的代码只运行 30fps 的事实让我很震惊。我没有计时器来刷新这些精灵。我相信系统应该只绘制所有精灵并且什么都不做(我假设?)

我曾尝试申请:

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA4444];

if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
    [CCDirector setDirectorType:kCCDirectorTypeMainLoop];

但是它们根本没有提高性能。还有什么我可以做的吗?

提前致谢。

亚历克斯

【问题讨论】:

  • 如果您说出您使用的操作系统版本和设备,它可能会帮助人们帮助您。

标签: iphone performance graphics cocos2d-iphone frame-rate


【解决方案1】:

你说:

经过几天的谷歌搜索和尝试不同的解决方案......

但是what have you tried?


尝试将所有使用相同纹理的精灵放入CCSpriteBatchNode。这样一来,这些精灵就可以通过一个 glBindTexture 调用来渲染,这个调用非常慢。

CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Icon-72.png"];
for (NSInteger vli_Counter=0; vli_Counter<100; vli_Counter++)
    {
        vli_X = arc4random()%480;
        vli_Y = arc4random()%320;
        vlo_Temp[vli_Counter] = [CCSprite spriteWithTexture:vlo_Texture]; //[CCSprite spriteWithFile:@"Icon-72.png"];
        vlo_Temp[vli_Counter].position = ccp(vli_X, vli_Y);
        [batchNode addChild:vlo_Temp[vli_Counter]];
    }

[self addChild batchNode];

【讨论】:

  • 谢谢詹姆斯,在我的程序中,我在单个文件中有精灵,在过去的几天里,我将其更改为精灵表,并尝试了 [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA4444] 之类的设置;和 if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] ) [CCDirector setDirectorType:kCCDirectorTypeMainLoop];
  • 将所有精灵添加到该精灵表中,并使用精灵表纹理作为批处理节点的参数。
  • 谢谢,我现在就试试批处理节点 :)
  • 我刚刚使用 Zwoptex 为此创建了一个 spritesheet 并使用了以下代码: [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Untitled_default.plist"]; CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Untitled_default.png"]; CCSprite *sprite1 = nil; for (NSInteger vli_Counter=0; vli_Counter
  • vli_X = arc4random()%480; vli_Y = arc4random()%320; sprite1.position = ccp(vli_X, vli_Y); [batchNode addChild:sprite1]; } [self addChild:batchNode];但是速度仍然是30fps,还有其他提示吗?谢谢
【解决方案2】:

谈到性能...您是否尝试过在设备上运行?模拟器与实际的“iSomething”性能相差甚远,尤其是在处理高清图片和众多精灵时。

【讨论】:

    【解决方案3】:

    要比较运行两个不同代码块所需的时间,我建议使用BNRTimeBlock,如this Big Nerd Racn Weblog post 所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多