# How to find the closest sprite to the player

**URL:** <https://forum.makecode.com/t/how-to-find-the-closest-sprite-to-the-player/27578>\
**Category:** Help\
**Tags:** game\
**Created:** [March 8, 2024, 11:05pm UTC](https://forum.makecode.com/t/how-to-find-the-closest-sprite-to-the-player/27578 "2024-03-08T23:05:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nether\_Shard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nether_shard/32/13961_2.png) [@Nether\_Shard](https://forum.makecode.com/u/Nether_Shard)\
**Post date:** [March 8, 2024, 11:05pm UTC](https://forum.makecode.com/t/how-to-find-the-closest-sprite-to-the-player/27578/1 "2024-03-08T23:05:45Z")

</div>

For my old game my game corrupted and I couldn’t seem to fix it so I moved on. For my new game that I am making I wanted to try to make it so that when A is pressed, It will find the closest sprite and that sprite would become the new player and you could take control, how would I do that?  
This is the **very very** beta of this game and yes, I am using sprite utils.  
Also don’t judge me that I do the art first.  
[Swappang {VERY BETA}](https://arcade.makecode.com/S08324-66221-63159-24975)

---

<div class="post-metadata">

**Author:** ![WoofWoof](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/woofwoof/32/32523_2.png) [@WoofWoof](https://forum.makecode.com/u/WoofWoof)\
**Post date:** [March 11, 2024, 3:41pm UTC](https://forum.makecode.com/t/how-to-find-the-closest-sprite-to-the-player/27578/2 "2024-03-11T15:41:18Z")

</div>

I would do something like:

SpriteDistance = 1000000

For (value) in [list of all sprites of kind (potentialPlayer)]:

```
  Set Dist = (use Pythagorean theorem to find distance from the (current player) to (value))

  If (Dist) < (SpriteDistance):
        Set SpriteDistance to (Dist)
        Set ClosestSprite to (value)

```

After it loops through all the sprites, ClosestSprite will be the closest sprite to the player!  
If the current player is the same sprite type as the potential players, you might have to add a check that Dist =/= 0 so that your closest player won’t be the player you’re currently controlling.  
Hope this helps! (I also hope you can read my horrible sudo code lol)  
If you need help with the Pythagorean theorem just ask!

---

<div class="post-metadata">

**Author:** ![Nether\_Shard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nether_shard/32/13961_2.png) [@Nether\_Shard](https://forum.makecode.com/u/Nether_Shard)\
**Post date:** [March 11, 2024, 5:18pm UTC](https://forum.makecode.com/t/how-to-find-the-closest-sprite-to-the-player/27578/3 "2024-03-11T17:18:43Z")

</div>

Thank you, twice!

---

<div class="post-metadata">

**Author:** ![Nether\_Shard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nether_shard/32/13961_2.png) [@Nether\_Shard](https://forum.makecode.com/u/Nether_Shard)\
**Post date:** [March 13, 2024, 12:13am UTC](https://forum.makecode.com/t/how-to-find-the-closest-sprite-to-the-player/27578/4 "2024-03-13T00:13:41Z")

</div>

So I tried using what you said and I am having some errors and I can’t find out what is causing them, can you help me with this?  
[Swappang](https://arcade.makecode.com/S08324-66221-63159-24975)
