melonJS
    Preparing search index...

    Variable stateConst

    state: {
        CREDITS: 7;
        DEFAULT: 9;
        GAME_END: 5;
        GAMEOVER: 4;
        LOADING: 0;
        MENU: 1;
        PLAY: 3;
        READY: 2;
        SCORE: 6;
        SETTINGS: 8;
        USER: 100;
        change(
            stateId: number,
            forceChange?: boolean,
            ...extraArgs: unknown[],
        ): void;
        current(): Stage | undefined;
        get(stateId?: number): Stage | undefined;
        isCurrent(stateId: number): boolean;
        isPaused(): boolean;
        isRunning(): boolean;
        pause(music?: boolean): void;
        restart(music?: boolean): void;
        resume(music?: boolean): void;
        set(stateId: number, stage: Stage, start?: boolean): void;
        setTransition(stateId: number, enable: boolean): void;
        stop(shouldPauseTrack?: boolean): void;
        transition(effect: string, color: string, duration?: number): void;
    } = ...

    a State Manager (state machine)

    Type Declaration

    • CREDITS: 7

      default state ID for Credits Stage

    • DEFAULT: 9

      default state ID for the default Stage (the default stage is the one running as soon as melonJS is started)

    • GAME_END: 5

      default state ID for Game End Stage

    • GAMEOVER: 4

      default state ID for Game Over Stage

    • LOADING: 0

      default state ID for Loading Stage

    • default state ID for Menu Stage

    • PLAY: 3

      default state ID for Play Stage

    • READY: 2

      default state ID for "Ready" Stage

    • SCORE: 6

      default state ID for High Score Stage

    • SETTINGS: 8

      default state ID for Settings Stage

    • USER: 100

      default state ID for user defined constants

      const STATE_INFO = state.USER + 0;
      const STATE_WARN = state.USER + 1;
      const STATE_ERROR = state.USER + 2;
      const STATE_CUTSCENE = state.USER + 3;
    • change: function
      • change the game/app state

        Parameters

        • stateId: number

          State ID (see constants)

        • OptionalforceChange: boolean = false

          if true the state will be changed immediately (without waiting for the next frame)

        • ...extraArgs: unknown[]

          extra arguments to be passed to the reset functions

        Returns void

        // The onResetEvent method on the play screen will receive two args:
        // "level_1" and the number 3
        state.change(state.PLAY, false, "level_1", 3);
    • current: function
      • return a reference to the current stage
        useful to call a object specific method

        Returns Stage | undefined

        the current Stage instance, or undefined if no stage is active

    • get: function
      • returns the stage associated with the specified state (or the current one if none is specified)

        Parameters

        • OptionalstateId: number = _state

          State ID (see constants)

        Returns Stage | undefined

        the Stage instance associated with the given state ID, or undefined

    • isCurrent: function
      • return true if the specified state is the current one

        Parameters

        • stateId: number

          State ID (see constants)

        Returns boolean

        true if the specified state is the current one

    • isPaused: function
    • isRunning: function
    • pause: function
      • pause the current stage

        Parameters

        • Optionalmusic: boolean = false

          pause current music track on screen pause

        Returns void

    • restart: function
      • Restart the current stage from a full stop.

        Parameters

        • Optionalmusic: boolean = false

          resume current music track on screen resume

        Returns void

    • resume: function
      • resume the current stage

        Parameters

        • Optionalmusic: boolean = false

          resume current music track on screen resume

        Returns void

    • set: function
      • associate the specified state with a Stage

        Parameters

        • stateId: number

          State ID (see constants)

        • stage: Stage

          Instantiated Stage to associate with state ID

        • Optionalstart: boolean = false

          if true the state will be changed immediately after adding it.

        Returns void

        class MenuScreen extends Stage {
        onResetEvent(app) {
        // Load background image
        app.world.addChild(
        new ImageLayer(0, 0, {
        image : "bg",
        z: 0 // z-index
        })
        );

        // Play music
        audio.playTrack("menu");
        }

        onDestroyEvent() {
        // Stop music
        audio.stopTrack();
        }
        }

        state.set(state.MENU, new MenuScreen());
    • setTransition: function
      • enable/disable the transition to a particular state (by default enabled for all)

        Parameters

        • stateId: number

          State ID (see constants)

        • enable: boolean

          true to enable transition, false to disable

        Returns void

    • stop: function
      • Stop the current stage.

        Parameters

        • OptionalshouldPauseTrack: boolean = false

          pause current track on screen stop.

        Returns void

    • transition: function
      • specify a global transition effect

        Parameters

        • effect: string

          (only "fade" is supported for now)

        • color: string

          a CSS color value

        • Optionalduration: number

          expressed in milliseconds

        Returns void