Jump to content

How to code the player shooting right and left at the same time?????


khleug35
 Share

Recommended Posts

Hello everyone, I would like to create a player, when user press spacebar ,the player is shooting right and left at the same time.

But I don't know is it a stupid way to code this??

The full code and eample , spacebar is shooting button, Is it ???

https://jsfiddle.net/8q95wdv1/

a.png.0090fc39e3aa793c057c8d2f32081fb1.png

Create Two Group

//right shoot 
    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
 
    bullets.createMultiple(40, 'bullet');
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.5);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);
 //left shoot       
    bullets2 = game.add.group();
    bullets2.enableBody = true;
    bullets2.physicsBodyType = Phaser.Physics.ARCADE;
    bullets2.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;

 

function fire() {
  if (game.time.now > bulletTime)
    {
        bullet = bullets.getFirstExists(false);
        bullet2 = bullets2.getFirstExists(false);
        
        if (bullet && bullet2)
        {
            //right
            bullet.reset(player.body.x, player.body.y );
            bullet.lifespan = 2000;
            bullet.body.velocity.x = 1000;
            
            //left
            bullet2.reset(player.body.x, player.body.y );
            bullet2.lifespan = 2000;
            bullet2.body.velocity.x = -1000;
            
            bulletTime = game.time.now + 250;
        }     
        
    }
}

 


var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render:render});

var player;
var cursors;
var shootButton;
var bulletTime = 0;  
function preload() {

    game.stage.backgroundColor = '#ffffff';

    game.load.baseURL = 'http://examples.phaser.io/assets/';
    game.load.crossOrigin = 'anonymous';

    game.load.image('player', 'sprites/phaser-dude.png');
    game.load.image('platform', 'sprites/platform.png');
    game.load.image('bullet', 'games/invaders/enemy-bullet.png');
}

function create() {

    player = game.add.sprite(350, 200, 'player');

    game.physics.arcade.enable(player);

    player.body.collideWorldBounds = true;


    cursors = game.input.keyboard.createCursorKeys();
    shootButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    player.customParams = {};
    
    
   //right shoot 
    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
 
    bullets.createMultiple(40, 'bullet');
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.5);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);
 //left shoot       
    bullets2 = game.add.group();
    bullets2.enableBody = true;
    bullets2.physicsBodyType = Phaser.Physics.ARCADE;
    bullets2.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
 
    bullets2.createMultiple(40, 'bullet');
    bullets2.setAll('anchor.x', 0.5);
    bullets2.setAll('anchor.y', 0.5);
    bullets2.setAll('outOfBoundsKill', true);
    bullets2.setAll('checkWorldBounds', true);
}

function update () {
 
    game.physics.arcade.collide(player);
    
    player.body.velocity.x = 0;

    if (cursors.left.isDown || player.customParams.isMovingLeft)
    {
        player.body.velocity.x = -250;
    }
    if (cursors.right.isDown || player.customParams.isMovingRight)
    {
        player.body.velocity.x = 250;
    }

    if (shootButton.isDown)
    {
       fire();
    }
 

}

function fire() {
  if (game.time.now > bulletTime)
    {
        bullet = bullets.getFirstExists(false);
        bullet2 = bullets2.getFirstExists(false);
        
        if (bullet && bullet2)
        {
            //right
            bullet.reset(player.body.x, player.body.y );
            bullet.lifespan = 2000;
            bullet.body.velocity.x = 1000;
            
            //left
            bullet2.reset(player.body.x, player.body.y );
            bullet2.lifespan = 2000;
            bullet2.body.velocity.x = -1000;
            
            bulletTime = game.time.now + 250;
        }     
        
    }
}

function render() {

}

 

Thank you very much!!!!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...