views:

108

answers:

2

Hello, I am trying to find the simplest way to replicate a 12fps animation using CSS sprites and jQuery.

I was thinking using the setInterval() so every 83.33 millisecond, the next sprite will be loaded.

My problem, is that I don't know how to do that...

I was thinking, because my sprite names are incremental like:

mariohammerswing1.png 
mariohammerswing2.png 
mariohammerswing3.png
etc.

So, if we could somehow increment this until we reached the last instance, which in this case is mariohammerswing5.png it will loop back to the beginning.

If I can figure out that part, I'm ready to go! :)

Any suggestions?

+2  A: 

Untested, but something like this:

var images = ['one.png', 'two.png', 'three.ng'];

function startAnim() {
    var $target = $('#something');
    var counter = 0;
    setTimeout(function () {
        $target.css('background-image', images[counter]);
        if (++counter > images.length - 1)
            counter = 0;
    }, 83);
}

startAnim();

You could probably apply some trickery with % (modulo) somehow, but I think it's easier to read this way.

Deniz Dogan
Hey, thanks! This is quite out there for me being a jQuery n00b, but I get the basics, and I'm trying to comprehend the more advanced stuff I'm unfamiliar with.Why did you prefix your target var with a '$' sign? And setting it equal to $('#something') is that to set it equal to where it should be animated?
BOSS
I like naming variables that are "JQueried" with a "$" prefix to indicate this. It's merely a matter of personal preference. `$('#something')` gets the element with ID "something" and wraps it in JQuery so you can use methods like `css()` on it (that's not standard JavaScript). So the element with ID something is the element which you should CSS to set the background-image to the sprite image and height and width and so on.
Deniz Dogan
OK, just wanted to make sure it wasn't reiterating some sort of statement or something like that.
BOSS
+2  A: 

There's a sprite-dedicated plugin for jquery

http://www.spritely.net/

Take a look ;)

Napolux
Cute! .... !!!!!
Mark