Game Development Reference
In-Depth Information
We override the init method to add the new setup method that creates a new emitter
and a new physics body. We set contactBitMask to playerBitmask , since we want to
be notified when a player sprite is colliding with the enemy sprite, and we don't want
enemies to collide with each other. We set categoryBitMask in order to identify the
object, collisionBitMask to zero since we don't want our engine to handle collision
for us, and we will do it manually. We also set affectedByGravity to NO as we don't
want our enemies to fall off the screen.
The next thing we need is power-ups. Create the ERGPowerup class (we will use this
class to represent objects that grant shields to the player character) and set ERGEnemy
as its superclass . We need to redefine the setup method in ERGPowerup.m as we
want them to do different things:
- (void) setup
{
self.emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:
[[NSBundle mainBundle] pathForResource:@"powerup"
ofType:@"sks"]];
self.emitter.name = @"shieldEmitter";
self.emitter.zPosition = 50;
[self addChild:self.emitter];
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
self.physicsBody.contactTestBitMask = playerBitmask;
self.physicsBody.categoryBitMask = shieldPowerupBitmask;
self.physicsBody.collisionBitMask = 0;
self.physicsBody.affectedByGravity = NO;
}
This is same as the previous thing, but we pick a different particle effect and another
categoryBitmask .
After this, we need to add enemies to our scene. First, import the ERGEnemy.h and
ERGPowerup.h files at the top of ERGMyScene.m . Second, we will need methods to
create enemies:
- (ERGEnemy *) spawnEnemy
{
ERGEnemy *temp = [[ERGEnemy alloc] init];
temp.name = @"enemy";
temp.position = CGPointMake(self.size.width + arc4random() % 800,
arc4random() % 240 + 40);
return temp;
}
 
Search WWH ::




Custom Search