ChubbRck Posted April 11, 2014 Share Posted April 11, 2014 Hi all, I know this has been asked before, but I still couldn't get any clarity after reading those other posts. Every time I try to destroy an object when it passes out of bounds I get an error, usually: Uncaught TypeError: Cannot read property 'camera' of null I understand that I'm probably trying to do something with that object when it is already destroyed... the problem is I don't know what. The object is in a group which is being checked for collisions against another group. It also has a physics body. I've tried removing the object from its group before destroying it, but still no dice. Any guidance is appreciated - Nick Link to comment Share on other sites More sharing options...
adamyall Posted April 12, 2014 Share Posted April 12, 2014 Have you tried setting the outOfBoundsKill flag on the sprite to true? I think that will do it for you. Link to comment Share on other sites More sharing options...
codevinsky Posted April 14, 2014 Share Posted April 14, 2014 Adam's correct with one exception: For a sprite to kill itself when it goes out of bounds:var sprite = game.add.sprite(100,100,'sprite');sprite.checkWorldBounds = true;// checkWorldBounds must be enabled for a sprite to kill itself// when it goes out of bounds.// This causes the sprite to check it's position every frame.// It can be expensive, especially if you have a metric buttload of// sprites doing this.sprite.outOfBoundsKill = true;// this will automatically have the sprite call it's own "kill()" method // if it leaves the world bounds. Link to comment Share on other sites More sharing options...
ChubbRck Posted April 14, 2014 Author Share Posted April 14, 2014 Hi guys, Thanks for your replies - I have this working but I was concerned with 'killing' the sprites as opposed to destroying them. Since I'm not reusing sprites (I'm creating new ones at set intervals) I was hoping to avoid a memory buildup of undrawn, "killed" sprites. Does anyone know if this is a valid concern or not? Thanks again - Link to comment Share on other sites More sharing options...
ChubbRck Posted April 15, 2014 Author Share Posted April 15, 2014 Just a little bump. Anyone know how to safely destroy() sprites w/ physics bodies? Link to comment Share on other sites More sharing options...
j0hnskot Posted May 22, 2014 Share Posted May 22, 2014 I have the same problem. Any help here? Link to comment Share on other sites More sharing options...
ChubbRck Posted May 28, 2014 Author Share Posted May 28, 2014 Surely there must be a way to check if a sprite exists before destroying it? Link to comment Share on other sites More sharing options...
Recommended Posts