matts444 Posted August 25, 2015 Share Posted August 25, 2015 I have an issue where the gravity value doesnt change when switching between states e.g. between levels. im sure this is an issue with the plugin as the value of this.game.physics.box2d.gravity.y is changing, but the gravity change is having no effect on any of the objects. this only works the first time I load the state, when I start a new state with a new gravity.y with even a minus gravity value it has no different effect. Any advice would be greatly appreciated. Link to comment Share on other sites More sharing options...
rich Posted August 25, 2015 Share Posted August 25, 2015 Not entirely sure I understand - do you mean in your first state you set Gravity, and it works fine. But then in another State the new gravity value isn't set, and setting it again does nothing? Link to comment Share on other sites More sharing options...
matts444 Posted August 26, 2015 Author Share Posted August 26, 2015 Sorry. yes in the first state the gravity is set to 'this.game.physics.box2d.gravity.y = 100'. In this state I change the value of the gravity on button click(anti-gravity button) to -100. Then if I click the restart button which calls 'this.game.state.restart()', the anti gravity button changes the value of the gravity still(from 100 to -100) but the gravity change has no physical effect on the objects. Hope this is clearer cheers for the reply! Link to comment Share on other sites More sharing options...
matts444 Posted August 26, 2015 Author Share Posted August 26, 2015 I created an example of what i mean in the gravity scale example file. I used the ball sprite as the button, left for restart state and right to change the gravity. If you run it and click the right button to change the gravity, then click the left button to restart the state and you'll see that the gravity is still at -300. <!doctype html><html> <head> <meta charset="UTF-8" /> <title>Phaser Box2D Example</title> <script src="js/phaser-arcade-physics.min.js" type="text/javascript"></script> <script src="js/box2d-plugin-full.min.js" type="text/javascript"></script> </head> <body> <script type="text/javascript"> var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create }); function preload() { game.load.image('ball', 'assets/sprites/shinyball.png'); } function create() { game.stage.backgroundColor = '#124184'; // Enable Box2D physics game.physics.startSystem(Phaser.Physics.BOX2D); game.physics.box2d.setBoundsToWorld(); game.physics.box2d.gravity.y = 300; game.physics.box2d.restitution = 0.8; var sprite1 = game.add.sprite(200, 100, 'ball'); var sprite2 = game.add.sprite(400, 100, 'ball'); var sprite3 = game.add.sprite(600, 100, 'ball'); var button = game.add.button(300, 300, 'ball', restart, this); var button = game.add.button(500, 300, 'ball', gravity, this); // Enable for physics. This creates a default rectangular body. game.physics.box2d.enable([ sprite1, sprite2, sprite3 ]); // Adjust the gravity scale sprite1.body.gravityScale = 1; sprite2.body.gravityScale = 0.5; sprite3.body.gravityScale = 0.25; game.add.text(5, 5, 'Different gravity scales for bodies.', { fill: '#ffffff', font: '14pt Arial' });} function restart() { game.state.restart();} function gravity() { game.physics.box2d.gravity.y = -300; } </script> </body></html> Link to comment Share on other sites More sharing options...
matts444 Posted August 27, 2015 Author Share Posted August 27, 2015 resolved this by changing the gravityscale to a minus number, rather than changing the value of the physics.box2d.gravity.y Link to comment Share on other sites More sharing options...
eddieone Posted August 12, 2016 Share Posted August 12, 2016 I'm having this issue as well. GravityScale won't work where only one axis needs to be changed. game.physics.box2d.gravity.x = map.wind; How to change the gravity after it has been set once? Link to comment Share on other sites More sharing options...
Tom Atom Posted August 13, 2016 Share Posted August 13, 2016 try this method: game.physics.box2d.world.SetGravity(); Link to comment Share on other sites More sharing options...
eddieone Posted August 15, 2016 Share Posted August 15, 2016 Hi Tom, I'm not sure why but today it's working. I think maybe I was editing deploy files, so nothing was changing locally. I tried what you said and it also worked. Though the scale seems to be different by a factor of -50 //game.physics.box2d.gravity.y = map.gravity; //game.physics.box2d.gravity.x = map.wind; game.physics.box2d.world.SetGravity({x: (map.wind / 50) * -1, y: (map.gravity / 50) * -1}); Link to comment Share on other sites More sharing options...
eddieone Posted September 12, 2016 Share Posted September 12, 2016 I did more testing and there certainly is a bug in the Phaser box2d plugin when setting gravity x and y. Some times it does work and others not. I ended up with something like the following and it seems to work. ? var gravityX = (map.wind / game.physics.box2d.ptmRatio) * -1; var gravityY = (map.gravity / game.physics.box2d.ptmRatio) * -1 game.physics.box2d.world.SetGravity({x: gravityX, y: gravityY}); Link to comment Share on other sites More sharing options...
Recommended Posts