1

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.5 秒間隔でスケジュールされ、敵を 1 つずつ画面に追加します。画面内の敵の最大数は 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];

}

これは大いに役立ちますか?

君たちありがとう!!!

4

1 に答える 1

0

FPS は永遠に 45 まで下がりますか? それとも、敵を追加するときにしばらく途切れますか? 後者の場合は、シーンの最初にそれらを作成し、それに応じて表示するかどうかを指定できます。

これらすべての updateBossesStateWithDeltaTime の中で何をしているかを確認して、それを確認する必要があります。

それがそれであるかどうかがわかるまで、コードの一部を削除してみてください。たとえば、敵のクラスを初期化コード以外のすべてから取り除き (敵を作成し、スプライトをシーンに追加します)、それだけで fps が下がるかどうかを確認します。

それらを追加するだけでそのように fps が低下する場合は、スプライトシートを使用してみてください (まだ使用していない場合)。

巨大な背景が fps に負担をかけている可能性があります。他に何も触れずに背景も削除してみて、それが原因かどうかを確認してください。

于 2012-04-23T13:21:43.410 に答える