我正在根据 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];
}
这有多大帮助?
谢谢你们!!!