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 | SnailBait.prototype = { draw: function (now) { this.setPlatformVelocity(); this.setTranslationOffsets(); this.drawBackground(); this.updateSprites(now); this.drawSprites(); }, setPlatformVelocity: function () { // Setting platform velocity was discussed in the second article in this series this.platformVelocity = this.bgVelocity * this.PLATFORM_VELOCITY_MULTIPLIER; }, setTranslationOffsets: function () { // Setting the background translation offset was discussed // in the second article in this series this.setBackgroundTranslationOffset(); this.setSpriteTranslationOffsets(); }, setSpriteTranslationOffsets: function () { var i, sprite; this.spriteOffset += this.platformVelocity / this.fps; // In step with platforms for (i=0; i < this.sprites.length; ++i) { sprite = this.sprites; if ('runner' !== sprite.type) { sprite.offset = this.platformOffset; // In step with platforms } } }, ... }; |
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 | SnailBait.prototype = { ... updateSprites: function (now) { var sprite; for (var i=0; i < this.sprites.length; ++i) { sprite = this.sprites; if (sprite.visible && this.spriteInView(sprite)) { sprite.update(now, this.fps); } } }, drawSprites: function() { var sprite; for (var i=0; i < this.sprites.length; ++i) { sprite = this.sprites; if (sprite.visible && this.spriteInView(sprite)) { this.context.translate(-sprite.offset, 0); sprite.draw(this.context); this.context.translate(sprite.offset, 0); } } }, spriteInView: function(sprite) { return sprite === this.runner || // runner is always visible (sprite.left + sprite.width > this.platformOffset && sprite.left < this.platformOffset + this.canvas.width); }, |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |