Skip to content

Instantly share code, notes, and snippets.

@Josh-Miller
Created December 18, 2015 18:24
Show Gist options
  • Select an option

  • Save Josh-Miller/678e211edb6755e9a08e to your computer and use it in GitHub Desktop.

Select an option

Save Josh-Miller/678e211edb6755e9a08e to your computer and use it in GitHub Desktop.
A state manager for Phaser v2 html5 game platform
'use strict';
export const stateManager = {
state: [],
states: {},
currentState: function() {
return this.state;
},
addState: function(state, expiration, fn) {
this.state.unshift(state);
if (expiration) {
this.expireState(state, expiration, fn);
}
},
removeState: function(state) {
this.state.splice(this.state.indexOf(state));
},
registerState: function(name, state) {
this.states[name] = state;
},
expireState: function(state, expiration, fn) {
setTimeout(() => {
this.removeState(state);
if (fn) fn();
}, expiration);
},
fireStates: function() {
this.currentState().map(currentState => {
this.states[currentState]();
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment