PixelProgrammer Posted January 28, 2017 Share Posted January 28, 2017 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 More sharing options...
PhasedEvolution Posted January 28, 2017 Share Posted January 28, 2017 You can apply the gravity to a specific sprite/s. Example: sprite.body.gravity.y = 1000; I suppose that if you want a global gravity of 1000 and you want to some elements to don't have gravity effect you could do as you did and just specify their gravity to 0 using the above code. Link to comment Share on other sites More sharing options...
PixelProgrammer Posted January 28, 2017 Author Share Posted January 28, 2017 @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 Link to comment Share on other sites More sharing options...
PhasedEvolution Posted January 28, 2017 Share Posted January 28, 2017 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 More sharing options...
samme Posted January 28, 2017 Share Posted January 28, 2017 weapon.bullets.setAll('body.gravity.y', 0) Link to comment Share on other sites More sharing options...
PixelProgrammer Posted January 28, 2017 Author Share Posted January 28, 2017 1 hour ago, samme said: weapon.bullets.setAll('body.gravity.y', 0) This doesnt seem to work either Link to comment Share on other sites More sharing options...
samme Posted January 28, 2017 Share Posted January 28, 2017 Oops, there is already `bulletGravity`: weapon.bulletGravity.y = 0; Link to comment Share on other sites More sharing options...
PixelProgrammer Posted January 28, 2017 Author Share Posted January 28, 2017 2 minutes ago, samme said: Oops, there is already `bulletGravity`: weapon.bulletGravity.y = 0; Awesome, this seems to work with some slight modifications. I had previously set world gravity to 1000. All i had to do was weapon.bulletGravity.y = -1000; Link to comment Share on other sites More sharing options...
Recommended Posts