Hey guys so I have a sprite sheet with multiple sprites that I want to use to animate a website using canvas.
However the problem I am having is I don't know how to go about reading in only the frames that I need.
Example:
1 1 1 2
2 2 2 2
3 3 4 4
4 4 4 4
Here I have 4 different sprites that I want to animate. How would I go about retrieving and animating the correct frames?
P.S I'm using javascript.
Retrieving and Playing multiple sprites on a spritesheet
Retrieving: Create an object defining each sprite's x,y,width,height and save the objects in an array
Depending on your actual spritesheet, this code can be optimized--especially if the sprites are equally sized and spaced.
Playing a frame: Use the clipping verision of context.drawImage to "play" a sprite frame:
Example usage: Draw frame #2 of sprite1 at canvas 100,100
You didn't ask about how to create an animation loop, but here is starter info anyway: