Game Development Reference
In-Depth Information
+(int) getSpawnFrequencyForEnemyType:(EnemyTypes)enemyType
{
NSAssert(enemyType < EnemyType_MAX, @"invalid enemy type");
NSNumber* number = [spawnFrequency objectAtIndex:enemyType];
return number.intValue;
}
You store the spawn frequency values for each type of enemy in a static spawnFre-
quency NSMutableArray . It's a static variable because the spawn frequency isn't
needed for each enemy but only for each enemy type. The first Enemy instance that
executes the initSpawnFrequency method will find that the spawnFrequency
NSMutableArray is nil and so initializes it.
Because an NSMutableArray can store only objects and not primitive data types
like integers, the values have to be wrapped into an NSNumber class using the num-
berWithInt initializer. I chose to use insertObject here instead of addOb-
ject because it not only ensures that the values will have the same index as the en-
emy type defined in the enum , but it also tells any other programmer looking at this
code that the index used has a meaning. In this case, the index is synonymous with the
enemy type. Although it's technically unnecessary to specify the index here, it helps to
show which value is used for which enemy type.
Because you're using ARC, you don't need to consider freeing the memory of the
spawnFrequency array. Its memory will be released when it's no longer used.
Which in the case of static variables is when the app shuts down.
Note This reminds me of a misconception about memory leaks—fact is, when
an iOS app is leaking memory, then less free memory is available for the app,
and that's all there is to it. But when an app terminates or is terminated by the
system, iOS frees all of the memory that was in use by the app, leaked or not.
This behavior is unlike desktop operating systems. Memory leaks in iOS apps
won't affect other apps or decrease available memory for other apps if the leak-
ing app was terminated.
The reason why rebooting your device helps free up memory is caused by either
memory leaks in iOS itself or iOS loading additional subsystems and spawning
new processes as part of the app's interaction with iOS.
Search Nedrilad ::




Custom Search