Game Development Reference
In-Depth Information
an individual frame. As texture coordinates range from 0 to 1, the width and
height of a single frame is calculated by dividing the number of frames along
the X and the Y by 1. Once the dimensions of a single frame are calculated, the
positions of the U,Vs can be worked out by multiplying the frame width and
height by the X,Y coordinates of the current frame; this gets the top-left point of
the frame on the texture map. The SetUVs method requires a TopLeft
and BottomRight point. The BottomRight position is calculated from the
TopLeft position by adding an extra frame width and height.
SetAnimation is the method used to set the number of frames along the X and
Y of the texture map. It makes a call to UpdateUVs so that the sprite is updated
to display the correct frame. GetFrameCount gets the total number of frames
in the animation. The AdvanceFrame method moves the animation to the next
frame if it comes to the end of the frames; then the frame index wraps around to
0 again. The wrap around is done using modulus—the % operator. The modulus
operator computes the remainder that results from performing integer division.
The best way to understand the use of the modulus operator is to provide
an example you are probably already familiar with: time. A clock face has
12 numbers, and it works in modulo 12: 13:00 hours in modulo 12 is 1 o'clock.
In our case, the modulo is equal to the total number of frames in the animation.
The Update method is responsible for updating the current frame and making
the explosion appear to animate. If Looping is set to false and the current
frame is the last frame, then the Update method returns immediately and the
Finished flag is set to true. If the animation hasn't finished or is looping, then
the frame countdown, _currentFrameTime , is updated, and if it goes below
0, the frame needs to be changed. The frame is updated by making a call to
AdvanceFrame , resetting the _currentFrameTime , and finally updating the
U,Vs.
With the AnimatedSprite class added to the Engine project, the explosion
animation can be tested. Find the explode.tga file on the CD in the Assets folder
and add it to the project, setting the properties as usual. It can then be loaded in
the form.cs file with the other textures.
_textureManager.LoadTexture("explosion", "explode.tga");
A quick way to test the animation is to load it directly into the Level as an
animated sprite.
 
Search WWH ::




Custom Search