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

时间:2012-04-23 09:02:08

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

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

-(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.5秒的间隔安排以下方法将敌人逐个添加到屏幕。屏幕上的最大敌人数量是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像素的大精灵作为我的背景。

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

}

这有用吗?

谢谢你们!!!

1 个答案:

答案 0 :(得分:0)

FPS会永远降到45吗?或者在添加敌人时它只是口吃了一会儿?如果是后一种情况,您可能希望在场景的开头创建它们,并相应地显示它们。

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

尝试删除部分代码,直到您知道是不是这样。例如,除了初始化代码(创建它们,将它们的精灵添加到场景中)之外,将你的敌人类除去一切,然后看看是否只是你的fps了。

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

巨大的背景可能会对fps产生影响,尝试将其移除而不触及其他任何东西,看看是否是原因。