Jump to content

TypeError: sprite.body is null


avb
 Share

Recommended Posts

I've been working on a Phaser game, and I started seeing a "TypeError: sprite.body is null" after ~3-5mins of playing, which then stops the whole game. It's coming out of phaser.js:81251 which is in collideSpriteVsGroup. Any ideas of how to fix this? Is it a bug in phaser?

 

A live version is here: https://dl.dropboxusercontent.com/u/1124506/planet/index.html

 

I'm running version "Phaser v2.4.3 | Pixi.js v2.2.8 | WebGL | WebAudio | http://phaser.io" in Firefox and Chrome.

Link to comment
Share on other sites

destroy is a permanent thing: it means "remove every useful piece of state off this display object and get it ready to be garbage collected". That means removing its reference to the game, removing its body... all of it. "destroy" is the kind of method that is called when the system is cleaning up after itself.

 

You might want to look at kill instead. "kill" means "make it look like this object isn't in the game anymore". It won't get its update method called. It won't render. It won't come back to its parent group's calls of getFirstAlive. But, *crucially*, the sprite can be revived or reset. And thus re-used! Which is usually what you want.

 

What was it that was being destroyed? If it was an enemy or a player or a bullet or something... that's a great candidate for reuse in which case you should really consider killing it instead of destroying it.

Link to comment
Share on other sites

I ended up killing and then running a cleanup check (forEachDead … destroy()) at the end of the update loop. I didn't think about reusing, and the project's too small to make it worth rewriting now, but thanks for the idea.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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