Jump to content

[P2] StartSystem : issue when initialised in another state


pandavigoureux29
 Share

Recommended Posts

Hi all.

 

I've been encountering this issue since some time now : 

 

I know how to initialise P2 Physics 

game.physics.startSystem(Phaser.Physics.P2JS);

When I do this in the create of a State, everything is okay, I can enable bodies on sprites and set my callback events ( onBeginContact ...). I also listen to the broadphase callback and this is working.

 

The thing is, if I move the code above to another State create() function, or in the Game create() function, I lose all my events, the physics seems weird, and the broadphase callback setting trigger an error ( about pairs parent )

 

Do I have to start a different physics system for each State? 

That seems weird to me.

 

Thanks

Link to comment
Share on other sites

I do so, this is my main state.

var Play = function (){};Play.prototype = {preload:function(){},create:function(){game.physics.startSystem(Phaser.Physics.P2JS);enemigo.create();jugador.create();},update:function(){enemigo.update();jugador.update();};},};
Link to comment
Share on other sites

  • 2 months later...

Sorry, I wasn't notified of your answer ( although I'm the one who initiated it) 

 

What you're doing is initializing the P2 system in your playState. This is also working for me.

 

What's not is when I initialize it in a previours state ( LoadingState for example ). Well, it seems to work but contact events are not triggered anymore.

Link to comment
Share on other sites

Are you wanting to have a whole new set up of physics in a new state but with previously set out rules in another state?

that i don't think you can do but if you kill() your previous state and load a new state and start the whole physics system, sprite creation and more then i don't see why it should error on you asking as the previous physics enabled state is no longer running.

Link to comment
Share on other sites

I would assume if you put the startSystem of the physics in your boot.js and then test it with sprites using the physics in any state afterwards then it should work.

i have used states and variables widely around my state set up so try setting the physics up first then set up the sprite physics and spread them out into states for testing.

Link to comment
Share on other sites

That's actually what's not working.

 

Let's say I have : boot.js => play.js

 

If I start my system in boot and create my sprites in play, the physics will work fine but the contact events won't be triggered. ( I'm talking about onBeginContact and onEndContact here)

 

By starting my system in play.js it fixes the issue.

Link to comment
Share on other sites

yeah so you were thinking along the same lines as me then. Well i really dont know.

Only thing i can say is to initate the creation of the sprites in the play.js but have the physics started in boot?

 

If that doesnt work then you will have to kill the state and initiate a new state as well as starting the physics back up.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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