我有以下代码块
-(void) spawnEnemies:(ccTime)dt
{
static float lastSpawn;
lastSpawn += dt;
if (lastSpawn > 0.75) {
for (NSDictionary *enemyData in levelData.enemies) {
if ([[enemyData valueForKey:@"spawnCount"] intValue] < [[enemyData valueForKey:@"quantity"] intValue]) {
// reset spawn timer
lastSpawn = 0;
// init enemy
id enemy = [NSClassFromString([enemyData valueForKey:@"className"]) enemyInit];
[enemy startAnimating];
[enemy setSpawnPosition];
[enemy moveToSpriteInit:playerSprite];
[self addChild:enemy];
[enemy release];
NSNumber *spawnCount = [enemyData valueForKey:@"spawnCount"];
[enemyData setValue:[NSNumber numberWithInt:([spawnCount intValue] + 1)] forKey:@"spawnCount"];
}
}
}
}
我想知道为什么这条线:
[enemyData setValue:[NSNumber numberWithInt:([spawnCount intValue] + 1)] forKey:@"spawnCount"];
是否引发了错误的访问错误?我正在尝试每次迭代更新每个敌人的生成计数。我是否需要在 for 循环中重建 levelData.enemies 数组,并在 for 循环完成后将具有更新的生成计数值的新数组重新分配给 levelData.enemies?