avb Posted October 13, 2015 Share Posted October 13, 2015 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 More sharing options...
avb Posted October 13, 2015 Author Share Posted October 13, 2015 I seem to have fixed the problem by moving a Sprite.destroy() call out of the physics.arcade.collide callback. Link to comment Share on other sites More sharing options...
jmp909 Posted October 13, 2015 Share Posted October 13, 2015 i think this is a common issue with physics engines. you need to wait until after the physics step completes from what I understand. Maybe this should be integrated into the Phaser core. I would possibly raise it as an issue http://www.iforce2d.net/b2dtut/removing-bodies Link to comment Share on other sites More sharing options...
drhayes Posted October 13, 2015 Share Posted October 13, 2015 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 More sharing options...
avb Posted October 15, 2015 Author Share Posted October 15, 2015 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 More sharing options...
Recommended Posts