# I need to kill the closest sprite in an array please help

**URL:** https://forum.makecode.com/t/i-need-to-kill-the-closest-sprite-in-an-array-please-help/45187
**Category:** Help
**Tags:** game
**Created:** [July 31, 2026, 5:28pm UTC](https://forum.makecode.com/t/i-need-to-kill-the-closest-sprite-in-an-array-please-help/45187 "2026-07-31T17:28:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![temmie](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@temmie](https://forum.makecode.com/u/temmie)
#### Post date: [July 31, 2026, 5:28pm UTC](https://forum.makecode.com/t/i-need-to-kill-the-closest-sprite-in-an-array-please-help/45187/1 "2026-07-31T17:28:10Z")

</div>

i have 3 sprites in an array i know how to make it so uppon a button press a specific sprite is killed, but i need a way to kill the closest sprite to the player

---

<div class="post-metadata">

### Author: ![CodaKnight](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/codaknight/32/33491_2.png) [@CodaKnight](https://forum.makecode.com/u/CodaKnight)
#### Post date: [July 31, 2026, 6:40pm UTC](https://forum.makecode.com/t/i-need-to-kill-the-closest-sprite-in-an-array-please-help/45187/2 "2026-07-31T18:40:56Z")

</div>

Alright, @temmie , you can use the sprite utils extension and the sprite-data extension and use the distance from sprite to sprite block to check the distance between the sprites and then kill the closest one; like this:

> **[@temmie (help)](https://arcade.makecode.com/S62129-71229-18549-71984)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

And also, since you’re using an array make sure to **remove the enemy from the array** so you don’t cause any accidental null/undefined bugs!

---

<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: [July 31, 2026, 6:41pm UTC](https://forum.makecode.com/t/i-need-to-kill-the-closest-sprite-in-an-array-please-help/45187/3 "2026-07-31T18:41:34Z")

</div>

I created a function that gets the closest sprite among a sprite array toward another sprite. Note that the sprite utils extension needs to be installed for it to work.

```auto
function getClosestInAnArray(spriteArray: Sprite[], sprite: Sprite) {
    let closestDistance: number = 1/0
    let currentSprite: Sprite = null
    for (let spriteValue of spriteArray) {
        let spriteDistance: number = spriteutils.distanceBetween(spriteValue, sprite)     
        if (spriteDistance < closestDistance) {
            closestDistance = spriteDistance
            currentSprite = spriteValue
        }
    }
    return(currentSprite)
}

```

As a bonus, here’s a demo project:

> **[Closest-To-Sprite-Algorithm](https://arcade.makecode.com/S99826-49327-31073-05996)**
>
> Press A to create another sprite.
> Press B to destroy the closest sprite.

---

<div class="post-metadata">

### Author: ![Rune](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/rune/32/18951_2.png) [@Rune](https://forum.makecode.com/u/Rune)
#### Post date: [July 31, 2026, 6:41pm UTC](https://forum.makecode.com/t/i-need-to-kill-the-closest-sprite-in-an-array-please-help/45187/4 "2026-07-31T18:41:44Z")

</div>

Finding the closest sprite is actually quite complex. Something you will need first is the distance between block from the Sprite Utils extension. Then you will set the first sprite in the array to “the current closest” Now you must go through the array and see if the distance between the new sprite is less than the distance between the last closest one. Once you go through all of them checking in this way, the current closest will be the most closest.
