Jump to content

Possible bug adding graphics to sprites with p2, phaser 2.4.1


Mehow
 Share

Recommended Posts

I just decided to move my game from 2.3 to 2.4, only to find my sprites with p2 bodies have gone insane.

 

I managed to narrow it down to this test code:

function create() {    game.physics.startSystem(Phaser.Physics.P2JS);    var sprite = game.add.sprite(100, 50);    //drawing    var graphic = game.add.graphics();    graphic.beginFill('0xff0000');    graphic.drawCircle(100, 50, 10);    graphic.endFill();    sprite.addChild(graphic);    //  Create our physics body.    game.physics.p2.enable(sprite, true);    sprite.body.setCircle(10);    //drawing    var graphic2 = game.add.graphics();    graphic2.beginFill('0x0000ff');    graphic2.drawCircle(100, 50, 10);    graphic2.endFill();    sprite.addChild(graphic);}var game = new Phaser.Game(800, 600, Phaser.CANVAS, '#phaser_parent', {    create: create});

Basically,the first graphic gets given a separate physics body at (0,0) and starts with velocity for some reason (because its start position overlaps worldbounds maybe?)

 

The second graphic, added after physics was enabled, works as expected.

 

Also just realised the second graphic is place with global co-ordinates, and the first is placed relative to the sprite.

 

Are these bugs or expected behaviours I've misunderstood/not read the docs properly?

 

Link to comment
Share on other sites

sprite.addChild(graphic); // should be sprite.addChild(graphic2);

 

Here see:

function create() {    game.physics.startSystem(Phaser.Physics.P2JS);    var sprite = game.add.sprite(100, 50, 'phaser');    //  Create our physics body.    game.physics.p2.enable(sprite, true);    sprite.body.setCircle(20);       //drawing    var graphic = game.add.graphics();    graphic.beginFill('0xff0000');    graphic.drawCircle(50, 0, 10);    graphic.endFill();    sprite.addChild(graphic);    //drawing    var graphic2 = game.add.graphics();    graphic2.beginFill('0x0000ff');    graphic2.drawCircle(100, 0, 10);    graphic2.endFill();    sprite.addChild(graphic2);}
Link to comment
Share on other sites

A square is appearing in the right hand corner which is perhaps a default bound of the circle drawn and that is the object getting moved.

 

See:

function create() {    game.physics.startSystem(Phaser.Physics.P2JS);    var sprite = game.add.sprite(100, 50, 'phaser');    //drawing    var graphic = game.add.graphics();    graphic.beginFill('0xff0000');    graphic.drawCircle(0, 0, 10);    graphic.endFill();    sprite.addChild(graphic);    //  Create our physics body    game.physics.p2.enable(sprite, true);    sprite.body.setCircle(18);}

Example:

http://phaser.io/sandbox/hGiDkGKH

 

Note - I tested this in 2.3.0 and it came up with the folling error:

TypeError: a.anchor is undefined

Link to comment
Share on other sites

Thanks langerz, should've seen that graphic2 typo.
 
Sorry but I just realised my previous phaser version seems to of been 2.2.2, not 2.3 :/
So apologies for being a massive spoon, I imagine there's something in the 2.3 change log that would of pointed this out.

That said if this is defined behaviour it would probably make more sense for 2.4 to error like 2.3 does.

My game can be completely fixed in 2.4 by making sure graphics are added to a sprite only after that sprite has had p2 enabled on it

function create() {    game.physics.startSystem(Phaser.Physics.P2JS);    var sprite = game.add.sprite(100, 50);    //drawing    var graphic = game.add.graphics();    graphic.beginFill('0xff0000');    graphic.drawCircle(0, 0, 10);    graphic.endFill();    //switching these 2 lines makes the difference    //in 2.4 this works, switched it causes the graphic to gain a kind of default body at (0,0)    //in 2.3 this works, switched, it blows up with an errorTypeError: Cannot read property 'set' of undefined    //in 2.2.2 both work    game.physics.p2.enable(sprite, true);    sprite.addChild(graphic);    sprite.body.setCircle(18);}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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