
Untitled
By: a guest on
Jun 14th, 2012 | syntax:
JavaScript | size: 1.58 KB | hits: 17 | expires: Never
function SpriteAnim (options) {
var timerId = 0;
var i = 0;
this.init = function () {
var element = document.getElementById(options.elementId);
element.style.width = options.width + "px";
element.style.height = options.height + "px";
element.style.backgroundRepeat = "no-repeat";
element.style.backgroundImage = "url(" + options.sprite + ")";
};
this.showFrame = function (which) {
if (which < options.frames) {
i = which;
element = document.getElementById(options.elementId);
element.style.backgroundPosition = "0px -" + which * options.height + "px";
}
};
this.play = function () {
timerId = setInterval(function () {
if (i < options.frames) {
element = document.getElementById(options.elementId);
element.style.backgroundPosition = "0px -" + i * options.height + "px";
i++;
} else {
clearInterval(timerId);
}
}, 100);
};
this.playReverse = function () {
timerId = setInterval(function () {
if (i > 0) {
element = document.getElementById(options.elementId);
element.style.backgroundPosition = "0px -" + i * options.height + "px";
i--;
} else {
clearInterval(timerId);
}
}, 100);
};
}
var arrow = new Array();
var arrow[1][1] = new SpriteAnim({
width: 7,
height: 7,
frames: 8,
sprite: "arrow1.png",
elementId: "arrow1_1"
});
var arrow[1][2] = new SpriteAnim({
width: 7,
height: 7,
frames: 8,
sprite: "arrow1.png",
elementId: "arrow1_2"
});