This is all a test

Let’s make some bullets

June 8, 2015 by Corey


Hi everyone,

I’ve been fiddling around with the Phaser framework as I make the shooting mechanic for an action-platformer I’m working on.  I’m putting up a few functions for making bullets and shooting them. Enjoy!


//How to create bullets for your game!
//load your bullet sprite here
function preload() {
    game.load.image('bullet', 'Assets/images/bullet.png');
}

/*
This bit of code here creates the bullets. I lifted this from the Game Mechanics Explorer which is an excellent resource. The idea is to create a group of bullets, then fill it with dead or nonexistent bullets. You do this by filling it with live ammo, and destroying them. If we had live ammo lying around our game. Things could get out of hand. Later on we cast revive on a bullet and bring it into existence when we decide to shoot
*/

function createBullets1() {
    bulletPool = game.add.group();

    for (var i = 0; i < NUMBER_OF_BULLETS; i++) {
        var bullet = game.add.sprite(0, 0, 'bullet');
        bulletPool.add(bullet);
        bullet.anchor.setTo(0.5, 0.5);
        game.physics.arcade.enable(bullet);
        bullet.kill();
    }
}

/*
In this example the createMultiple function is the bread and butter.
Same idea: fill this group with dead ammo but this example
skips a step. Instead of filling the group up with live ammo
then killing it, it just creates the nonexistent ammo. And doesn't
this just look cleaner?
*/
function createBullets2() {

    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.createMultiple(NUMBER_OF_BULLETS, 'bullet', 0, false);
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.5);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);

}

/*
Now for our shoot function! The idea is to create
a bullet variable within this function, and fill it
with one of the bullets from our bulletPool group.
That's where .getFirstDead comes in. Next we "awaken"
the bullet with .revive, position the bullet next to our
character, and then make it fly.

*/
function fire() {
    //take a screen shot for when the last bullet was fired
    //and use that data to delay the next shot.

    if (lastBulletShotAt === undefined) lastBulletShotAt = 0;
    if (game.time.now - lastBulletShotAt < SHOT_DELAY) return;

    lastBulletShotAt = game.time.now;

    var bullet = bulletPool.getFirstDead();

    //if you can't find a bullet, don't shoot
    if (bullet === null || bullet === undefined) return;

    //cast revive on the bullet
    bullet.revive();

    //bullets die when they leave the world bounds
    //This bit of code isn't needed if you're using
    //the createBullets2 method.
    bullet.checkWorldBounds = true;
    bullet.outOfBoundsKill = true;

    //facing right
    if (player.frame === 2) {
        //set the bullet position to my right and fire
        bullet.reset(player.body.right, player.body.center.y);
        bullet.body.velocity.x = BULLET_SPEED;
        bullet.body.velocity.y = 0;
    }
    //facing left
    else if (player.frame === 0) {
        //set the bullet position to my left and fire
        bullet.reset(player.body.x, player.body.center.y);
        bullet.body.velocity.x = BULLET_SPEED * -1;
        bullet.body.velocity.y = 0;

    }

}

Conversation

Your email address will not be published. Required fields are marked *

*