Distance and Angle

Work out how far apart two sprites are, and which way one is from the other.

Distance

Distance takes two points and gives the straight-line gap between them in pixels. Each point is an across and a down number, so it takes four numbers:

Distance(X("Hero"), Y("Hero"), X("Ghost"), Y("Ghost"))

That is how far apart the hero and the ghost are. Use it with Compare two values from the ⚙ Special column:

WhenDistance(X("Hero"), Y("Hero"), X("Ghost"), Y("Ghost")) is < 100
DoPlay sound Spooky tuneGhost steps 2 pixels towards Hero

Angle

Angle takes the same four numbers and gives the direction from the first point to the second, as a number from 0 to 31. That is the same scale as NutHouse’s direction wheel: 0 is right, 8 is up, 16 is left, 24 is down.

Angle(X("Turret"), Y("Turret"), X("Hero"), Y("Hero")) is the direction a turret must face to point at the hero. Feed it to Set direction to a number:

WhenAlways
DoSet direction of Turret to Angle(X("Turret"), Y("Turret"), X("Hero"), Y("Hero"))

How to

  1. Click + New event, then ⚙ Special, then Compare two values.
  2. In First value, type Distance(X("Hero"), Y("Hero"), X("Ghost"), Y("Ghost")). The ƒx helper can insert each part.
  3. Click < less than. In Second value, type 100. Press Add.
  4. Add the actions in the row.

The easy way for most games

You often do not need the formula. Is near another object under a sprite’s Position tests “closer than N pixels” and picks only the close copies, which the formula cannot do. The Chases another object movement has an Only when closer than box built in. And Turn to face another object turns a sprite to face another one in a 3D level.

Use Distance and Angle when you want the number itself: to show it, to compare two distances, or to shoot in a worked-out direction.

Other points

The four numbers can be anything. Distance(X("Hero"), Y("Hero"), MouseX, MouseY) is how far the pointer is from the hero. Angle(X("Ship"), Y("Ship"), MouseX, MouseY) points the ship at the mouse. Distance(0, 0, X("Hero"), Y("Hero")) is the distance from the top-left corner.

Still stuck?

Try the Chocobreak tutorial, open a sample game and read its rules, or look at the questions people ask.