Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Animating Player

This thread is locked; no one can reply to it. rss feed Print
Animating Player
Jeff Bernard
Member #6,698
December 2005
avatar

I'm looking for some tips on animating the player. There's 16 different frames, and 3 in the running animations (3 seperate for left and right, well, more like 2 since the left/right standing frame is used in them).

Anway, I've got a really long function that checks pretty much every key combination to get the frame to use (X is my jump key). I'm sure there's gotta be a better way though...right?

Also, here's my current hero.bmp so you can see what each frame is...it's 0-7 on top and then 8-15 on the bottom. hero.jpg

1int getHeroFrame()
2{
3 // check to make sure in the air before doing aerial frames
4 if (!key[KEY_RIGHT] && (key[KEY_LEFT] || hero.direction < 8) && getpixel(buffer[2], hero.x+cameraX+hero.frame[hero.direction]->w-6, hero.y+cameraY+hero.frame[hero.direction]->h+1) == bitmap_mask_color(buffer[2]) && getpixel(buffer[2], hero.x+cameraX+6, hero.y+cameraY+hero.frame[hero.direction]->h+1) == bitmap_mask_color(buffer[2]) && getpixel(buffer[2], hero.x+cameraX+hero.frame[hero.direction]->w/2, hero.y+cameraY+hero.frame[hero.direction]->h+1) == bitmap_mask_color(buffer[2]))
5 if (key[KEY_UP])
6 return 4; // right-up, spread legs
7 else if (key[KEY_DOWN])
8 return 6; // right-down (air)
9 else if (key[KEY_X] && hero.ground && hero.altitude < JUMPTIME-10)
10 return 2; // right, legs together
11 else
12 return 1; // right, spread legs
13 // one more air, just the other facing
14 else if ((key[KEY_RIGHT] || hero.direction > 7) && getpixel(buffer[2], hero.x+cameraX+hero.frame[hero.direction]->w-6, hero.y+cameraY+hero.frame[hero.direction]->h+1) == bitmap_mask_color(buffer[2]) && getpixel(buffer[2], hero.x+cameraX+hero.frame[hero.direction]->w/2, hero.y+cameraY+hero.frame[hero.direction]->h+1) == bitmap_mask_color(buffer[2]) && getpixel(buffer[2], hero.x+cameraX+6, hero.y+cameraY+hero.frame[hero.direction]->h+1) == bitmap_mask_color(buffer[2]))
15 if (key[KEY_UP])
16 return 12; // left-up, spread legs
17 else if (key[KEY_DOWN])
18 return 14; // left-down (air)
19 else if (key[KEY_X] && hero.ground && hero.altitude < JUMPTIME-10)
20 return 10; // left, legs together
21 else
22 return 9; // left, spread legs
23 hero.step++; // to cycle running frames
24 if (hero.step >= 25)
25 {
26 if (hero.step >= 50)
27 {
28 if (hero.step >= 75)
29 hero.step = 0;
30 if (key[KEY_UP] && key[KEY_RIGHT])
31 return 11; // right-up
32 else if (key[KEY_UP] && key[KEY_LEFT])
33 return 3; // left-up
34 if (key[KEY_RIGHT])
35 return 8; // right
36 else if (key[KEY_LEFT])
37 return 0; // left
38 }
39 if (key[KEY_UP] && key[KEY_RIGHT])
40 return 12; // right-up, legs spread
41 else if (key[KEY_UP] && key[KEY_LEFT])
42 return 4; // left-up, legs spread
43 if (key[KEY_RIGHT])
44 return 9; // right, legs spread
45 else if (key[KEY_LEFT])
46 return 1; // left, legs spread
47 }
48 if (key[KEY_UP] && key[KEY_RIGHT])
49 return 13; // right-up, legs together
50 else if (key[KEY_UP] && key[KEY_LEFT])
51 return 5; // left-up, legs together
52 if (key[KEY_RIGHT])
53 return 10; // right, legs together
54 else if (key[KEY_LEFT])
55 return 2; // left, legs together
56 if (hero.direction < 7 || (hero.direction < 8 && key[KEY_UP]))
57 if (key[KEY_UP])
58 return 3; // left-up
59 else if (key[KEY_DOWN])
60 return 7; // lefts-backwards
61 else
62 return 0; // left
63 else if ((hero.direction != 15 && hero.direction != 7) || key[KEY_UP])
64 if (key[KEY_UP])
65 return 11; // right-up
66 else if (key[KEY_DOWN])
67 return 15; // right-backwards
68 else
69 return 8; // right
70 return hero.direction; // keep current direction
71}

--
I thought I was wrong once, but I was mistaken.

Onewing
Member #6,152
August 2005
avatar

First thing I noticed is you have 3 if(key[KEY_RIGHT]) situations, each returning 8, 9, or 10. That means that function is return 9 give or take 1.

What I do is have the key determine the base frame in the set of frames. Another, completely separate function is checking the time they've been going in whatever direction and if enough time has gone by, increase or decrease an offset that picks out the right part of the animation in that direction.

Basically, my logic is have a input function which determines which direction they're going and an animate function which gets the right frame within that direction. Make any sense?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Jeff Bernard
Member #6,698
December 2005
avatar

Ah, thank you very much, Steven Silvey. I now only have it check the key pressed once and then I pass a value to a function which then determines the correct frame of animations. It's way better now.

--
I thought I was wrong once, but I was mistaken.

Go to: