Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: JavaScript  |  size: 1.58 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function SpriteAnim (options) {
  2.         var timerId = 0;
  3.         var i = 0;
  4.         this.init = function () {
  5.                 var element = document.getElementById(options.elementId);
  6.                 element.style.width = options.width + "px";
  7.                 element.style.height = options.height + "px";
  8.                 element.style.backgroundRepeat = "no-repeat";
  9.                 element.style.backgroundImage = "url(" + options.sprite + ")";
  10.         };
  11.         this.showFrame = function (which) {
  12.                 if (which < options.frames) {
  13.                         i = which;
  14.                         element = document.getElementById(options.elementId);
  15.                         element.style.backgroundPosition = "0px -" + which * options.height + "px";
  16.                 }
  17.         };
  18.         this.play = function () {
  19.                 timerId = setInterval(function () {
  20.                         if (i < options.frames) {
  21.                                 element = document.getElementById(options.elementId);
  22.                                 element.style.backgroundPosition = "0px -" + i * options.height + "px";
  23.                                 i++;
  24.                         } else {
  25.                             clearInterval(timerId);
  26.                         }
  27.                 }, 100);
  28.         };
  29.         this.playReverse = function () {
  30.                 timerId = setInterval(function () {
  31.                         if (i > 0) {
  32.                                 element = document.getElementById(options.elementId);
  33.                                 element.style.backgroundPosition = "0px -" + i * options.height + "px";
  34.                                 i--;
  35.                         } else {
  36.                             clearInterval(timerId);
  37.                         }
  38.                 }, 100);
  39.         };
  40. }
  41.  
  42. var arrow = new Array();
  43. var arrow[1][1] = new SpriteAnim({
  44.         width: 7,
  45.         height: 7,
  46.         frames: 8,
  47.         sprite: "arrow1.png",
  48.         elementId: "arrow1_1"
  49. });
  50. var arrow[1][2] = new SpriteAnim({
  51.         width: 7,
  52.         height: 7,
  53.         frames: 8,
  54.         sprite: "arrow1.png",
  55.         elementId: "arrow1_2"
  56. });