This post is a portion of Part 7 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 7.0 – Sprites
- 7.1 – Bitmap Transparency in Allegro 5
- 7.2 – Basic Animation in Allegro 5
- 7.3 – Using Allegro 5 to Create a Sprite Sheet
- 7.4 – Sprite Sheet Animation in Allegro 5
It is time to add basic animation to our programs. You will see in this video, if we cycle still images in order we can trick the eye into believing the object we are seeing on the screen is really moving. Pay attention to the variables used and how the cycling control works.
Full source can be found here
hey mike i am having a bit of a problem when i run this program it brings up a window that says “unhandled exeption at 0x100306ee in Sprites!.exe: 0xC0000005: Access violation reading location 0x00000014.” then it has two buttons that say break or? continue, if i break it takes me to tidtable.c and if i continue it takes me back to the same option, this has happened before and i had to reinstall everything in order to fix it could you help me?
It sounds like the image you are trying to access is not in the correct folder. Either that, or you are trying to read outside of the bounds of the image.
I had the same issue Jacob. It was fixed by checking the images are in their expected location.
If you ever encounter errors that you’re unsure about, try commenting out some lines and running it to see if you get the error. While this isn’t a definite way to find the issue, it will help you 80% of the time.
I’m having the same issue and I can’t figure out what I’m doing wrong. I made sure the images are in the right folder, i rechecked my code but nothing is working.
i figured it out i just forgot the .bmp thanks though…
Hey Mike,
just wanted to let you know these are some incredible tutorials. i’ve been following along and have basically run up to 7.2 in the last day and a half (addicted to this series, for sure).
Maybe I’m getting a little ahead of myself since there are 7 more lessons left, but during Lesson 5 you mentioned that we now have the tools to create almost any 2D game we can conceive of.
My question is this: how would we go about a mario-style platformer? Where the screen moves along with the player?
Keep up the good work, Mike! Definitely going to go through the OOP course too
whoops, just got to the later lessons and saw that my question was taken care of. you really did think of everything mate!
thanks again
I’m having a problem where I tried to animate the comets in the the side shooter game, and it only animates 1 of the comets at any point in time. I tried using the code:
for(int i = 1; i < 2; i++)
{
if(comets[i].live == true){
al_draw_bitmap(cometAnimation[curFrame_comet], (comets[i].x – comet_imageWidth/2), (comets[i].y – comet_imageHeight),0);
break;
}
}
and it wont work!!!
Yes… look at your loop. You are looping from 1 to 2, so the loop iterates only once.
Thanks, but it was fixed when I went into your later tutorials. You really do cover everything!!! I just need to learn how to do class based programming now then I can make the game I’ve been wanting to. Thanks for helping though!!!
Heads up: You forgot to destroy your timer in this one!
good tutorial as always,
works great, unless i try to give my image to my drawhero function,
failed to convert’ALLEGRO_BITMAP’ in ‘ALLEGRO_BITMAP *[]’..
or other kind of convert problems,
do someone got an idea how i solve this?
ok, i got i by myself
1. think
2. talk
in future 🙂
so your code that goes:
else if (ev.type == ALLEGRO_EVENT_TIMER)
{
if (++frameCount >= frameDelay)
{
if (++curFrame >= maxFrame)
curFrame = 0;
frameCount = 0;
}
}
for some reason in my head, i’m thinking, “couldn’t this be simpler by using modulus some how?”
am i wrong?
also i don’t think you did al_destroy_timer (timer); in your code, is it necessary?
PS. love this website 🙂 recommending it to friends.
You are correct. You should call that function. I correct myself in a future video. I am terrible about remembering these things.
Well, modulus could work. The problem is that modulus will only return the result of the remainder and not change the original number. Therefore, curFrame will continue to get bigger (until it reaches the maximum int size and crashes). Also, modulus will take longer with bigger numbers.
Wouldnt it be better to create a bitmap class with all this stuff “functioned” in it?
Yes, but one of the philosophies of this series was to avoid using OOP as it was targeted at new programmers.
Hey Mike, I was running into a crash when the animation finished.
It would run the “animation loop” only once.. when it finished drawing the frame 7 (five times) it would crash.
Then I noticed (by printing some stuff) that the frame 0 was being drawn only 4 times instead of 5.
So I changed ++frameCount to frameCount++ in the first if-statement.
And changed curFrame++ to ++curFrame on the second if-statement.
This solved my crash and now it runs a smooth animation without problems.
Just posting it so that other people may end up having the same problem.
Hmm, if that is working then something else might be going on in your code. I’m glad you were able to get it to work though.
i know this is a stupid ques bt i am going to ask it anyways —>
when doing animation with “primitives” we are changing/updating the the xPos,yPos 60 times a sec(when we are holding the arrow keys) but here->
when we are doing animation with sprites we are changing only 12 times in a sec. why? is it because we do not have 60 sprites or because changing 60 sprites a sec it a bit too fast?? if it is the ans then why changing the primitives is not too fast? i am confused :@
I am new in Allegro, I found the article is very helpful. I am now in China, unfortunately, I can not view youtube from here. But I think I could load the source code to study. But above listed url is not availalbe (http://dl.dropbox.com/u/6116499/Animation.zip). The source code in all chapters are not downloadable. Could you help me to send the source code to my email. (2521137394@qq.com)
Regards & Thanks!
Contact me via email. I emailed you the source you were looking for.
Hi, your tutorials are great for learning Allegro 5 and are a real help to me. I just wanted to point out that you could automate the loading of the bitmap array by naming you images consecutively:1.bmp , 2.bmp etc. and then load them :
char name[5];//name of the picture
for(i = 0 ; i < maxFrame ; i++) {
sprintf(name , "%d.bmp" , i);
image[i] = al_load_bitmap(name);
Anyway, great tutorial and realy-realy helpful!
Nice C, C++ idea for using sprintf, but why not use its formatting to load the example filename directly like so:
char name[20]; // local def for sprintf
for(int i=0; i<MAXFRAME; i++) {
sprintf(name, "fliegt w%.4d.bmp", i);
bitmap[i] = al_load_bitmap(name);
al_convert_mask_to_alpha(bitmap[i], al_map_rgb(106, 76, 48));
}
Interesting tutorials btw.
My last experience with Allegro was long ago with DJGPP. 😉
Greets,
Jan
Hello, whenever I push and hold a button, the sprite (dragon) changes image and moves faster. I’ve removed every condition check except timer but still no improvement. Is this phenomenon normal?
while (!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type = ALLEGRO_EVENT_TIMER)
{
if (++frameCount >= frameDelay)
{
if (++curFrame >= maxFrame) curFrame = 0;
frameCount = 0;
}
x += 5;
//if (x = width + al_get_bitmap_width(image[curFrame]) / 2)
{
x = 0 – al_get_bitmap_width(image[curFrame]) / 2;
}
}
al_draw_bitmap(image[curFrame], x – al_get_bitmap_width(image[curFrame]) / 2, height/2, 0);
al_flip_display();
al_clear_to_color(al_map_rgb(100, 100, 100));
}
Oh, the “=”… this is the 2nd time I got this mistake… sorry for the disturbing