当许多对象创建时,FPS(帧每秒)下降

问题描述:

我正在根据Ray Wenderlich的教程制作我的第一款游戏。当我尝试在屏幕上总共添加50个敌人时。 fps下降到每秒45个而不是60个。你们能告诉我为什么吗?当许多对象创建时,FPS(帧每秒)下降

-(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像素的大精灵作为我的背景。

基本上在更新循环中,我将每个spritesheet中的所有对象更新为数组。这会是更新每个帧上所有对象的问题吗?但在我的情况下,我只有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]; 

} 

这对您有帮助吗?

谢谢你们!

+0

第一:我是除游戏或opengl或cocos2d专家之外的其他东西。但有两个问题:为什么你要这么高的帧速率(GLKit的默认值是30,我一直很好)。我无法从您发布的代码中知道您的应用程序在两个帧之间做了什么(更新和重新绘制)。 – 2012-04-23 09:19:10

+0

你在模拟器或设备上运行它吗?模拟器比真实设备慢得多。 – 2012-04-23 09:42:46

+0

我编辑了原帖后启。你认为那会是问题吗?尼克,我正在使用iPhone4。 – rickylai 2012-04-23 10:01:22

FPS永远降到45吗?还是只是在添加敌人时口吃了一段时间?如果后面的情况是这样,你可能想在场景开始时创建它们,并且相应地显示它们。

我将不得不看看你在做什么updateBossesStateWithDeltaTime知道这是否是。

尝试删除部分代码,直到您知道是否是它。例如,除了init代码之外,将你的敌人类剥离出来(创建它们,将它们的精灵添加到场景中),然后看看是否只有你获得了fps。

如果只是添加它们可以减少这样的fps,你应该尝试使用spritesheets(如果你还没有这样做)。

巨大的背景可能会对fps产生影响,尝试删除它也没有触及其他任何东西,看看是否是原因。

+0

尝试删除部分代码部分进行测试后,我发现问题出在哪里!谢谢! – rickylai 2012-04-23 14:29:13