From Test-Scratch-Wiki

Translate: - English - 中文 
Distance to ()
2.0 Distance to ().png
Category Sensing
Type Reporter

The Distance to () block is a Sensing block and a Reporter block. The block reports the distance (a measurement of pixels) between it and a specified sprite's costume center, or the mouse-pointer.

Example Uses

As this block can give distances between objects, it is very useful in projects that require a great deal of careful sensing and movement.

Some common uses for the Distance to () block:

  • Estimating when collisions will occur
if <(distance to [snowball v]) < (40)> then
say [Watch out!]
wait until <touching [snowball v]?>
say [] //stop saying the message
  • Helping determine how long it should take a sprite to move somewhere, judging from the distance
set [movements v] to ([ceiling v] of ((distance to [meteor v]) / (10))) //"movements" being the amount of 10 pixel motions to reach the meteor
  • Determining how far a rocket should fire
set [real distance v] to (join ((distance to [asteroid v]) * (10000)) [ kilometers]) //for relating pixel distance to real life

Workaround

Main article: List of Block Workarounds

This block can be replicated with the following code:

([sqrt v] of ((((x position) - ([x position v] of [Wanted Sprite v])) * ((x position) - ([x position v] of [Wanted Sprite v]))) + (((y position) - ([y position v] of [Wanted Sprite v])) *((y position) - ([y position v] of [Wanted Sprite v])))))

Related Suggestion

Some Scratchers want a block that gives the distance to any location, not just a sprite.[1] It would be like this:

(distance to x: () y: ())//category=sensing

It is technically unnecessary because it can be replicated with a similar [[Eng:#Workaround|workaround]]:

([sqrt v] of ((((x position) - (wanted x)) * ((x position) - (wanted x))) + (((y position) - (wanted y)) * ((y position) - (wanted y)))))

See Also


References

  1. https://scratch.mit.edu/discuss/topic/109628/?page=1#post-963174