0.9.7:Using animated sprites

From DXFWiki

Jump to: navigation, search

DXFramework has simple support for animated sprites. First thing to do is create an image containing the frames of an animation. Here is the rancher sprite from Bovine Drover:

Image:Rancher-20-4-5.PNG

That is one (.png) file. Because it has 4 columns and 5 rows (for a total of 20 frames), it is named Rancher-20-4-5.PNG

Generally:

anything-<frames>-<cols>-<rows>.ext

... where "anything" can be any (filesystem legal) identifying string you want (don't use dashes though, that's what is looked for when parsing the frames/cols/rows).

Create the sprite the same way you create any sprite:

dxf::Sprite rancher;
rancher.CreateFromFile(L"Bovine/rancher-20-4-5.png");

Draw it the usual ways:

rancher.SetPosition(200,200);
rancher.Render2D();
rancher.Render2D(300,200);

Note that when drawn only one frame will appear. Width and height return the size of one frame:

rancher.GetWidth() == 64; // image width 256 / number of cols 4
rancher.GetHeight() == 64; // image height 320 / number of rows 5

The frame that appears defaults to zero (the upper left frame). Frames start at zero in the upper-left and are numbered like you read a book (first row is 0-3, second row is 4-7, etc.)

If you wanted to iterate through all frames, simply call Animate() to switch to the next frame (switching past the last frame of the image loops back to the first frame (0)).

ghost.Animate();

With the rancher image, you want to animate through one row, so you add an offset and cycle through the four images using SetAnimation(). Here's one way to do this (changing the animation every quarter-second:

switch(state) {
    ...
    offset = 4; // offset can be 0, 4, 8, 12, 16
    ...
}
int frame = offset + (static_cast<int>(fTime / 0.25f) % 4);
player.pSprite->SetAnimation(frame);

Remember to unload it when done:

rancher.Unload();
Personal tools