InputManager09 / 15

Handling a button press

Send a restart event when the player presses the button.

Attach a component to the button. node.input.onTap handles mouse clicks and taps. Send RESTART_REQUESTED on a press and define the response to that event in the FSM.

import { BaseComponent } from '@gamedevland/engine/layouts';

export class RestartButtonComponent extends BaseComponent {
  private off: (() => void) | undefined;

  protected override onEnable(): void {
    this.off = this.node.input.onTap(() => {
      this.node.events.emit({ type: 'RESTART_REQUESTED', data: {} });
    });
  }
  protected override onDisable(): void {
    this.off?.();
    this.off = undefined;
  }
}
The player presses the buttonMouse or touch
onTapThe component sends RESTART_REQUESTED
FSMRestarts the game