|
59 | 59 | // default animation sequence |
60 | 60 | this.current = null; |
61 | 61 |
|
| 62 | + // animation frame delta |
| 63 | + this.dt = 0; |
| 64 | + |
62 | 65 | // default animation speed (ms) |
63 | 66 | this.animationspeed = 100; |
64 | 67 |
|
|
115 | 118 | frames : [], |
116 | 119 | idx : 0, |
117 | 120 | length : 0, |
118 | | - animationspeed: animationspeed || this.animationspeed, |
119 | | - nextFrame : 0 |
| 121 | + animationspeed: animationspeed || this.animationspeed |
120 | 122 | }; |
121 | 123 |
|
122 | 124 | if (index == null) { |
|
203 | 205 | this.current = this.anim[name]; |
204 | 206 | this.resetAnim = resetAnim || null; |
205 | 207 | this.setAnimationFrame(this.current.idx); // or 0 ? |
206 | | - this.current.nextFrame = this.current.animationspeed; |
207 | 208 | } else { |
208 | 209 | throw new me.Renderable.Error("animation id '" + name + "' not defined"); |
209 | 210 | } |
|
277 | 278 | * @param {Number} dt time since the last update in milliseconds. |
278 | 279 | */ |
279 | 280 | update : function (dt) { |
280 | | - // update animation if necessary |
281 | | - if (!this.animationpause && this.current.length > 1) { |
282 | | - this.current.nextFrame -= dt; |
283 | | - if (this.current.nextFrame <= 0) { |
284 | | - this.setAnimationFrame(++this.current.idx); |
| 281 | + // Update animation if necessary |
| 282 | + if (this.animationpause || this.current.length <= 1) { |
| 283 | + return this._super(me.Sprite, "update", [ dt ]); |
| 284 | + } |
| 285 | + |
| 286 | + var duration = 0, |
| 287 | + result = false; |
| 288 | + |
| 289 | + this.dt += dt; |
| 290 | + duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay; |
| 291 | + while (this.dt >= duration) { |
| 292 | + result = true; |
| 293 | + this.dt -= duration; |
| 294 | + this.setAnimationFrame(this.current.idx + 1); |
285 | 295 |
|
286 | | - // switch animation if we reach the end of the strip |
287 | | - // and a callback is defined |
288 | | - if (this.current.idx === 0 && this.resetAnim) { |
289 | | - // if string, change to the corresponding animation |
290 | | - if (typeof this.resetAnim === "string") { |
291 | | - this.setCurrentAnimation(this.resetAnim); |
292 | | - } |
293 | | - // if function (callback) call it |
294 | | - else if (typeof this.resetAnim === "function" && |
295 | | - this.resetAnim() === false) { |
296 | | - this.current.idx = this.current.length - 1; |
297 | | - this.setAnimationFrame(this.current.idx); |
298 | | - this._super(me.Sprite, "update", [dt]); |
299 | | - return false; |
300 | | - } |
| 296 | + // Switch animation if we reach the end of the strip and a callback is defined |
| 297 | + if (this.current.idx === 0 && this.resetAnim) { |
| 298 | + // If string, change to the corresponding animation |
| 299 | + if (typeof this.resetAnim === "string") { |
| 300 | + this.setCurrentAnimation(this.resetAnim); |
301 | 301 | } |
| 302 | + // Otherwise is must be callable |
| 303 | + else if (this.resetAnim() === false) { |
| 304 | + // Reset to last frame |
| 305 | + this.setAnimationFrame(this.current.length - 1); |
302 | 306 |
|
303 | | - // set next frame timestamp |
304 | | - this.current.nextFrame = this.getAnimationFrameObjectByIndex(this.current.idx).delay; |
305 | | - return this._super(me.Sprite, "update", [dt]) || true; |
| 307 | + // Bail early without skipping any more frames. |
| 308 | + this.dt %= duration; |
| 309 | + break; |
| 310 | + } |
306 | 311 | } |
| 312 | + |
| 313 | + // Get next frame duration |
| 314 | + duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay; |
307 | 315 | } |
308 | | - return this._super(me.Sprite, "update", [dt]); |
| 316 | + |
| 317 | + return this._super(me.Sprite, "update", [ dt ]) || result; |
309 | 318 | } |
310 | 319 | }); |
311 | 320 | })(); |
0 commit comments