
Untitled
By: a guest on
Jul 29th, 2012 | syntax:
None | size: 0.89 KB | hits: 15 | expires: Never
Hierarchical state machines for JavaScript
MyFSM = function() { // my constructor function
this.startup();
};
MyFSM.prototype = {
onpanic: function(event, from, to) { alert('panic'); },
onclear: function(event, from, to) { alert('all is clear'); },
// my other prototype methods
};
StateMachine.create({
target: MyFSM.prototype,
events: [
{ name: 'startup', from: 'none', to: 'green' },
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]});
function MyCalmFSM() {
MyFSM.apply(this, arguments);
}
MyCalmFSM.prototype = Object.create(MyFSM.prototype);
MyCalmFSM.prototype.constructor = MyCalmFSM;
// Don't panic
MyCalmFSM.prototype.onpanic = function(event, from, to) { alert("Don't panic"); }