Game Development Reference
In-Depth Information
reason:@"unhandled enemy type"
userInfo:nil];
}
self = [super initWithSpriteFrameName:enemyFrameName];
if (self)
{
// Create the game logic components
[self addChild:[StandardMoveComponent node]];
StandardShootComponent* shootComponent = [StandardShootComponent node];
shootComponent.shootFrequency = shootFrequency;
shootComponent.bulletFrameName = bulletFrameName;
[self addChild:shootComponent];
// enemies start invisible
self.visible = NO;
[self initSpawnFrequency];
}
return self;
}
+(id) enemyWithType:(EnemyTypes)enemyType
{
return [[self alloc] initWithType:enemyType];
}
The code begins by setting variables, depending on the enemy type, using a switch
statement to provide default values for each type of enemy—the sprite frame name to
use, the name of the bullet sprite frame, and the shooting frequency. The default case of
the switch statement throws an exception because that option usually results from
adding a new enemy type to the EnemyTypes enum without extending this switch
statement accordingly. Safeguarding your switch statements in this way so that no
default case will be accepted is a good strategy to avoid spending too much debugging
time on simple human errors. And you are a human being, right? So, you're prone to
forget these things. I know I am. Instead of wondering why your new enemy doesn't
Search Nedrilad ::




Custom Search