Game Development Reference
In-Depth Information
GameLayer* game = [GameLayer sharedGameLayer];
Ship* ship = game.defaultShip;
BulletCache* bulletCache = game.bulletCache;
if (fireButton.active && totalTime > nextShotTime)
{
nextShotTime = totalTime + 0.4f;
// Set the position, velocity and spriteframe before shooting
CGPoint shotPos = CGPointMake(ship.position.x + 45, ship.position.y - 19);
float spread = (CCRANDOM_0_1() - 0.5f) * 0.5f;
CGPoint velocity = CGPointMake(200, spread * 50);
[bulletCache shootBulletFrom:shotPos velocity:velocity frameName:@"bullet.png"];
}
// Allow faster shooting by quickly tapping the fire button.
if (fireButton.active == NO)
{
nextShotTime = 0;
}
CGPoint velocity = ccpMult(joystick.velocity, 7000 * delta);
ship.position = CGPointMake(ship.position.x + velocity.x * delta,
ship.position.y + velocity.y * delta);
}
This short refactoring session adds much-needed flexibility to shooting bullets. I'm
sure you can imagine how enemies can now use the very same code to shoot their own
bullets.
Let's Make Some Enemies
At this point, maybe you have only a fuzzy idea about what the enemies are, what they
do, and what their behavior will be. That's the thing with enemies—you never quite
know what they're up to.
Search Nedrilad ::




Custom Search