1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | SnailBait.prototype = { ... createRubySprites: function () { var ruby, rubyArtist = new SpriteSheetArtist(this.spritesheet, this.rubyCells); for (var i = 0; i < this.rubyData.length; ++i) { ruby = new Sprite('ruby', rubyArtist, [ new CycleBehavior(100, // animation duration 500) ]); // interval between animations ... } }, ... }; |
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 | // This behavior advances the sprite artist through // the sprite's images at a specified animation rate. CycleBehavior = function (duration, interval) { this.duration = duration || 0; // milliseconds this.interval = interval || 0; this.lastAdvance = 0; }; CycleBehavior.prototype = { execute: function(sprite, time, fps) { if (this.lastAdvance === 0) { this.lastAdvance = time; } // During the interval start advancing if the interval is over if (this.interval && sprite.artist.cellIndex === 0) { if (time - this.lastAdvance < this.interval) { sprite.artist.advance(); this.lastAdvance = time; } } // Otherwise, if the behavior is cycling, advance if duration is over else if (time - this.lastAdvance > this.duration) { sprite.artist.advance(); this.lastAdvance = time; } } }; |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |