1 2 3 4 5 6 7 8 9 10 11 12 | var SnailBait = function () { ... this.runner = new Sprite('runner', // Type this.runnerArtist, // Artist [this.runBehavior, // Behaviors this.jumpBehavior, this.fallBehavior, this.runnerCollideBehavior ]); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | var SnailBait = function () { ... this.runBehavior = { execute: function(sprite, time, fps) { // sprite is the runner ... } }; this.jumpBehavior = { execute: function(sprite, time, fps) { // sprite is the runner ... } }; this.fallBehavior = { execute: function(sprite, time, fps) { // sprite is the runner ... } }; this.runnerCollideBehavior = { execute: function(sprite, time, fps) { // sprite is the runner ... } }; }; |
1 2 3 4 5 6 7 8 9 10 11 | Sprite.prototype = { update: function (time, fps) { for (var i=0; i < this.behaviors.length; ++i) { if (this.behaviors === undefined) { // You never know return; } this.behaviors.execute(this, time, fps); } } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | var SnailBait = function () { ... this.BACKGROUND_VELOCITY = 32, // pixels/second this.RUN_ANIMATION_RATE = 17, // frames/second ... this.runAnimationRate, this.runBehavior = { // Every milliseconds, this behavior advances the // runner's artist to the next frame of the sprite sheet. lastAdvanceTime: 0, execute: function(sprite, time, fps) { if (sprite.runAnimationRate === 0) { return; } if (this.lastAdvanceTime === 0) { // skip first time this.lastAdvanceTime = time; } else if (time - this.lastAdvanceTime > 1000 / sprite.runAnimationRate) { sprite.artist.advance(); this.lastAdvanceTime = time; } } }, ... }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | SnailBait.prototype = { ... turnLeft: function () { this.bgVelocity = -this.BACKGROUND_VELOCITY; this.runner.runAnimationRate = this.RUN_ANIMATION_RATE; // 17 fps, see <a href="#listing4">Listing 4</a> this.runnerArtist.cells = this.runnerCellsLeft; this.runner.direction = this.LEFT; }, turnRight: function () { this.bgVelocity = this.BACKGROUND_VELOCITY; this.runner.runAnimationRate = this.RUN_ANIMATION_RATE; // 17 fps, see <a href="#listing4">Listing 4</a> this.runnerArtist.cells = this.runnerCellsRight; this.runner.direction = this.RIGHT; }, }; |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |