random anim state on click in guestbook

main
Michael Murtaugh 1 week ago
parent 2eee1624f4
commit d312b986d5

@ -11,6 +11,7 @@ var ANIMS = {
run_left: a("2,5"), run_left: a("2,5"),
run_right: a("2f,5f"), run_right: a("2f,5f"),
} }
var ANIM_NAMES = Object.keys(ANIMS);
class Creature { class Creature {
constructor(img) { constructor(img) {
@ -20,6 +21,8 @@ class Creature {
} else { } else {
img.addEventListener("load", this.init.bind(this)); img.addEventListener("load", this.init.bind(this));
} }
this.interval_id = null;
this.currentAnimName = "stand";
} }
init () { init () {
this.div = document.createElement("div"); this.div = document.createElement("div");
@ -45,7 +48,6 @@ class Creature {
this.div.appendChild(this.centerbottom); this.div.appendChild(this.centerbottom);
this.centerbottom.appendChild(this.caption_div); this.centerbottom.appendChild(this.caption_div);
this.caption.textContent = this.img.title; this.caption.textContent = this.img.title;
this.currentAnimName = "stand";
this.canvas.addEventListener("click", this.click.bind(this)); this.canvas.addEventListener("click", this.click.bind(this));
} }
animate(){ animate(){
@ -69,9 +71,16 @@ class Creature {
ctx.restore(); ctx.restore();
} }
click () { click () {
console.log("click", this); // console.log("click", this);
this.currentAnimName = "run_front"; var index = ANIM_NAMES.indexOf(this.currentAnimName);
window.setInterval(this.animate.bind(this), 16); // this.currentAnimName = ANIM_NAMES[index + 1] || ANIM_NAMES[0];
var oldState = this.currentAnimName;
while (this.currentAnimName == oldState) {
this.currentAnimName = ANIM_NAMES[Math.floor(Math.random() * ANIM_NAMES.length)];
}
if (!this.interval_id) {
this.interval_id = window.setInterval(this.animate.bind(this), 16);
}
} }
} }

Loading…
Cancel
Save