gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:34:21 29 4
gpt4 key购买 nike

我正在根据 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.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 像素的大 Sprite 作为我的背景。

基本上在更新循环中,我将每个 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];

}

这有什么帮助吗?

谢谢大家!!!

最佳答案

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

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

尝试删除部分代码,直到您知道是否是这样。例如,将你的敌人类从除初始化代码以外的所有内容中删除(创建它们,将它们的 Sprite 添加到场景中),然后看看你是否只用它就可以降低你的 fps。

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

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

关于iphone - 创建许多对象时 FPS(每秒帧数)下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277581/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com