Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Hierarchical state machines for JavaScript
  2. MyFSM = function() {    // my constructor function
  3.   this.startup();
  4. };
  5.  
  6. MyFSM.prototype = {
  7.  
  8.   onpanic: function(event, from, to) { alert('panic');        },
  9.   onclear: function(event, from, to) { alert('all is clear'); },
  10.  
  11.   // my other prototype methods
  12.  
  13. };
  14.  
  15. StateMachine.create({
  16.   target: MyFSM.prototype,
  17.   events: [
  18.     { name: 'startup', from: 'none',   to: 'green'  },
  19.     { name: 'warn',    from: 'green',  to: 'yellow' },
  20.     { name: 'panic',   from: 'yellow', to: 'red'    },
  21.     { name: 'calm',    from: 'red',    to: 'yellow' },
  22.     { name: 'clear',   from: 'yellow', to: 'green'  }
  23.   ]});
  24.        
  25. function MyCalmFSM() {
  26.     MyFSM.apply(this, arguments);
  27. }
  28.  
  29. MyCalmFSM.prototype = Object.create(MyFSM.prototype);
  30. MyCalmFSM.prototype.constructor = MyCalmFSM;
  31.  
  32. // Don't panic
  33. MyCalmFSM.prototype.onpanic = function(event, from, to) { alert("Don't panic"); }