# How do I make a distance formula in block code

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.

hihi here’s a sample program!

I made a distance formula function that takes in two sprites

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!

4 Likes

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

5 Likes

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?

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:

1 Like

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.

1 Like

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

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

1 Like

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

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.

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)

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.

Nevermind!! It started working!!

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.

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

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

1 Like