Skip to content

Animation

The Animation class handles sprite animations by cycling through a series of frames (sprites) from a sprite sheet, akin to a flip book. It uses the Timer class to control how long each frame is displayed before switching to the next. This class allows you to define an animation loop, specify the interval between frame switches, and limit the number of cycles an animation should run.

Constructor

constructor(frames, (interval = 0), (cycles = 0));
  • frames (Sprite[]): An array of Sprite objects representing the frames of the animation.
  • interval (number): The time (in seconds) between frame changes. Defaults to 0.
  • cycles (number): The number of times the animation should cycle through all frames. A value of 0 means infinite cycles. Defaults to 0.

Methods

update(dt)

Updates the animation’s internal timer based on the delta time (dt). If the animation has more than one frame, it increments the current frame after each interval.

  • dt (number): The time that has passed since the last update (in seconds).

startTimer()

Starts the animation timer, which increments the current frame at each interval. If the animation reaches the last frame, it loops back to the beginning. If the number of cycles has been defined, it stops after the specified number of cycles.

getCurrentFrame()

Returns the current frame (sprite) to be used for rendering.

  • Returns: Sprite – The current frame of the animation.

refresh()

Resets the animation to the first frame and resets the cycle counter to 0. This is useful for restarting the animation.

isDone()

Checks whether the animation has completed all its cycles.

  • Returns: booleantrue if the animation has completed its cycles, otherwise false.

isHalfwayDone()

Checks if the animation has reached or passed the halfway point of its total number of frames.

  • Returns: booleantrue if the animation is halfway or more through its frames, otherwise false.