Jump to content

Changing state with box2d - gravity


matts444
 Share

Recommended Posts

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.


 


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 ]); 

   

}

 

function restart() {

    game.state.restart();

}

 

function gravity() {

    game.physics.box2d.gravity.y = -300;

    

}

        </script>

 

    </body>

</html>

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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