Game Development Reference
In-Depth Information
This method is straightforward, but for the arc4random() function that returns a
random number, and if we perform a modulo operation on it with certain numbers,
we will get a random number from 0 to the previous number of that number. This is
useful, since we want our enemies to spawn somewhat randomly. They spawn off
screen. We will need the same method to spawn power-ups:
- (ERGPowerup *) spawnPowerup
{
ERGPowerup *temp = [[ERGPowerup alloc] init];
temp.name = @"shieldPowerup";
temp.position = CGPointMake(self.size.width + arc4random() % 100,
arc4random() % 240 + 40);
return temp;
}
In order to move them, add the following code to the update method:
[self enumerateChildNodesWithName:@"enemy" usingBlock:^(SKNode
*node, BOOL *stop) {
ERGEnemy *enemy = (ERGEnemy *)node;
enemy.position = CGPointMake(enemy.position.x -
backgroundMoveSpeed * timeSinceLast, enemy.position.y);
if (enemy.position.x < -200) {
enemy.position = CGPointMake(self.size.width +
arc4random() % 800, arc4random() % 240 + 40);
enemy.hidden = NO;
}
}];
[self enumerateChildNodesWithName:@"shieldPowerup"
usingBlock:^(SKNode *node, BOOL *stop) {
ERGPowerup *shield = (ERGPowerup *)node;
shield.position = CGPointMake(shield.position.x -
backgroundMoveSpeed * timeSinceLast, shield.position.y);
if (shield.position.x < -200) {
shield.position = CGPointMake(self.size.width +
arc4random() % 100, arc4random() % 240 + 40);
shield.hidden = NO;
}
}];
 
Search WWH ::




Custom Search