From Test-Scratch-Wiki

與 () 的間距
與 [角色 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
end
  • 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)))))

參見


參考

Cookies help us deliver our services. By using our services, you agree to our use of cookies.