Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

How to not apply gravity to weapon?


PixelProgrammer
 Share

Recommended Posts

Hi there,

I'm creating a simple platform game where you can shoot enemies. I've applied 

game.physics.arcade.gravity.y = 1000;

But this seems to now affect my weapon class and bullets too! 

How do I set my weapons or bullets to NOT be affected by gravity?

 

Here's the code I've used for reference:

var playState = {
    //code to make assets goes here
    create: function(){
        /*-- creating player --*/
        game.add.sprite(0, 0, 'background');
        game.physics.arcade.gravity.y = 1000; //Do not enable, need to fix gravity.
        this.player = game.add.sprite(game.width/2, 780, 'player');
        this.player.anchor.setTo(0.5, 0.5);
        game.physics.arcade.enable(this.player);
        this.player.body.gravity.y = 1500;
        this.player.body.bounce.set(0.3);
        this.player.body.collideWorldBounds = true;
        this.player.cursor = game.input.keyboard.createCursorKeys();
        this.player.fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
        this.player.hp = 100;
        this.player.weapon = game.add.weapon(30, 'bullet'); //
        this.player.weapon.enableBody = true;
        this.player.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
        this.player.weapon.bullets.setAll('body.gravity.y', 0);
        this.player.weapon.bulletSpeed = 850;
        this.player.weapon.fireRate = 200;
        this.player.weapon.trackSprite(this.player);
Link to comment
Share on other sites

2 hours ago, PixelProgrammer said:

@PhasedEvolution Yes, I could apply gravity to a sprite body. However, i'm using the Phaser.Weapon class. And if I try `weapon.body.gravity.y = 0;` I just get an error saying "Cannot read property 'gravity' of undefined". Same thing for weapon.bullets

For a group then? Run a for loop / .each to each element of the group then

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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