2 AnimationsInDepth
Tim Young edited this page 2017-10-04 03:22:24 +02:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Sprites

Creating a sprite

A Sprite is an animation. It is a series of images that are displayed one after the other. Usually, those images are related to each-other, such that it looks like it is moving. If you only have one image, that one image keeps repeating; the same frame over and over.

One SpriteLibrary user complained about terrible response-time to his program. It turned out that he had his single-frame sprites trying to refresh every 1ms. The program was spending all its time trying to re-draw the same image. He changed it so his sprites had a duration of 1000ms, and that made all the difference in the world to his program.

When you instantiate a named sprite, you want to make sure you set all the defaults for animation speeds. Those speeds will be passed down to all duplicates of the named sprite.

Multiple Animations in a Sprite

A Sprite can have multiple animations. For example, you may have an adventurer who has animations for walk left, walk right, jump left, jump right, climb up, fall down, etc. They are all the one adventurer, so they are all in one sprite. You can tell the sprite to display animation 0, and it looks like he is walking right. Tell it to do animation 1, and it looks like he is walking left.

An image with multiple animations

The SpriteLibrary is written in C#, and so the indexes start at Zero. The first animation is 0, the second is 1, etc.

I usually have all the frames for one sprite in one image, but you do not need to do that. You can pull frames from multiple images for different animations. BUT, every animation needs to come from one image. At the time of the writing of this documentation, I do not have a sprite, or animation creation method that pulls frames from multiple images.

Duration of sprite animations

The duration that one frame is displayed is tracked in milliseconds. Please be aware that it is mostly impossible for the system to animate faster than 15ms. It might appear to do so, but what happens is that some of the frames will be skipped so that it appears to animate fast. I have seen people attempt to build sprites that animate insanely fast, and all that happens is that two-thirds of their frames are skipped. It is best to assume that 50 to 100ms is “fast.” That is ten to twenty images a second, which is plenty fast for an animation. The problem with this speed is that it does take a toll on the processor. A bunch of sprites animating at 50ms will quickly make your game run slowly on an older computer.

200-300 is a nice speed for an animation. It does not take as much of a toll on the processor, and you can handle many sprites animating at that speed. A lot will depend on how many sprites you need to make your game work. You may need to adjust the animation speed of things once your game has been created if you find that it is running too slowly.

When I programmed the sprite controller, I did skimp a bit on drawing optimally. The right way to do it would be to occasionally draw partial sprites when two of them overlap. Instead, what I did was to tell the system to redraw all sprites that overlapped, in the order they needed to be drawn. This means that, when you have a lot of overlapping sprites, it can sometimes do a lot of rendering. And, if a sprite is completely hidden by other sprites, that hidden sprite is drawn completely, and then overwritten by the sprites on top of them. And, this all happens every time something redraws.

All this to say, the duration of any animation may be something to consider. You can override the speed of an individual frame, making it animate faster or slower than the rest of the frames in the sequence. But usually you will have the same duration for each frame in the animation. Most of the time, I just draw something and change the duration until it looks right. But, if you have a lot of sprites, and a lot of overlap, you may need to adjust your duration a little bit to make things work.

Single-frame sprites

Single-frame sprites can be very useful. Often menu-buttons, scenery, or stationary objects will not animate. You should use a duration of 1000 or thereabouts for these sprites.