Arcade Sprite Pool

When pooling ordinary sprites you toggle a sprite’s active and visible properties on when the sprite is in use and off when it’s out of use (free).

With physics sprites you’ll also toggle the body.enable property on and off at the same time. It’s convenient to use the enableBody() and disableBody() methods because they can set all 3 properties at once. When recycling you should also use the reset, x, and y arguments in enableBody() to synchronize the body and game object at a new position.

If your game objects don’t have those methods, you can use

gameObject.body.enable = false;

to disable and

gameObject.body.enable = true;
gameObject.body.reset(x, y);

to enable when recycling them, in addition to toggling active and visible.

10 Likes

Thank you !!

I was just looking for this, I was deactivating my sprites but the physics kept updating.
enable/disable body to the rescue =)

Here the bodies are colored with phaser-plugin-debug-body-colors. Note

  • At the start, there are disabled bodies (gray) in the top-left of the game canvas. Those are the bodies of the free (inactive) sprites. They are visible there until the last sprite is activated.
  • There are disabled bodies (gray) at the bottom border, where sprites are deactivated. They remain there until the sprite is reactivated.

2 Likes