Move a group of sprites with physics enabled



I want to move a collection of sprites which have their physics enabled.
To display my desired complex object, I made some nested groups to organize the textures a little better and to be able to design that object within the Phaser Editor. That gives me the advantage of not having to recompute the position each time and I can design the object at any anchor.

Group (toplevel group)

  • Group1
    • Sprite1
    • Sprite2
  • Group2
    • Sprite3
    • Sprite4
  • Group3

So Group is my toplevel group and it consists of multiple groups which themselves contain sprites.
Each sprites has physics data (loaded from json, created with PhysicsEditor, Lima + Corona JSON). How can I move the entire group with the physics body?
When I set x and y properties of the toplevel group, then all sprites are moved to that location, but not the bodies. This, of course, results in a strange behaviour.

Can someone help me? :slight_smile:


You need to change the body of sprites too.
sprite.body.reset(sprite.x, sprite.y)


Hi @Davaaron

What physics system are you using? I guess it is not Arcade. Probably the physics system has a mechanism to move the objects at the same time. Maybe to add them to groups is not a good idea. if it works for you in design-time, maybe you can remove them from the groups in runtime.


I use the box2d port.
Your idea was also in my mind but I decided not to so as the designers and programmers might come up with some issues. So, what I did was using body.reset for all elements within the group.
Take on sprite/body, move it and calculate the x and y difference, then apply that difference to all other sprites/bodies within that group.
Be aware you need to move the group, not the sprite itself. Because: If you move the sprite, the body will not move along. But moving the body will also move your sprite.

Once the project is done, the documentation about Phaser 3 and PE2 got evolved, I’m looking forward to migrate. Because this also means that I can finally drop box2d and use matterjs (even not a port version) instead.