# \[Tutorial\] Making an Enemy Follow Multiple Sprites :snake:

**URL:** <https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914>\
**Category:** Help\
**Tags:** game, graphics-and-math, make\
**Created:** [February 17, 2025, 12:36am UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914 "2025-02-17T00:36:56Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)\
**Post date:** [February 17, 2025, 12:36am UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/1 "2025-02-17T00:36:56Z")

</div>

The code gathers all the player-enemy distances, sorts them in an array to find the shortest distance, then for each player sprite, compares it’s distance to the shortest distance in the array to find the closest player.  
This player sprite is stored in the enemy sprite data and set as a follow target. All of this happens every game update so that the enemy can register a new closest player as fast as possible.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/c/a/cac0840f87c45358831ff44ad96afd3e55de67b7.png)  
The array before being sorted. After the sort, the 52.47… distance will be in the 0th spot in the array.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/3/5/35708fa1266d7d6a7a307c1de82368849c095e8c.png)  
The distances are recorded to such an absurd number of precision that I’ve managed to use this method to register sprite overlaps as the default block can be quite buggy (especially when lagging or when dealing with multiple sprites)  
Registering sprite overlaps is the same as finding the closest sprite, but it checks if the shortest distance is also short enough to be overlapping with the player.

> **Code for the visuals**
>
> ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/1/6/161eb0389eb6a4a2c3da443f3e41dfaf78cc0398.png)  
> ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/1/4/142151e68ab710fddaa61991e1aee1b3b0866b54.png)

> **[Closest sprite with chasing w colours](https://arcade.makecode.com/24370-52301-86490-15262)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

Have fun spamming B with these ones as well (just looks cool lol)

> **[Closest sprite with chasing](https://arcade.makecode.com/17900-68493-22009-34773)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

> **[Closest sprite with chasing](https://arcade.makecode.com/25992-89289-53148-21033)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

btw you need better arrays for the sorting block (oh and also [sprite utils](https://github.com/jwunderl/arcade-sprite-util) for like everthing else lol)

> **[GitHub - sargedev/betterarrays: Extension that adds many useful utility array...](https://github.com/sargedev/betterarrays)**
>
> Extension that adds many useful utility array methods

---

<div class="post-metadata">

**Author:** ![\_jupiter](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/_jupiter/32/28012_2.png) [@\_jupiter](https://forum.makecode.com/u/_jupiter)\
**Post date:** [February 18, 2025, 1:10pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/2 "2025-02-18T13:10:22Z")

</div>

“I am a good coder”  
“I am a good coder”

Literally anything involving arrays:

---

<div class="post-metadata">

**Author:** ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)\
**Post date:** [February 18, 2025, 11:53pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/3 "2025-02-18T23:53:00Z")

</div>

that’s why im sharing this, it’s pretty simple but shows the power of arrays!

---

<div class="post-metadata">

**Author:** ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)\
**Post date:** [February 19, 2025, 6:09pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/4 "2025-02-19T18:09:45Z")

</div>

Wow this is a really detailed tutorial / extension! And very useful, with great graphics! 😺 👍

---

<div class="post-metadata">

**Author:** ![redSprite](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/redsprite/32/34262_2.png) [@redSprite](https://forum.makecode.com/u/redSprite)\
**Post date:** [February 23, 2025, 6:01am UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/5 "2025-02-23T06:01:53Z")

</div>

At least tell us what extensions to download.  
Peak tutorial however.

---

<div class="post-metadata">

**Author:** ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)\
**Post date:** [February 24, 2025, 5:39am UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/6 "2025-02-24T05:39:41Z")

</div>

> [@Luke](#):
>
> btw you need better arrays for the sorting block (oh and also [sprite utils](https://github.com/jwunderl/arcade-sprite-util) for like everthing else lol)
> 
> [https://github.com/sargedev/betterarrays](https://github.com/sargedev/betterarrays)

ahem.

---

<div class="post-metadata">

**Author:** ![redSprite](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/redsprite/32/34262_2.png) [@redSprite](https://forum.makecode.com/u/redSprite)\
**Post date:** [February 24, 2025, 5:14pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/7 "2025-02-24T17:14:52Z")

</div>

Oh right  
I’m blind.

---

<div class="post-metadata">

**Author:** ![JaidenThePerson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jaidentheperson/32/16250_2.png) [@JaidenThePerson](https://forum.makecode.com/u/JaidenThePerson)\
**Post date:** [May 30, 2025, 5:12pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/8 "2025-05-30T17:12:40Z")

</div>

Oh no oh no ARRAYS!? I have no clue how these work 😭

---

<div class="post-metadata">

**Author:** ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)\
**Post date:** [June 9, 2025, 1:50pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/9 "2025-06-09T13:50:23Z")

</div>

[They’re really useful when you get to know them!](https://arcade.makecode.com/courses/csintro2/arrays)

---

<div class="post-metadata">

**Author:** ![JaidenThePerson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jaidentheperson/32/16250_2.png) [@JaidenThePerson](https://forum.makecode.com/u/JaidenThePerson)\
**Post date:** [June 17, 2025, 6:20pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/10 "2025-06-17T18:20:20Z")

</div>

OMG I had no idea there were array tutorial TYSM!!!

---

<div class="post-metadata">

**Author:** ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)\
**Post date:** [June 20, 2025, 6:23pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/11 "2025-06-20T18:23:43Z")

</div>

You’re welcome!! : D I’m glad more people can get to use them now!

---

<div class="post-metadata">

**Author:** ![aChessFanatic123](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/achessfanatic123/32/33444_2.png) [@aChessFanatic123](https://forum.makecode.com/u/aChessFanatic123)\
**Post date:** [August 9, 2026, 8:06pm UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/12 "2026-08-09T20:06:17Z")

</div>

does it pathfinds in a tilemap?

---

<div class="post-metadata">

**Author:** ![TheEarth](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theearth/32/34579_2.png) [@TheEarth](https://forum.makecode.com/u/TheEarth)\
**Post date:** [August 10, 2026, 4:33am UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/13 "2026-08-10T04:33:22Z")

</div>

No please read the post fully to understand what it does

---

<div class="post-metadata">

**Author:** ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)\
**Post date:** [August 10, 2026, 4:33am UTC](https://forum.makecode.com/t/tutorial-making-an-enemy-follow-multiple-sprites/33914/14 "2026-08-10T04:33:50Z")

</div>

no, to do that you’d need to use a\* tilemap pathfinding
