bergben
bergben

Reputation: 1415

How to make sprite follow another sprite in Phaser 3

How would you go about making a sprite follow another sprite in Phaser 3?

An example would be the parts of a snake in slither.io

In Phaser 2 there was the addChild (see here), which in Phaser 3 only exists for containers... There are many examples on how to accomplish this in Phaser 2, but I can't figure it out for Phaser 3...

Alternatively I could create my own Path array like in this example. But I am quite sure there should be an easier solution.

Upvotes: 2

Views: 4590

Answers (1)

BdR
BdR

Reputation: 3048

Sounds similar to my question here, String of moving sprites following the player, most efficient way to do it?

In the end I just used my own suggestion as mentioned in the opening question:

  • Create a Phaser.Group of sprites
  • Use array of like 9999 locationpoints (x,y)
  • Keep track of array index for head locations
  • For each frame, save value of head locations at index position in array
  • Update positions of the following sprites accordingly in .update()
  • index+1 (if larger than say 9999 then index=0)

That is what I used in the final game ;)

Upvotes: 2

Related Questions