state
namespace state
a State Manager (state machine)
Summary
Properties from state
number |
|
number |
|
number |
|
number |
|
number |
|
number |
|
number |
|
number |
|
number |
|
number |
|
number |
|
Methods from state
|
|
Stage |
|
boolean |
|
boolean |
|
boolean |
|
|
|
|
|
|
|
|
|
Stage |
|
|
|
|
|
|
Public Properties
static DEFAULT: number = 9
number
default state ID for the default Stage (the default stage is the one running as soon as melonJS is started)
static USER: number = 100
number
default state ID for user defined constants
let STATE_INFO = me.state.USER + 0;
let STATE_WARN = me.state.USER + 1;
let STATE_ERROR = me.state.USER + 2;
let STATE_CUTSCENE = me.state.USER + 3;
Public Methods
change(state: number, forceChange: boolean, args: unknown) → {}
change the game/app state
// 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);
Name | Type | Attributes | Description |
---|---|---|---|
state | number |
State ID (see constants) |
|
forceChange | boolean |
if true the state will be changed immediately |
|
args | unknown |
<optional> |
extra arguments to be passed to the reset functions |
current() → {Stage}
return a reference to the current stage
useful to call a object specific method
Type | Description |
---|---|
Stage |
isCurrent(state: number) → {boolean}
return true if the specified state is the current one
Name | Type | Description |
---|---|---|
state | number |
State ID (see constants) |
Type | Description |
---|---|
boolean |
true if the specified state is the current one |
isPaused() → {boolean}
Return the pause state of the state manager
Type | Description |
---|---|
boolean |
true if the game is paused |
isRunning() → {boolean}
return the running state of the state manager
Type | Description |
---|---|
boolean |
true if a "process is running" |
pause(music: boolean) → {}
pause the current stage
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
music | boolean |
<optional> |
false |
pause current music track on screen pause |
restart(music: boolean) → {}
Restart the current stage from a full stop.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
music | boolean |
<optional> |
false |
resume current music track on screen resume |
resume(music: boolean) → {}
resume the current stage
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
music | boolean |
<optional> |
false |
resume current music track on screen resume |
set(state: number, stage: Stage, start : boolean) → {}
associate the specified state with a Stage
class MenuButton extends me.GUI_Object {
onClick() {
// Change to the PLAY state when the button is clicked
me.state.change(me.state.PLAY);
return true;
}
};
class MenuScreen extends me.Stage {
onResetEvent() {
// 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() {
// Stop music
me.audio.stopTrack();
}
};
me.state.set(me.state.MENU, new MenuScreen());
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
state | number |
State ID (see constants) |
||
stage | Stage |
Instantiated Stage to associate with state ID |
||
start | boolean |
<optional> |
false |
if true the state will be changed immediately after adding it. |
set(state: number) → {Stage}
returns the stage associated with the specified state (or the current one if none is specified)
Name | Type | Attributes | Description |
---|---|---|---|
state | number |
<optional> |
State ID (see constants) |
Type | Description |
---|---|
Stage |
setTransition(state: number, enable: boolean) → {}
enable/disable the transition to a particular state (by default enabled for all)
Name | Type | Description |
---|---|---|
state | number |
State ID (see constants) |
enable | boolean |
stop(pauseTrack: boolean) → {}
Stop the current stage.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
pauseTrack | boolean |
<optional> |
false |
pause current track on screen stop. |
transition(effect: string, color: Color | string, duration: number) → {}
specify a global transition effect
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
effect | string |
(only "fade" is supported for now) |
||
color | Color | string |
a CSS color value |
||
duration | number |
<optional> |
1000 |
expressed in milliseconds |