What is a sprite?
The main thing about the SpriteLibrary, as you might be able to guess, is how it deals with Sprites. A Sprite is a series of animations, some properties, and some code to go along with it.
You can have a Sprite that is a mouse. It has a few different pictures which are displayed, one after the other. By cycling through these images, it looks like the mouse is running. We can have multiple sets of images, so you can have your mouse running from left to right, and from top to bottom. Each different set of images is called an animation.
But you also need to have it move across the screen. Each sprite can be told what to do, whether setting it to move from left to right, or giving it a series of points on the screen that it moves from one to the other. The sprite tends to move smoother if you use a move-to or give it a direction. If you try to move it by putting its x and y coordinates to positions, it tends to look jerky. That is because the sprite movement routines take into consideration the imprecise nature of the timing system, and makes it appear to be moving smoothly.
Sprites also have events. An event is something that happens to the sprite, which you can write code for. For example, you can add a “On_Click” event that will let you run some code when someone clicks on your sprite. You want to use this if you want to create a menu, with “forward” / “back” sprites, or “play game” / “Exit” sprites. More about these events can be found below in the Events section.
The SpriteLibrary can be used anyway you can get it to work. But it was designed to work by making master sprites, and then using copies of those master sprites whenever you need a new one.
The reason for this is mainly for memory conversation. If you are always generating new sprites from scratch, the SpriteLibrary will end up storing all their images multiple times. When you use a master sprite, all sprites duplicated off of that one use the one set of images. This makes it very memory efficient.
When you create a new sprite, you should set the sprite name to something that you will remember. That way you can create as many duplicates of that sprite, and destroy those duplicate sprites as you need to.
OneSprite = new Sprite(new Point(0, 0), MySpriteController, Properties.Resources.shot, 50, 50, 200, 4); OneSprite.SetSize(new Size(30, 30)); OneSprite.SetName(“shot”);