# How do I make a distance formula in block code

**URL:** <https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009>\
**Category:** Help\
**Created:** [December 14, 2020, 10:31pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009 "2020-12-14T22:31:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 14, 2020, 10:31pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/1 "2020-12-14T22:31:53Z")

</div>

I know the distance formula is (X\_2-X\_1)^2+(Y\_1-Y\_2)^2 squared is the formula. I wanna make something like if it is a certain distance or closer, it would be umm… well I don’t wanna spoil my game.

ex. of what I wanna do  
If Sprite1 is 30 pixels away or lower to Sprite2, then a command should do something. I wanna make the formula to see how close a Sprite is to another. I will be trying to experiment with this formula.

---

<div class="post-metadata">

**Author:** ![livcheerful](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/livcheerful/32/3509_2.png) [@livcheerful](https://forum.makecode.com/u/livcheerful)\
**Post date:** [December 14, 2020, 10:46pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/2 "2020-12-14T22:46:42Z")

</div>

hihi here’s a sample program!

> **[distance formula](https://arcade.makecode.com/84795-11394-93326-48351)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

I made a distance formula function that takes in two sprites

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/8/84fea4f168e88433a4c8d3d81b29e3e37721a426.png)

BUT ALSO @jwunderl made a super useful extension that has the distance formula built in! It’s jwunderl/pxt-sprite-util (I think???)

I imported it into the example so you can see!

---

<div class="post-metadata">

**Author:** ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)\
**Post date:** [December 14, 2020, 10:47pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/3 "2020-12-14T22:47:52Z")

</div>

ahhhh it’s [https://github.com/jwunderl/arcade-sprite-util](https://github.com/jwunderl/arcade-sprite-util), naming is hard okay

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 14, 2020, 10:51pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/4 "2020-12-14T22:51:25Z")

</div>

So will this detect how many pixels an enemy sprite is then do an action if too close or too far? Depending if you use a greater than or equal sign?

---

<div class="post-metadata">

**Author:** ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)\
**Post date:** [December 14, 2020, 10:54pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/5 "2020-12-14T22:54:25Z")

</div>

The function Vivian made (or the block from my extension) will give you how far the center of one sprite is from the center of another – you’d have to use that in some other check to make use of it. For example, you could make it do something in an on game update when the centers are within 50px like this:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/8/83a80eb0ad986298ae0c78ac421f4dcdf4a4f475.png)

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 14, 2020, 10:58pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/6 "2020-12-14T22:58:48Z")

</div>

Now that helps A LOT. Thanks. I could even use what Vivian did as well but I replace the `distance between mySprite and myEnemy` with the ginormous distance formula? Unless if I use your extension then this helps a lot. I’m making a game that is based on chance and is very frusterating.

---

<div class="post-metadata">

**Author:** ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)\
**Post date:** [December 14, 2020, 11:00pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/7 "2020-12-14T23:00:37Z")

</div>

Right, for reference my extension is almost identical to her code (just adds a null check on each sprite): [https://github.com/jwunderl/arcade-sprite-util/blob/9ae94815999ebbedf8f670351d4f323e4be3d1d7/spriteutils.ts#L39](https://github.com/jwunderl/arcade-sprite-util/blob/9ae94815999ebbedf8f670351d4f323e4be3d1d7/spriteutils.ts#L39)

for the most part that extension is just “things that are super easy in typescript but a little tedious or not exposed in blocks”

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 14, 2020, 11:02pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/8 "2020-12-14T23:02:11Z")

</div>

I bet you guys make extensions to shorten math problems with 1 block. This is great! I like these extensions.

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 14, 2020, 11:23pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/9 "2020-12-14T23:23:45Z")

</div>

Ok so I’m making my game and every time I copy and paste, the thing crashes and it freezes up. I start over every time because it takes forever to load and it never does. Just a heads up because the crtl+c+v is going to crash all your work. Just saying as a bug… umm fix? or a bug problem.

---

<div class="post-metadata">

**Author:** ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)\
**Post date:** [December 15, 2020, 12:11am UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/10 "2020-12-15T00:11:46Z")

</div>

Hm, I haven’t seen that before! If it does happen again, there might be more info we can use to see what’s wrong, but it depends on the browser; usually pressing `F12` will pop up the ‘developer tools’ that show the files / etc that make up a website, and there will be a console that shows error messages – if there’s a big red one that shows up after you try to copy and paste that could tell us what is breaking. (or if you could share a link to the portion that’s crashing when you copy / paste or anything of the sort).

Also, as a general reminder I always add when mentioning the dev tools; be careful to check over anything from there before sending it to other people (and don’t run any code or type things that other people say to, unless you understand for sure what it’s doing) – some people will try to get you to paste in code that will send account information / etc to them. (Occasionally websites will drop messages with similar warnings into the log, just something to be aware of)

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 15, 2020, 12:20am UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/11 "2020-12-15T00:20:32Z")

</div>

Ok thanks! I will not edit the JavaScript in the whole code thing of the computer since I have no idea what im doing. im using the right click and duplicate so far. I’ll test it out with another project.

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 15, 2020, 12:30am UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/12 "2020-12-15T00:30:10Z")

</div>

Nevermind!! It started working!!

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 15, 2020, 12:30am UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/13 "2020-12-15T00:30:18Z")

</div>

When I try to turn on F12, it goes straight to airplane mode. I know right?! How do I prevent this? Also i can’t use control+shit+i and I cant inspect afterwards. I dunno why but yeah that is what is happening.

---

<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 15, 2020, 1:08am UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/14 "2020-12-15T01:08:06Z")

</div>

Some school Chromebooks (like mine) disable inspecting and dev tools.

---

<div class="post-metadata">

**Author:** ![Dreadmask197](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dreadmask197/32/10494_2.png) [@Dreadmask197](https://forum.makecode.com/u/Dreadmask197)\
**Post date:** [December 15, 2020, 4:42pm UTC](https://forum.makecode.com/t/how-do-i-make-a-distance-formula-in-block-code/5009/15 "2020-12-15T16:42:08Z")

</div>

Same here except I used my own computer with my school account. Hmm that explains it I guess
