Sprite sheets have been used in game development for many years. In addition to the fact that all the tiles related to some objects are stored together, in web development it also allows you to reduce the number of requests for downloading many separate images.
To demonstrate it, I compiled a Sonic sprite sheet from the "Sonic the Hedgehog 3" game.
It consists of three rows of sprites, responsible for a specific set of actions: idle (9 frames), walk (8 frames), and run (4 frames). And in my case, each frame has the same height and width.
Let's create a base Sprite class from which our objects will be inherited, which we need to animate.
We declared a constructor() and created two methods - update() and render().
To get started, let's deal with the constructor(). At the input, it accepts parameters, among which deserves attention:
- frames - Number of frames in a row
- frameIndex - Index of the current frame
- row - Row number
- ticksPerFrame - Determine how long to stay on this frame. This way we control the speed of the animation.
- tickCount - How long we've already shown this frame.
When we call the update() method, we increment the tickCount counter and compare it with the ticksPerFrame that we specified in the constructor. If enough time has passed and there are still frames, then go to the next frame. Otherwise, switch to the first frame.
The render() method is responsible for drawing an object on canvas using the drawImage() method.
Now let's create a Sonic class that inherits our base Sprite class. At the input, it takes the coordinates where to draw our hero, the context, and a set of constant parameters for this class, which override the parameters of the parent class.
This class has three additional methods walk(), run(), and idle(). Inside them, we redefine the type and frame rate, thereby changing the appearance and speed of the animation.
Let's create another Coin class to show how easily we can now animate our objects, inheriting from the base class.
Finally, create the index.html file in which we initialize our classes and create the base object of the game.
Here the init() method responsible for initially creating the canvas, Sonic and coin objects, and running drawingLoop(), in which we repeat the following steps:
Clear a canvas.
Draw (render) all objects.
You can see the result below. Also, all the code is available on the GitHub.
Hope this was helpful. If you have any questions, please leave a comment.