Sign in to follow this  

can't destroy p2 bodies created by 'convertCollisionObjects' from Tiled

Recommended Posts

this is how the array that contains all the physicsbodies is created

 polyline_bodies = game.physics.p2.convertCollisionObjects(supermap,"polygons");

i can then cycle over all bodies and flawlessly set collisiongroups, materials and so on.. but if i want to cycle through this array and kill the physics bodies in it

for (i=0; i<polyline_bodies.length; i++){        polyline_bodies[i].destroy()}

i get the following weird error:

Uncaught TypeError: Cannot set property 'body' of nullc.Physics.P2.Body.destroy @ phaser.min.js:23level0.update @ game.js:616

how can i destroy the bodies in there.. 


thank you !

Share this post

Link to post
Share on other sites

well sorry..   as i already wrote twice the array "polyline_bodies" already contains the physics bodies (hence the name)


the is no property body on a p2 body..   there just isn't !  whatever you are doing you are doing something different..


if your code works than only if your array contains sprites..    sprites have a body property which has a sprite property (recursive)  in that case you should be able to call destroy() right away "polyline_bodies.destroy()" and it should work..


my question is:

i'm calling destroy() on a p2 physics body and it isn't working..  instead i get this weird error message from above..  


this only happens on p2 bodies that are polygons created from a tilemap..  


the only difference between a body that i can destroy and this one is that the first one also has a sprite.. the undestroyable body has sprite:null 

Share this post

Link to post
Share on other sites

Did you find a solution to your problem?


When using a body by calling var _body = new p2.Body(); _body is created containing and null parent.


So for matter of discussion lets make a new body and a sprite to it.

var _body = new Phaser.Physics.P2.Body(game,game.add.sprite(0,0),0,0,1);


calling _body.destroy(); will nullify the sprite, but since the sprite has it's own body that is not linked to our body our will not be destroyed.


Lets make an body without a sprite:

var _body = new Phaser.Physics.P2.Body(game,null,0,0,1);


_body.destroy(); won't do anything to it, since it seems it will only, as you stated, recursively destroy the _body.sprite.body.


Right now I'm just adding and removing the from the world using:;;


and then just nullify the rest, any other input on this?

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.