1 2 3 4 5 6 7 | // Sprite Animators........................................................... var SpriteAnimator = function (cells, duration, callback) { this.cells = cells; this.duration = duration || 1000; this.callback = callback; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | SpriteAnimator.prototype = { start: function (sprite, reappear) { var originalCells = sprite.artist.cells, originalIndex = sprite.artist.cellIndex, self = this; sprite.artist.cells = this.cells; sprite.artist.cellIndex = 0; setTimeout(function() { sprite.artist.cells = originalCells; sprite.artist.cellIndex = originalIndex; sprite.visible = reappear; if (self.callback) { self.callback(sprite, self); } }, self.duration); }, }; |
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 31 32 33 34 35 36 37 38 39 40 41 42 | var SnailBait = function () { this.canvas = document.getElementById('game-canvas'), this.context = this.canvas.getContext('2d'), ... this.RUN_ANIMATION_RATE = 17, // frames/second this.EXPLOSION_CELLS_HEIGHT = 62, // pixels this.EXPLOSION_DURATION = 500, // milliseconds this.explosionCells = [ { left: 1, top: 48, width: 50, height: this.EXPLOSION_CELLS_HEIGHT }, { left: 60, top: 48, width: 68, height: this.EXPLOSION_CELLS_HEIGHT }, { left: 143, top: 48, width: 68, height: this.EXPLOSION_CELLS_HEIGHT }, { left: 230, top: 48, width: 68, height: this.EXPLOSION_CELLS_HEIGHT }, { left: 305, top: 48, width: 68, height: this.EXPLOSION_CELLS_HEIGHT }, { left: 389, top: 48, width: 68, height: this.EXPLOSION_CELLS_HEIGHT }, { left: 470, top: 48, width: 68, height: this.EXPLOSION_CELLS_HEIGHT } ], ... this.explosionAnimator = new SpriteAnimator( this.explosionCells, // Animation cells this.EXPLOSION_DURATION, // Duration of the explosion function (sprite, animator) { // Callback after animation sprite.exploding = false; if (sprite.jumping) { sprite.stopJumping(); } else if (sprite.falling) { sprite.stopFalling(); } sprite.visible = true; sprite.track = 1; sprite.top = snailBait.calculatePlatformTop(sprite.track) - sprite.height; sprite.artist.cellIndex = 0; sprite.runAnimationRate = snailBait.RUN_ANIMATION_RATE; } ); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 | SnailBait.prototype = { ... explode: function (sprite, silent) { if (sprite.runAnimationRate === 0) { sprite.runAnimationRate = this.RUN_ANIMATION_RATE; } sprite.exploding = true; this.explosionAnimator.start(sprite, true); // true means sprite reappears }, }; |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |