valueerror Posted May 4, 2015 Share Posted May 4, 2015 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 itfor (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:616how can i destroy the bodies in there.. thank you ! Link to comment Share on other sites More sharing options...
icp Posted May 5, 2015 Share Posted May 5, 2015 for (var i = 0; i < polyline_bodies.length; i++){ polyline_bodies[i].body.sprite.destroy();}Try this. Link to comment Share on other sites More sharing options...
valueerror Posted May 5, 2015 Author Share Posted May 5, 2015 thx for your thoughts but the array already contains the physics bodies.. therefore .body on a body will definitely fail. and those polygons have no sprite.. so i'm unable to access the sprite.kill or sprite.destroy methods.. Link to comment Share on other sites More sharing options...
icp Posted May 5, 2015 Share Posted May 5, 2015 I tested this and it works. Link to comment Share on other sites More sharing options...
valueerror Posted May 5, 2015 Author Share Posted May 5, 2015 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 Link to comment Share on other sites More sharing options...
holmberd Posted September 20, 2015 Share Posted September 20, 2015 Did you find a solution to your problem? When using a body by calling var _body = new p2.Body(); _body is created containing p2.body.data 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 body.data 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 _body.data from the world using:game.physics.p2.world.addBody(_body);game.physics.p2.world.removeBody(_body); and then just nullify the rest, any other input on this? Link to comment Share on other sites More sharing options...
Recommended Posts