Attach a sprite to another sprite and make them move together

#1

Hello ! I have created a healthbar object that I want to attach to my character object.
What the best/simpliest way to move the healthbar at the same time that the character move ?

I have found a solution here but it looks very ugly to me.

#2

For a low-effort but somewhat inefficient solution, you could use a Container. Another possibility is to place the health bar at the player’s position (plus some offset) every frame (i.e. every update call) should work just fine and wouldn’t be hard to apply to other sprites if needed - you could, for example, store a reference to a sprite’s health bar on the sprite itself, then sync the positions of all sprites and their health bar.

1 Like
#3

Hello @mariostarr, as @Telinc1 mentioned i believe the best way is, create a Container, add everything what you want move together and just move Container around :smiley:

1 Like
#4

why is it inefficient ?

#5

It’s just that containers a bit overkill for something so simple. It’s best to use them for cases where they’re unavoidable because many of them can take up a lot of time during rendering. You also shouldn’t rely on them too much for now as there are future plans to rework them, but I don’t know whether that still applies.

If you want to use a container for this, go ahead, there’s nothing wrong with it. In my personal opinion, however, syncing the positions manually would take less code, which is why I’m questioning the usefulness of containers in this case.

1 Like
#6

After trying container, I decided to update healthbar position manualy and yes it was much easier

Pin/Connect/Set object position to another object