AnimationX is a special class for handling sprite animation. Instead of keeping counters and frame numbers all over the place for the purpose of animating a sprite image, you can use this class to handle those variables internally. This document will explain how to use it.
First, it is important to state here that AnimationX class has two major components. One is the AnimationX class itself, and the other is the Instance class. The latter class is a nested class and is highly dependent on the former. The main role of an AnimationX object is to load up an image and 'break' it into frames by dividing up the image into rows and columns as you specify. To actually draw a frame of the image, you have to get an 'instance' (hence the class name) of the AnimationX object. The method AnimationX::CreateInstance() simplifies the process.
With the instance class you don't have to load the same frames image so many times; simply load it once into an AnimationX object and use CreateInstance() call to create the instances. Each instance has separate internal counter for the frame number so you can draw two sprites of the same animation but with different frames at a particular time.
Note:
AnimationX does not support animated GIF file. A normal GIF file is OK though.
Example:
// AnimX is the shortname for AnimationX
AnimX anim = AnimX();
// Load the image
// Our image is a special image having 3 column and 2 rows of frames
anim.Load("Images\\anim_sprite.bmp", "Images\\anim_sprite_alpha.bmp", 3, 2);
// Get an instance
AnimX::Instance anim_instance = anim.CreateInstance();
// Set the steps to be 20 steps (i.e. each frame takes 20 game cycles)
// By default it is 30 unless specified otherwise using this function call
anim_instance.SetSteps(20);
In Board::Draw() or XXXX::Draw() if all this occurs in a different class
// Draw current frame to location x, y
// Call whatever GameX SetDrawXXXX function beforehand
// except SetDrawPart() since this Draw() function depends heavily on it
anim_instance.Draw(x, y);
In Board::Run() or XXXX::Run()
anim_instance.Step();
Or if you want to manually advance the frame without using the internal steps counter
anim_instance.StepFrame();
(calling StepFrame() resets the internal steps counter)
Misc functions
Get width and height of a frame
unsigned AnimationX::GetWidth()
unsigned AnimationX::GetHeight()
or you can use the corresponding functions of the Instance object
Destroy the internal image (for cleaning up or reusing the same object for a different image)
void AnimationX::Destroy()
Jump to a particular frame
void AnimationX::Instance::SetFrame(unsigned frame)
Get the number of frames for any reason at all
unsigned AnimationX::GetFramesCount()
or the instance's version
unsigned AnimationX::Instance::GetFramesCount()
Get the current frame number for an instance (frame # is 0 -> total # of frame - 1)
unsigned AnimationX::Instance::GetCurrentFrame()
More notes
- AnimationX::Load() should only be call during Board::Initialize() or any functions called by it or anytime during normal game cycles (not sure about the last one). In particular do not call this function in a constructor or in the global scope. Otherwise the image loading will fail.
- It is OK to call AnimationX::CreateInstance() before calling AnimationX::Load() (since the latter call might come late in the execution due to the above restriction). However, you should not call any member functions of the created Instance object until after AnimationX::Load() is done. (Usually this is not really a problem since AnimationX::Load() will most probably be called before the normal game cycles start)





