Jump to content

Fire Weapon Based Off Sprite's Direction


willdta
 Share

Recommended Posts

I'm trying to shoot a weapon in whatever direction the character is facing. Through looking around, I've found some examples using fireAngle but I'm not sure how to incorporate it to work with my code.

Here is my character move function along with a weapon fire on spacebar. Where should I put the fireAngle to work correctly?

Thanks

player.body.velocity.x = 0;
player.body.velocity.y = 0;
 
if (cursors.left.isDown) {
player.body.velocity.x = -300;
player.animations.play('left')
}
 
else if (cursors.right.isDown) {
player.body.velocity.x = 300;
player.animations.play('right')
}
 
else if (cursors.up.isDown) {
player.body.velocity.y = -300;
player.animations.play('up')
}
 
else if (cursors.down.isDown) {
player.body.velocity.y = 300;
player.animations.play('down')
}
 
if (fireButton.isDown) {
weapon.fireAngle = Phaser.ANGLE_LEFT;
weapon.fireAngle = Phaser.ANGLE_RIGHT;
weapon.fireAngle = Phaser.ANGLE_UP;
weapon.fireAngle = Phaser.ANGLE_DOWN;
weapon.fire();
}
Link to comment
Share on other sites

1 hour ago, Amberalex said:

I recommend use trackSprite for rotation of weapon's bullet, for example:


weapon = game.add.weapon(10, 'bullet');
weapon.trackSprite(player, 0, 0, true);

It's seems the easiest way to solve the problem.

I have that set, unfortunately it doesn't work

Link to comment
Share on other sites

Hi @willdta,

You are assigning a value 4 times in a row the same variable (weapon.fireAngle) , but only the last value will be assigned (Phaser.ANGLE_DOWN).

Try this:

player.body.velocity.x = 0;
player.body.velocity.y = 0;
 
if (cursors.left.isDown) {
player.body.velocity.x = -300;
player.animations.play('left');
weapon.fireAngle = Phaser.ANGLE_LEFT;
}
 
else if (cursors.right.isDown) {
player.body.velocity.x = 300;
player.animations.play('right');
weapon.fireAngle = Phaser.ANGLE_RIGHT;
}
 
else if (cursors.up.isDown) {
player.body.velocity.y = -300;
player.animations.play('up');
weapon.fireAngle = Phaser.ANGLE_UP;
}
 
else if (cursors.down.isDown) {
player.body.velocity.y = 300;
player.animations.play('down');
weapon.fireAngle = Phaser.ANGLE_DOWN;
}
 
if (fireButton.isDown)
weapon.fire();

 

Link to comment
Share on other sites

1 hour ago, onlycape said:

Hi @willdta,

You are assigning a value 4 times in a row the same variable (weapon.fireAngle) , but only the last value will be assigned (Phaser.ANGLE_DOWN).

Try this:


player.body.velocity.x = 0;
player.body.velocity.y = 0;
 
if (cursors.left.isDown) {
player.body.velocity.x = -300;
player.animations.play('left');
weapon.fireAngle = Phaser.ANGLE_LEFT;
}
 
else if (cursors.right.isDown) {
player.body.velocity.x = 300;
player.animations.play('right');
weapon.fireAngle = Phaser.ANGLE_RIGHT;
}
 
else if (cursors.up.isDown) {
player.body.velocity.y = -300;
player.animations.play('up');
weapon.fireAngle = Phaser.ANGLE_UP;
}
 
else if (cursors.down.isDown) {
player.body.velocity.y = 300;
player.animations.play('down');
weapon.fireAngle = Phaser.ANGLE_DOWN;
}
 
if (fireButton.isDown)
weapon.fire();

 

Appreciate it! However it doesn't work :(

Link to comment
Share on other sites

I have tried the code modifying some detail of the asteroids example in the Phaser Sandbox and I think I have found the problem:

/**
 * Generated from the Phaser Sandbox
 *
 * //phaser.io/sandbox/vtUxaTwH
 *
 * This source requires Phaser 2.6.2
 */

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

