me.state

a State Manager (state machine)

Members

static, constant CREDITS

default state ID for Credits Stage

static, constant GAME_END

default state ID for Game End Stage

static, constant GAMEOVER

default state ID for Game Over Stage

static, constant LOADING

default state ID for Loading Stage

static, constant MENU

default state ID for Menu Stage

static, constant PLAY

default state ID for Play Stage

static, constant READY

default state ID for "Ready" Stage

static, constant SCORE

default state ID for High Score Stage

static, constant SETTINGS

default state ID for Settings Stage

static, constant SETTINGS

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

static, constant USER

default state ID for user defined constants

Example
var STATE_INFO = me.state.USER + 0;
var STATE_WARN = me.state.USER + 1;
var STATE_ERROR = me.state.USER + 2;
var STATE_CUTSCENE = me.state.USER + 3;

Methods

static change(state, forceChange)

change the game/app state

Parameters:
Name Type Attributes Description
state Number

State ID (see constants)

forceChange Boolean

if true the state will be changed immediately

arguments... <optional>

extra arguments to be passed to the reset functions

Example
// The onResetEvent method on the play screen will receive two args:
// "level_1" and the number 3
me.state.change(me.state.PLAY, "level_1", 3);

static current() → {me.Stage}

return a reference to the current screen object
useful to call a object specific method

Returns:
me.Stage

static isCurrent(state)

return true if the specified state is the current one

Parameters:
Name Type Description
state Number

State ID (see constants)

static isPaused() → {Boolean}

Return the pause state of the state manager

Returns:
Boolean -

true if the game is paused

static isRunning() → {Boolean}

return the running state of the state manager

Returns:
Boolean -

true if a "process is running"

static onPause()

onPause callback

static onRestart()

onRestart callback

static onResume()

onResume callback

static onStop()

onStop callback

static pause(pauseTrack)

pause the current screen object

Parameters:
Name Type Description
pauseTrack Boolean

pause current track on screen pause

static restart(resumeTrack)

Restart the screen object from a full stop.

Parameters:
Name Type Description
resumeTrack Boolean

resume current track on screen resume

static resume(resumeTrack)

resume the screen object

Parameters:
Name Type Description
resumeTrack Boolean

resume current track on screen resume

static set(state, stage, startopt)

associate the specified state with a Stage

Parameters:
Name Type Attributes Default Description
state Number

State ID (see constants)

stage me.Stage

Instantiated Stage to associate with state ID

start Boolean <optional>
false

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

Example
var MenuButton = me.GUI_Object.extend({
    "onClick" : function () {
        // Change to the PLAY state when the button is clicked
        me.state.change(me.state.PLAY);
        return true;
    }
});

var MenuScreen = me.Stage.extend({
    onResetEvent: function() {
        // Load background image
        me.game.world.addChild(
            new me.ImageLayer(0, 0, {
                image : "bg",
                z: 0 // z-index
            }
        );

        // Add a button
        me.game.world.addChild(
            new MenuButton(350, 200, { "image" : "start" }),
            1 // z-index
        );

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

    "onDestroyEvent" : function () {
        // Stop music
        me.audio.stopTrack();
    }
});

me.state.set(me.state.MENU, new MenuScreen());

static setTransition(state, enable)

enable/disable transition for a specific state (by default enabled for all)

Parameters:
Name Type Description
state Number

State ID (see constants)

enable Boolean

static stop(pauseTrack)

Stop the current screen object.

Parameters:
Name Type Description
pauseTrack Boolean

pause current track on screen stop.

static transition(effect, color, durationopt)

specify a global transition effect

Parameters:
Name Type Attributes Default Description
effect String

(only "fade" is supported for now)

color me.Color | String

a CSS color value

duration Number <optional>
1000

expressed in milliseconds