Last active
July 24, 2019 17:59
-
-
Save UbaldoRosas/770b4afdac1539eb449dd2184371bfc0 to your computer and use it in GitHub Desktop.
Javascript | Sprites animator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animated_sprite = function () { | |
this.vars = { | |
currentIndex : 0, | |
path : null, | |
class : null, | |
interval : null, | |
lenght : null, | |
time : null | |
} | |
this.init = function(sprite, lenght = null, time) | |
{ | |
this.vars.paths = document.querySelector("." + sprite); | |
this.vars.class = sprite; | |
this.vars.lenght = lenght; | |
this.vars.time = time; | |
window.clearInterval(this.vars.interval); | |
this.vars.interval = window.setInterval(this.draw_image.bind(this), this.vars.time); | |
} | |
this.draw_image = function () | |
{ | |
this.vars.paths.setAttribute('class', this.vars.class + ' ' + this.vars.class + '_' + this.vars.currentIndex); | |
if (this.vars.currentIndex < this.vars.lenght - 1) { | |
this.vars.currentIndex += 1; | |
} else { | |
this.vars.currentIndex = 0; | |
} | |
} | |
this.stop = function () | |
{ | |
window.clearInterval(this.vars.interval); | |
}, | |
this.play = function () | |
{ | |
this.vars.interval = window.setInterval(this.draw_image.bind(this), this.vars.time); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment