简体   繁体   中英

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.

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM