June 24, 2014
function Hero() { } Hero.prototype = { handleInput: function(event) { if (event.type == BUTTON_B) { this.jump(); } }, jump: function() { // animate jump... } };
Hero.prototype = { handleInput: function(event) { var self = this; if (event.type == BUTTON_B) { if (!this._isJumping) { this._isJumping = true; this.jump(function() { self._isJumping = false; }); } } }, jump: function(callback) { // animate jump, then invoke callback. } };
Hero.prototype = { handleInput: function(event) { var self = this; if (event.type == BUTTON_B) { if (!this._isJumping) { this._isJumping = true; this.jump(function() { self._isJumping = false; }); } } else if (event.type == PRESS_DOWN) { if (!this._isJumping) { this.duck(); } } else if (event.type == RELEASE_DOWN) { this.stand(); } }, jump: function(callback) {}, duck: function() {}, stand: function() {} };
Hero.prototype = { handleInput: function(event) { var self = this; if (event.type == BUTTON_B) { if (!this._isJumping && !this._isDucking) { this._isJumping = true; this.jump(function() { self._isJumping = false; }); } } else if (event.type == PRESS_DOWN) { if (!this._isJumping) { this._isDucking = true; this.duck(); } } else if (event.type == RELEASE_DOWN) { if (this._isDucking) { this._isDucking = false; this.stand(); } } }, jump: function(callback) {}, duck: function() {}, stand: function() {} };
Hero.prototype = { handleInput: function(event) { var self = this; if (event.type == BUTTON_B) { if (!this._isJumping && !this._isDucking) { this._isJumping = true; this.jump(function() { self._isJumping = false; }); } } else if (event.type == PRESS_DOWN) { if (!this._isJumping) { this._isDucking = true; this.duck(); } else { this._isJumping = false; this.dive(); } } else if (event.type == RELEASE_DOWN) { if (this._isDucking) { this._isDucking = false; this.stand(); } } }, jump: function(callback) {}, duck: function() {}, stand: function() {}, dive: function() {} };
function Hero() { var self = this; this._stateMachine = State.define(function() { this.state('standing', function() { this.enter(function() { self.stand(); }); this.event('pressB', function() { this.goto('jumping'); }); }); this.state('ducking', function() { this.enter(function() { self.duck(); }); this.event('releaseDown', function() { this.goto('standing'); }); }); this.state('jumping', function() { this.enter(function() { self.jump(); }); this.event('pressDown', function() { this.goto('diving'); }); this.event('jumpFinished', function() { this.goto('standing'); }); }); this.state('diving', function() { this.enter(function() { self.dive(); }); this.event('diveFinished', function() { this.goto('standing'); }); }); }); } Hero.prototype = { jump: function() { // do jump this._stateMachine.send('jumpFinished'); }, duck: function() {}, stand: function() {}, dive: function() { // do dive this._stateMachine.send('diveFinished'); } };
State.define(function() { this.state('P', function() { this.state('C1'); this.state('C2'); }); });
State.define({concurrent: true}, function() { this.state('P1', function() { this.state('A'); this.state('B'); }); this.state('P2', function() { this.state('C'); this.state('D'); }); this.state('P3', function() { this.state('E'); this.state('F'); }); });
this.state('radio', {H: true}, function() { this.state('off'); this.state('on'); });