Skip to content

State

The State class is an abstract base class for creating different states within a StateMachine. Each state can define its own behavior for entering, exiting, updating, and rendering.

Constructor

new State();

Creates a new State instance.

  • Parameters: None

  • Returns: A new State instance.

  • Example:

    class IdleState extends State {
    // Custom implementation for the Idle state
    }

Methods

enter([enterParameters])

Called when the state is entered. This method is intended to be overridden by subclasses to define custom behavior when the state is activated.

  • Parameters:

    • enterParameters (any, optional): Any parameters passed to the state upon entering.
  • Returns: void

  • Example:

    enter({ speed: 10 }) {
    // Custom behavior when entering the state
    }

exit()

Called when the state is exited. This method is intended to be overridden by subclasses to define custom behavior when the state is deactivated.

  • Parameters: None

  • Returns: void

  • Example:

    exit() {
    // Custom behavior when exiting the state
    }

update(dt)

Called every frame to update the state. This method is intended to be overridden by subclasses to define custom update logic.

  • Parameters:

    • dt (number): The time delta between frames, typically used for updating animations or physics.
  • Returns: void

  • Example:

    update(dt) {
    // Custom update logic
    }

render(context)

Called every frame to render the state. This method is intended to be overridden by subclasses to define custom rendering logic.

  • Parameters:

    • context (CanvasRenderingContext2D): The canvas rendering context where the state will be rendered.
  • Returns: void

  • Example:

    render(context) {
    // Custom rendering logic
    }