One of 3 main API entry points. This one provides you with the useful utils.

BeetPxUtils.clamp($v_0_0, this.playerXy, BeetPx.canvasSize);

Methods

  • This function is meant to be used in a last branch of if - else if - … - else chain or in default of switch - case - case - …. Let's imagine there is a union type of which we check all possible cases. Someday we add one more type to the union, but we forget to extend our switch by that one more case. Thanks to assertUnreachable(theValueOfThatUnionType) the TypeScript checker will inform us about such mistake.

    Parameters

    • thingThatShouldBeOfTypeNeverAtThisPoint: never

      a value which we expect to be of type never

    Returns void

  • Parameters

    • n: number

      How often to change the returned value

    • Optionalopts: {
          onGamePause?: "pause" | "ignore";
      }
      • OptionalonGamePause?: "pause" | "ignore"

        By default the method doesn't progress during the game pause. But with this param set to "ignore" we can change that behaviour.

    Returns boolean

    Either true or false, which changes every n frames

  • Returns the middle number out of given three numbers. Useful for keeping a given property within specified bounds.

    Parameters

    • a: number
    • b: number
    • c: number

    Returns number

    clamp(minSpeed, currentSpeed, maxSpeed);
    
  • A simple helper which returns what it takes, without any changes.

    Type Parameters

    • Param

    Parameters

    Returns Param

    const doTheFancyTransformation = (x: value) => ...;
    const fn = makeItFancy ? doTheFancyTransformation : BeetPxUtils.identity;
    const newX = fn(x);
  • Picks a number between a and b which is in a "distance" between them as specified by t. Specifically: lerp(a,b,0) === a and lerp(a,b,1) === b.

    With opts: { clamp: true }, the resulting value cannot is always within bounds of a and b, even if t is below 0 or above 1.

    Parameters

    • a: number
    • b: number
    • t: number
    • Optionalopts: {
          clamp?: boolean;
      }
      • Optionalclamp?: boolean

    Returns number

  • A modulo operation – in contrary to native JavaScript's %, this one returns results from [0, n) range (positive values only).

    Parameters

    • value: number
    • modulus: number

    Returns number

    if ($x.wasButtonJustPressed("up")) {
    selected = BeetPxUtils.mod(selected - 1);
    }
    const menuItem = menuItems[selected];
  • A simple helper which does nothing.

    Returns void

    const doTheFancyThing = () => ...;
    const fn = makeItFancy ? doTheFancyThing : BeetPxUtils.noop;
    fn();
  • Generates an array from 0 to n-1. Useful when we want to do a thing N times.

    Parameters

    • n: number

    Returns number[]

    BeetPxUtils.range(10).forEach(i => {
    BeetPxDraw.rect($v(1, 1 + i * 8), $v(40, 7), $rgb_red);
    });
  • Takes an array an returns a new one, in which each element is repeated given amount of times.

    Type Parameters

    • TElement

    Parameters

    Returns TElement[]

    BeetPxUtils.repeatEachElement(3, ["a", "b"]);
    // The above produces `["a", "a", "a", "b", "b", "b"]`.
  • To be used as a value, e.g. in definedValue: maybeUndefined() ?? throwError("…").

    Parameters

    • message: string

    Returns never

    function getValue(): number | null {
    // ...
    }
    const value = getValue() ?? throwError("Failed to get the value");
  • Parameters

    • x: number
    • y: number

    Returns number

    Turn angle. A full circle turn = 1. In other words: 0 deg = 0 turn, 90 deg = 0.25 turn, 180 deg = 0.5 turn, 270 deg = 0.75 turn.

  • Parameters

    • turnAngle: number

      A full circle turn = 1. In other words: 0 deg = 0 turn, 90 deg = 0.25 turn, 180 deg = 0.5 turn, 270 deg = 0.75 turn.

    Returns number

  • Parameters

    • turnAngle: number

      A full circle turn = 1. In other words: 0 deg = 0 turn, 90 deg = 0.25 turn, 180 deg = 0.5 turn, 270 deg = 0.75 turn.

    Returns number