Jump to content

Changing states with box2d causes crash


Matthew Cooley
 Share

Recommended Posts

(this was originally, accidentally  posted in the Phaser 3 forum, sorry for the dupe)

 

I have the box2d plugin for Phaser (which is rad), but I've run into a problem when changing states. The problem being that the game crashes. I'm trying to determine if this is a bug in box2d or Phaser (in which case I will have to just modify the library myself which I'd rather not do), or if I am just doing something wrong.

 

The chain of events that I've been able to trace:

1. I change the state

2. Phaser calls clear on the physics. 

3. inside clear, a new box2d.b2World world is created: 

clear: function () {this.world = new box2d.b2World(this.gravity);

inside that function it tries to called Clone() on that gravity parameter...

box2d.b2World = function (gravity){... yadda yadda yadda ...this.m_gravity = gravity.Clone();

4. Crash

 

The only other time new box2d.b2World is ever called is thusly:

this.world = new box2d.b2World(new box2d.b2Vec2(0, 0)); 

But 'this.gravity' that is passed on clear() is not a b2Vec2, its a Phaser.Physics.Box2D.PointProxy object. I don't totally understand this object, but I see when its created it has a gravity getter and setter passed in. But either way, when Clone is called on this object it crashes. 

 

So my question is... is this a bug? Or is there a way around this?

Link to comment
Share on other sites

I don't suppose you have a minimal test case do you? I've got a new release of this plugin ready, which fixes a few issues in the docs and examples and adds in support for the new RUBE Loader, but if this is an as-yet unfound bug I'd like to fix it too.

Link to comment
Share on other sites

Hi guys,

 

I setup a minimal test case (well sort of) to show this problem. As you can see, there are 2 states; MainMenu and MyGame. When you press space button to switch from state MainMenu to MyGame the game crashes throwing an exception message (undefined is not a function). If you do not enable box2d physics system, everything works fine.

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /><title>Phaser Test</title><script type="text/javascript" src="phaser.min.js"></script><script type="text/javascript" src="box2d-plugin-full.js"></script>    <style type="text/css">        body {            margin: 0;            background-color: rgb(80,80,80);        }    </style></head><body>    <script type="text/javascript">     MainMenu = function(game) {         this.game = game;    };     MainMenu.prototype = {                create: function() {            console.log("MainMenu.create");             this.game.physics.startSystem(Phaser.Physics.BOX2D);        },          update: function() {            console.log("MainMenu.update");              if (this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) {                                this.game.state.start("MyGame");            }          },      };     MyGame = function(game) {       };     MyGame.prototype = {                create: function() {            console.log("MyGame.create");        },          update: function() {            console.log("MyGame.update");        },      };         var game = new Phaser.Game(800, 600, Phaser.AUTO, 'My Super Game');        game.state.add("MainMenu", MainMenu);    game.state.add("MyGame", MyGame);        game.state.start("MainMenu"); </script> </body></html>
Link to comment
Share on other sites

  • 2 months later...

Running into the same issue here, any updates since April? One can't even try to destroy the physics on shutdown because the destroy function also calls clear(). I tried explicitly setting gravity.x and gravity.y after calling startPhysics, hoping for some miracle to occur but unfortunately without success. Any workarounds, hacks or advice?

 

I'm super thankful for any input!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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