C++ Animation in Allegro Problem

Started by
5 comments, last by Ars7c3 12 years, 4 months ago
Hey guys. I have been trying for days now to figure out this problem! I am 13 years old and I am trying to write an animation class for my game. It will be able to blit sprites off a
spritesheet onto the screen based on what frame the animation is on. However, I have encountered a problem, and I will appreciate anyone's assistance to my problem. Happy Holidays and thanks in advance!biggrin.gif




void Animation::PlayAnimation(int framex, int framey, int totalframes, int objectx, int objecty,BITMAP *buffer)
{

SetFrameX(framex); //Sets the X cord for SpriteSheet
SetFrameY(framey); // Sets the Y cord for SpriteSheet
SetTotalFrames(totalframes); //Total frames in animation

for(int i = 0; i < TotalFrames; i++)
{
Frame = i;
FrameX = i * Size; //Size is the size of each sprite on SpriteSheet. What this line does is sets X cord(FrameX) on the SpriteSheet to the next Sprite


if(Frame == TotalFrames) //Resets Animation when animation reaches the max frames
{
Frame = 0;
FrameX = 0;
}





}

masked_blit(SpriteSheet,buffer,FrameX,FrameY,objectx,objecty,Size,Size); //This is where i blit the sprite to the screen. But it only plays the 3(last) frame?! When I put it in the loop // it just draws all frames at the same time so it looks all like 1 bitmap

}



I have attached the SpriteSheet i am using
Advertisement
It seems as if you don't have code to cause a delay between two frames, which would cause the frames to display very quickly. Also, your post doesn't have the spritesheet attached for some reason.
I understand what you are saying, but not entirely sure how to implement that. Could you maybe help me out by writing some quick peusado code; or at least give me a basic idea how to do that. Btw idk why the sprites beet didn't attach.
Well, a simple way would be to do this:
  1. advance all the animations 1 frame and then redraw them
  2. wait until the next frame
For the second step, look for a function named "Delay" or "Sleep" (depending on what platform, language and library you are using to write your game)


I tried the Sleep function and it does help slow down the animation speed, thank you very much. However, I still encounter the same problem i was having before. The blit function is outside the for loop and so can only blit when the for loop is over, making it only able to draw the last frame. I tried to fix this by putting the blit function inside the for loop, but it just drew all the sprites at the same time.
Well, that was rather silly of me. I didn't notice that you had a loop going through all the frames and then drawing the picture. Alright, so you probably want to replace the loop with a single step.
Thank you so much! It works!biggrin.gif

This topic is closed to new replies.

Advertisement