function preload() {

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

    game.load.image('phaser', 'sprites/phaser-dude.png');

}


var weapon;
var cursors;
var fireButton;
var sprite;


function create() {

    sprite = game.add.sprite(0, 0, 'phaser');
    
    //  Creates 10 bullets, using the 'phaser' graphic
    weapon = game.add.weapon(10, 'phaser');

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

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

    //  Speed-up the rate of fire
    weapon.fireRate = 50;
    
    // IF SET TRUE THE BULLETS ALLWAYS GO TO THE RIGHT
    weapon.trackSprite(sprite, 0, 0, false);

    game.physics.arcade.enable(sprite);

    sprite.body.drag.set(70);
    sprite.body.maxVelocity.set(200);

    cursors = this.input.keyboard.createCursorKeys();

    fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);

}

function update() {
    
    
    if (cursors.left.isDown)
    {
        sprite.body.velocity.x = -300;
        weapon.fireAngle = Phaser.ANGLE_LEFT;
    }
    else if (cursors.right.isDown)
    {
        sprite.body.velocity.x = 300;
        weapon.fireAngle = Phaser.ANGLE_RIGHT;
    }
    else if (cursors.up.isDown)
    {
        sprite.body.velocity.y = -300;
        weapon.fireAngle = Phaser.ANGLE_UP;
    }
    else if(cursors.down.isDown)
    {
        sprite.body.velocity.y = 300;
        weapon.fireAngle = Phaser.ANGLE_DOWN;
    }
    
    
    
    if (fireButton.isDown) {
        weapon.fire();
    }

    game.world.wrap(sprite, 16);

}

You can try here: http://phaser.io/sandbox/vtUxaTwH/play

The key was the next line:

weapon.trackSprite(sprite, 0, 0, false);  (with "true" the code don´t work like you want, because the sprite don´t change the angle).

Regards.

Link to comment
Share on other sites

7 hours ago, onlycape said:

I have tried the code modifying some detail of the asteroids example in the Phaser Sandbox and I think I have found the problem:


/**
 * Generated from the Phaser Sandbox
 *
 * //phaser.io/sandbox/vtUxaTwH
 *
 * This source requires Phaser 2.6.2
 */

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

function preload() {

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

    game.load.image('phaser', 'sprites/phaser-dude.png');

}


var weapon;
var cursors;
var fireButton;
var sprite;


function create() {

    sprite = game.add.sprite(0, 0, 'phaser');
    
    //  Creates 10 bullets, using the 'phaser' graphic
    weapon = game.add.weapon(10, 'phaser');

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

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

    //  Speed-up the rate of fire
    weapon.fireRate = 50;
    
    // IF SET TRUE THE BULLETS ALLWAYS GO TO THE RIGHT
    weapon.trackSprite(sprite, 0, 0, false);

    game.physics.arcade.enable(sprite);

    sprite.body.drag.set(70);
    sprite.body.maxVelocity.set(200);

    cursors = this.input.keyboard.createCursorKeys();

    fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);

}

function update() {
    
    
    if (cursors.left.isDown)
    {
        sprite.body.velocity.x = -300;
        weapon.fireAngle = Phaser.ANGLE_LEFT;
    }
    else if (cursors.right.isDown)
    {
        sprite.body.velocity.x = 300;
        weapon.fireAngle = Phaser.ANGLE_RIGHT;
    }
    else if (cursors.up.isDown)
    {
        sprite.body.velocity.y = -300;
        weapon.fireAngle = Phaser.ANGLE_UP;
    }
    else if(cursors.down.isDown)
    {
        sprite.body.velocity.y = 300;
        weapon.fireAngle = Phaser.ANGLE_DOWN;
    }
    
    
    
    if (fireButton.isDown) {
        weapon.fire();
    }

    game.world.wrap(sprite, 16);

}

You can try here: http://phaser.io/sandbox/vtUxaTwH/play

The key was the next line:

weapon.trackSprite(sprite, 0, 0, false);  (with "true" the code don´t work like you want, because the sprite don´t change the angle).

Regards.

Thanks a lot mate! Works like a charm.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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