From cb46be00880705bfaa77a7981a1e39cecff88ac2 Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Sun, 22 Mar 2020 13:57:15 +0100 Subject: [PATCH] Fixed issues in AnimManager.js --- lib/AnimManager.js | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/AnimManager.js b/lib/AnimManager.js index 2335428..dd42e8e 100644 --- a/lib/AnimManager.js +++ b/lib/AnimManager.js @@ -4,7 +4,13 @@ class Anim { this.rate = rate; this.reset(); } - + + reset() { + this.frame = this.frames[0]; + this.curFrame = 0; + this.curTime = 0; + } + update(dt) { const { rate, frames } = this; if ((this.curTime += dt) > rate) { @@ -13,51 +19,47 @@ class Anim { this.curTime -= rate; } } - - reset() { - this.frame = this.frames[0]; - this.curFrame = 0; - this.curTime = 0; - } } - + class AnimManager { - constructor(e) { + constructor(e = { x: 0, y: 0 }) { this.anims = {}; this.running = false; this.frameSource = e.frame || e; - this.currrent = null; + this.current = null; } - + add(name, frames, speed) { this.anims[name] = new Anim(frames, speed); return this.anims[name]; } - - update(dt) { + + update(dt) { const { current, anims, frameSource } = this; if (!current) { return; } const anim = anims[current]; anim.update(dt); - + + // Sync the tileSprite frame frameSource.x = anim.frame.x; frameSource.y = anim.frame.y; } - - play(anim) { + + play(anim) { const { current, anims } = this; - if (anim === current ) { + if (anim === current) { return; } this.current = anim; anims[anim].reset(); } - - stop() { + + stop() { this.current = null; } } - -module.exports = AnimManager; \ No newline at end of file + +module.exports = AnimManager; + \ No newline at end of file