【问题标题】:FPS (Frames per second) drop when many objects created创建许多对象时 FPS(每秒帧数)下降
【发布时间】:2012-04-23 09:02:08
【问题描述】:

我正在根据 Ray Wenderlich 的教程制作我的第一款游戏。当我尝试在屏幕上总共添加 50 个敌人时。 fps 从 60 下降到每秒 45 左右。你们能告诉我为什么吗?

-(void)createObjectOfType:(GameObjectType)objectType
           withHealth:(int)initialHealth 
           atLocation:(CGPoint)spawnLocation 
           withZValue:(int)ZValue               {

if (objectType == kEnemyTypeEvil) {

    Evil *evil = [[Evil alloc] initWithSpriteFrameName:@"evil_1.png"];
    [evil setCharacterHealth:initialHealth];
    [evil setPosition:spawnLocation];
    [objectBatchNode addChild:evil z:ZValue tag:kEvilTagValue];
    [evil setDelegate:self];
    [evil release];
}
else if (objectType == kEnemyTypeGhost){

    Ghost *ghost = [[Banana alloc] initWithSpriteFrameName:@"Ghost.png"];
    [ghost setCharacterHealth:initialHealth];
    [ghost setPosition:spawnLocation];
    [objectBatchNode addChild:ghost z:ZValue tag:kGhostTagValue];
    [ghost setDelegate:self];
    [ghost release];
}
}

然后下面的方法会以0.5s的时间间隔将敌人一个一个添加到屏幕上。屏幕中敌人的最大数量是 50,这远非很多(我猜)。

-(void)addStage1Enemies{
CGSize levelSize = [[GameManager sharedGameManager] getDimensionOfCurrentScene];
int spawnIndex = arc4random() % 4;
float spawnXpos;
float spawnYpos;

if (spawnIndex == 0){
    spawnXpos= arc4random() % (int)(levelSize.width+30.0f) - 15.0f;
    spawnYpos= levelSize.height +15.0f;
}
else if (spawnIndex == 1){
    spawnXpos= levelSize.width + 15.0f;
    spawnYpos= arc4random() % (int)(levelSize.height+30.0f) - 15.0f;
}
else if (spawnIndex == 2){
    spawnXpos= arc4random() % (int)(levelSize.width+30.0f) - 15.0f;
    spawnYpos= -15.0f;
}
else if (spawnIndex ==3){
    spawnXpos = -15.0f;
    spawnYpos = arc4random() % (int)(levelSize.width+30.0f) - 15.0f;
}


int enemyEliminated = [GameManager sharedGameManager].killCount;

if (enemyEliminated <50) {
    if (evilCount<50) {
        [self createObjectOfType:kEnemyTypeEvil
                      withHealth:kEvilHealth
                      atLocation:ccp(spawnXpos,spawnYpos)
                      withZValue:10];
    }
    evilCount++;
}
else if (enemyEliminated <100) {
    evilCount=0;
    if (ghostCount<50) {
        [self createObjectOfType:kEnemyTypeGhost
                      withHealth:kGhostHealth
                      atLocation:ccp(spawnXpos,spawnYpos)
                      withZValue:10];
    }
    ghostCount++;
}



}

此外,敌人会从一个位置到随机位置重复执行 CCMoveTo。导致帧率下降的可能原因是什么?我该如何解决这个问题?

我想尽快解决这个问题。非常非常非常非常感谢您!

------已编辑-------

我在 iPhone 4 设备上运行它。

我有一个 1960*1280 像素的大精灵作为我的背景。

基本上在更新循环中,我将每个精灵表中的所有对象更新为数组。这会是更新每帧上所有对象的问题吗?但就我而言,我只有 50 个敌人。

CCArray *listOfGameObjects = [objectBatchNode children];
CCArray *listOfGameObjects2 = [objectBatchNode_2 children];
CCArray *listOfGameObjects3 = [objectBatchNode_3 children];
CCArray *listOfBosses = [bossesBatchNode children];
CCArray *listOfBosses2 = [bossesBatchNode_2 children];
CCArray *listOfBosses3 = [bossesBatchNode_3 children];
CCArray *listOfBosses4 = [bossesBatchNode_4 children];


for (GameCharacter *tempChar in listOfGameObjects){
    [tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects];

}

for (GameCharacter *tempChar in listOfGameObjects2){
    [tempChar update2StateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects2];

}
for (GameCharacter *tempChar in listOfGameObjects3){
    [tempChar update3StateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects3];

}

for (GameCharacter *tempChar in listOfBosses){
    [tempChar updateBossesStateWithDeltaTime:deltaTime andListOfGameObjects:listOfBosses];

}
for (GameCharacter *tempChar in listOfBosses2){
    [tempChar updateBosses2StateWithDeltaTime:deltaTime andListOfGameObjects:listOfBosses2];

}
for (GameCharacter *tempChar in listOfBosses3){
    [tempChar updateBosses3StateWithDeltaTime:deltaTime andListOfGameObjects:listOfBosses3];

}
for (GameCharacter *tempChar in listOfBosses4){
    [tempChar updateBosses4StateWithDeltaTime:deltaTime andListOfGameObjects:listOfBosses4];

}

这有很大帮助吗?

谢谢大家!!!

【问题讨论】:

  • 首先:我不是游戏、opengl 或 cocos2d 专家。但是有两个问题:你为什么要追求如此高的帧速率(GLKit 的默认值为 30,我一直都很好)。我无法从您发布的代码中看出,您的应用在两帧之间究竟做了什么(更新和重新绘制)。
  • 你是在模拟器还是设备上运行这个?模拟器比真机慢很多。
  • 我已经编辑了原帖凯。你认为这会是问题吗?尼克,我在 iPhone4 上运行。

标签: iphone ios cocos2d-iphone frame-rate ccsprite


【解决方案1】:

FPS 会永远降到 45 吗?还是在添加敌人时只是卡顿了一会儿?如果是后者,您可能希望在场景开始时创建它们,然后相应地显示或不显示它们。

我必须看看你在所有这些 updateBossesStateWithDeltaTime 中做了什么才能知道是不是这样。

尝试删除部分代码,直到您知道是不是这样。例如,将你的敌人类从除初始化代码之外的所有内容中删除(创建它们,将它们的精灵添加到场景中),看看是否仅仅因为你降低了 fps。

如果只是添加它们会降低 fps,你应该尝试使用 spritesheets(如果你还没有这样做的话)。

巨大的背景可能会对 fps 造成影响,请尝试在不触及其他任何东西的情况下也将其移除,看看是否是原因。

【讨论】:

  • 在尝试逐段删除代码进行测试后,发现问题出在哪里!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多