0

我有以下代码块

-(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?

4

1 回答 1

0

我很笨,我才意识到我没有将 for 循环对象类型从 NSDictionary 更新为 NSMutableDictionary,无法更改 NSDict 中的值,呵呵!

于 2012-11-03T07:04:26.480 回答