From Test-Scratch-Wiki

Translate: - English - 中文 
与 () 的间距
與 [角色 v] 的間距
类别 侦测类积木
形狀 椭圆形积木

The Distance to () 积木 is a 侦测类积木 and a 椭圆形积木. The block reports the 距离 (a measurement of pixels) between it and a specified 角色造型中心点, or the 滑鼠游标.


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

Some common uses for the Distance to () block:

  • Estimating when collisions will occur
如果 <(與 [snowball v] 的間距) < (40)> 那麼 
  說出 [Watch out!]
  等待直到 <碰到 [snowball v] ?>
  說出 [] // stop saying the message
  • Helping determine how long it should take a sprite to move somewhere, judging from the distance
變數 [movements v] 設為 ([ceiling v] 數值 ((與 [meteor v] 的間距) / (10))) // "movements" being the amount of 10 pixel motions to reach the meteor
  • Determining how far a rocket should fire
變數 [real distance v] 設為 (字串組合 ((與 [asteroid v] 的間距) * (10000)) 和 [ kilometers]) // for relating pixel distance to real life


Main article: List of Block Workarounds

This block can be replicated with the following code:

([sqrt v] 數值 ((((x座標) - ([x座標 v] \( [Wanted Sprite v] \))) * ((x座標) - ([x座標 v] \( [Wanted Sprite v] \)))) + (((y 座標) - ([y 座標 v] \( [Wanted Sprite v] \))) * ((y 座標) - ([y 座標 v] \( [Wanted Sprite v] \))))))

Related Suggestion

Some Scratchers want a block that gives the distance to any location, not just a 角色.[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] 數值 ((((x座標) - (wanted x)) * ((x座標) - (wanted x))) + (((y 座標) - (wanted y)) * ((y 座標) - (wanted y)))))