API Reference › Math › Math.sign
method

Math.sign

Math.sign(x) -> int

Returns -1 when x is negative, 1 when it is positive, and 0 when it is exactly zero. It works on both int and fixed values, since zero and sign are the same in Q16.16. Multiply a step by Math.sign(dx) to move one unit toward a target, or read the sign of a velocity to decide which way a sprite faces.

Parameters

xthe value to test

Example

program FaceTarget {
  var x: int = 8
  var target: int = 20

  handler StepToward phase Update {
    x = x + Math.sign(target - x)   # +1 while target is to the right
  }
}
← All symbols