# Sensing sprites

**URL:** <https://forum.makecode.com/t/sensing-sprites/4822>\
**Category:** Help\
**Created:** [December 7, 2020, 11:01am UTC](https://forum.makecode.com/t/sensing-sprites/4822 "2020-12-07T11:01:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![UnderwaterAstronaut](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/underwaterastronaut/32/2228_2.png) [@UnderwaterAstronaut](https://forum.makecode.com/u/UnderwaterAstronaut)\
**Post date:** [December 7, 2020, 11:01am UTC](https://forum.makecode.com/t/sensing-sprites/4822/1 "2020-12-07T11:01:27Z")

</div>

Is there a way to sense if a sprite is out of the screen or if a sprite is in ‘sight’ of another sprite? I’m trying to add in a battle mechanic where if the enemy sprite is out of the screen, the battle would end because you escaped the battle. Any type of help would mean a lot!

---

<div class="post-metadata">

**Author:** ![DahbixLP](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dahbixlp/32/4760_2.png) [@DahbixLP](https://forum.makecode.com/u/DahbixLP)\
**Post date:** [December 7, 2020, 4:23pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/2 "2020-12-07T16:23:11Z")

</div>

There is something like this! The sprite utils extension has a block : distance between sprite and other sprite! So it works with that you need an  
if (((distance between (sprite) and (sprite2)) (\>) (100) then

> **[jwunderl/arcade-sprite-util](https://github.com/jwunderl/arcade-sprite-util)**
>
> A few utility functions for sprites. Contribute to jwunderl/arcade-sprite-util development by creating an account on GitHub.

  
There you go!

---

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [December 7, 2020, 4:23pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/3 "2020-12-07T16:23:30Z")

</div>

I did this in my Decked Out:

1. Fire a 1x1 pixel sprite towards the player’s direction.
2. If it collides with the player, keep chasing/fighting him/her
3. If it doesn’t collide with the player for the next ex. 5 seconds, you’ve escaped.

---

<div class="post-metadata">

**Author:** ![GameGod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/gamegod/32/2756_2.png) [@GameGod](https://forum.makecode.com/u/GameGod)\
**Post date:** [December 7, 2020, 4:24pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/4 "2020-12-07T16:24:11Z")

</div>

Maybe the distance formula?

---

<div class="post-metadata">

**Author:** ![SPerkins25](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@SPerkins25](https://forum.makecode.com/u/SPerkins25)\
**Post date:** [December 7, 2020, 4:24pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/5 "2020-12-07T16:24:20Z")

</div>

When I programmed the behaviour of an enemy bat I had them wandering around until the player collided with an EnemyVision sprite which was essentially just a massive red circle set to invisible that had the bat’s X and Y position.

---

<div class="post-metadata">

**Author:** ![cosmoscowboy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cosmoscowboy/32/1718_2.png) [@cosmoscowboy](https://forum.makecode.com/u/cosmoscowboy)\
**Post date:** [December 7, 2020, 10:36pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/6 "2020-12-07T22:36:21Z")

</div>

> [@UnderwaterAstronaut](#):
>
> Is there a way to sense if a sprite is out of the screen

Just write a function that is called in the ‘game update’ that checks the following (you can program this in block or script)

// horizontal check  
if (sprite.left \> screen.width || sprite.right \< 0)

// vertical check  
if (sprite.top \> screen.height || sprite.bottom \< 0)

Also what @DahbixLP mentions, you could use the arcade-sprite-util extension to calculate the distance between the two sprites. This will take into account if the sprite is at an angle to the other sprite (i.e. using Pythagoras Theorem). If you just need to get the x or y distance, then subtracting those values will suffice:

> **[Check in screen & get distances](https://arcade.makecode.com/74569-51728-07060-85516)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

I this example I set the score to be the distance between the two sprites (using the extension from Joey). The ‘sprite’ also says the absolute x/y distance from the second sprite. Absolute just means get the value as a positive number. I also added a function that checks separately if the ‘sprite’ has left the screen horizontally or vertically.

---

<div class="post-metadata">

**Author:** ![felixtsu](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/felixtsu/32/12024_2.png) [@felixtsu](https://forum.makecode.com/u/felixtsu)\
**Post date:** [December 8, 2020, 3:32pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/7 "2020-12-08T15:32:24Z")

</div>

Big fan of sneaking games, thought would be nicer if players can hide behind the walls, try to make a small demo and,

> **[hideNseek](https://arcade.makecode.com/95701-95562-07975-72681)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

Enemies stay where they are until a player is in sight and stopping chasing player if lost sight.

---

<div class="post-metadata">

**Author:** ![Wanna\_be\_coder](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/wanna_be_coder/32/2667_2.png) [@Wanna\_be\_coder](https://forum.makecode.com/u/Wanna_be_coder)\
**Post date:** [December 14, 2020, 8:12pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/8 "2020-12-14T20:12:05Z")

</div>

what is that extension?

---

<div class="post-metadata">

**Author:** ![DahbixLP](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dahbixlp/32/4760_2.png) [@DahbixLP](https://forum.makecode.com/u/DahbixLP)\
**Post date:** [December 14, 2020, 8:23pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/9 "2020-12-14T20:23:37Z")

</div>

This extension is amazing! Could you tell me the name? (Or the link)

---

<div class="post-metadata">

**Author:** ![felixtsu](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/felixtsu/32/12024_2.png) [@felixtsu](https://forum.makecode.com/u/felixtsu)\
**Post date:** [December 14, 2020, 11:23pm UTC](https://forum.makecode.com/t/sensing-sprites/4822/10 "2020-12-14T23:23:51Z")

</div>

@DahbixLP @Wanna_be_coder I’ve created a new topic with demo and details of the implementation.

> [@Sneaking Demo](https://forum.makecode.com/t/sneaking-demo/4961):
>
> It’s like a proof of concept and some exts. Not fully in blocks (yet). FoV (distance and walls) Patrolling enemies More details later. Cheers
