Run Bob Run Weapon

In this version of the game you will add a weapon object so Bob can shoot at enemies.



Load State
Add this code to the load.js file at the beginning of the PreLoad function. This code will load our image data for our weapons bullets and explosion.



    var bulletData = [
    '...00...',
    '..0330..',
    '.038830.',
    '03888830',
    '03888830',
    '.038830.',
    '..0330..',
    '...00...'
    ];
    game.bmpBullet = game.create.texture('bullet', bulletData, 2, 2, 0, false);

    var particleData = [
    '.BB.',
    'BBBB',
    'BBBB',
    '.BB.'
    ];
    game.bmpParticle = game.create.texture('particle', particleData, 1, 1, 0, false);


        



Play Level 1 State - MAP
In the playlevel1.js file, replace the map in the PRELOAD function with one below. Since Bob now has a weapon, he is going to need alot of enemies to kill.



    // MAP - level 1 - change this to make ur own level.
    // map is setup as an array. the array has 6 rows.  each row has over 100 data points and can be as long as u want.
    // o = ground (stand on it)
    // F = flag (ends the game)
    // X = enemy (u die if u touch it)
    // H = heart (gets you points)
    // T = tree (does nothing)
    game.map = [
    '.............................XXXXX.',
    '...................................',
    '.........X.........................',
    '...................................',
    '.......................X.......X...',
    '.......................X.......XX..',
    '............X...........H..........',
    '...................o..o.X....X....F',
    '............X.X...o......H........X',
    '.............X.H.....X..oooo..X.X.o',
    '......H....X.....o.....o....oooooo.',
    '.....o......Xo..X.X..X.o...........',
    'oooooooooooooooooooooooo...........'
    ];





Add a Phaser Weapon object
Add this code to the end of the CREATE function. This code will add a weapon object to our game.




    // bob's weapon  - 
    // ----------------------------------------------------

    //  Add a built-in Phaser Engine Weapon object to the game
    //  weapon(30 means creates 30 bullets, bmpBullet is our 'bullet' image
    this.weapon = game.add.weapon(30, game.bmpBullet);

    //  The bullet will be automatically killed when it leaves the world bounds
    this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;

    //  The speed at which the bullet is fired
    this.weapon.bulletSpeed = 600;

    //  Speed-up the rate of fire, allowing player to shoot 1 bullet every 60ms
    this.weapon.fireRate = 100;

    //  Tell the Weapon to track the 'player' Sprite (bob)
    //  With no offsets from the position
    //  false means the weapon will not follow the players rotation
    this.weapon.trackSprite(this.bob, 0, 0, false);





Fire Button
In the file playlevel1.js , replace the makeBobMove function with the code below. This code will add a new key capture Left and Right arrows.



    // code to make bob move
    makeBobMove: function() {

        // check if user has pressed any keys
        if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
        {

        // check if bob is sitting on an immovable object
        // onTheGround = true or false
        this.onTheGround = this.bob.body.touching.down;

        // bob can only jump if he is currently no the ground
        if (this.onTheGround)
        {
        // make bob jump
        this.bob.body.velocity.y = -900;
        this.bob.body.velocity.x = 0;
        this.bob.play('jump');
        }

        } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
        {
        this.weapon.fireAngle = Phaser.ANGLE_RIGHT;
        this.weapon.fire();

        } else if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
        {

        this.weapon.fireAngle = Phaser.ANGLE_LEFT;
        this.weapon.fire();

        } else if (game.input.keyboard.isDown(Phaser.Keyboard.A))
        {

        this.bob.body.velocity.x = -200;
        this.bob.play('left');

        } else if (game.input.keyboard.isDown(Phaser.Keyboard.D))
        {

        this.bob.body.velocity.x = 200;
        this.bob.play('right');

        } else
        {

        // bobs not moving, so make him stand still
        this.bob.body.velocity.x = 0;
        this.bob.play('stand');

        }

    },
        



Run Game and Check for Errors
Press F12 to check console for any problems. You should be able to fire at will.




Detect when bullet collides with enemy or wall
Inside the playlevel1.js add the following lines of code right after makeBobMove function. This code will tell our game what to do when a bullet collides with a wall or player.



    enemyKilled: function(bullet, enemy) {

        // create an explosion by creating a phaser emitter object
        this.emitter = game.add.emitter(0,0,100);

        // tell the emitter what the explosion particle will look like
        this.emitter.makeParticles(game.bmpParticle);

        // give the particles some gravity
        this.emitter.gravity = 200;

        // where is this explosion to take place?
        this.emitter.x = bullet.body.x;
        this.emitter.y = bullet.body.y;

        // start explosion
        // emitter.start(true=all particles emitted at once, 2000ms particle lifespan, ignore, nbr particles)
        this.emitter.start(true, 2000, null, 10);


        // remove enemy
        enemy.kill();

        // add points
        game.score = game.score + 500;


    },

    killBullet: function(bullet, block) {

        bullet.kill();

    },

    heartTouched: function(bob, heart) {

        heart.kill();

        game.score = game.score + 1000;

    },





Check for collisions
Add this code to the playlevel1.js file's UPDATE function, right after the other collision/overlap lines of code.



    // did bullet hit enemy?
    game.physics.arcade.overlap(this.weapon.bullets, this.enemys, this.enemyKilled);
    game.physics.arcade.collide(this.weapon.bullets, this.blocks, this.killBullet);


        



Run Game and Check for Errors
Press F12 to check console for any problems. Play It! can you make your own bullets?



code on plnkr