|
Walk animation. |
julian_boolean
Member #8,201
January 2007
|
The problem I'm having is specifically with northwest, northeast, southeast, and southwest. Somewhere in my program I have this:
North, east, south, west, all works perfectly fine. But northwest, northeast, southeast, and southwest only displays the first frame for each direction instead of cycling through them. In other words when I press the up and right key, northeast only displays frame 40 and nothing else. Edit: I also have this around the direction switch to slow the frames down: if (frame_count++ > frame_delay) { f_count--; switch (dir) { // ... } } But it acts REALLY sketchy and I usually have to set the delay to 150 or so to slow it down even a bit. Allegro timers came to mind. |
Jakub Wasilewski
Member #3,653
June 2003
|
Just examine those ifs. Let's say I press up+left to go northwest. That means both key[KEY_UP] and key[KEY_LEFT] are true - which causes the program to call walk(NORTH), walk(WEST), walk(NORTHWEST) in that order. So NORTH will set the frame to something appropriate, then WEST will set the frame to the first frame of "walk west" animation, and then finally NORTHWEST will set the frame to the first frame of "walk northwest" animation. Just fix the ifs so that walk() is always called only once. --------------------------- |
julian_boolean
Member #8,201
January 2007
|
Hmm.. That reminds me, if I hold in an arrow key (like north) and press a different one while holding it in, it won't switch to that direction and will remain north. Is this problem related? Edit: Actually NM it does. Still not sure how I call walk() once. I was reading a book and in it the guy makes functions for walking in different directions, like walk_north(), but I really don't want to do that. |
Jakub Wasilewski
Member #3,653
June 2003
|
Just make sure to call walk(NORTH) only if KEY_UP is pressed, and no other directional keys are pressed. Similar for other directions. --------------------------- |
julian_boolean
Member #8,201
January 2007
|
Isn't that what I have already? If so, then I'm not sure what your getting at. |
Jakub Wasilewski
Member #3,653
June 2003
|
Quote: if KEY_UP is pressed, and no other directional keys are pressed.
--------------------------- |
julian_boolean
Member #8,201
January 2007
|
LennyLen
Member #5,313
December 2004
|
Jakub Wasilewski
Member #3,653
June 2003
|
if (up && left) walk(NORTHWEST); else if (up && right) walk(NORTHEAST); else if (down && right) walk(SOUTHEAST); else if (down && left) walk(SOUTHWEST); // at these point all the "double" directions are handled, so we can check for the single ones - the elses ensure that we only call walk() once. else if (down) walk(SOUTH); //...
--------------------------- |
julian_boolean
Member #8,201
January 2007
|
LennyLen: You gotta be kidding me. Is there any other way to do it that doesn't look like.. That? |
LennyLen
Member #5,313
December 2004
|
Quote: LennyLen: You gotta be kidding me. Is there any other way to do it that doesn't look like.. That? Yeah, what Jakub posted.
|
julian_boolean
Member #8,201
January 2007
|
Edited it as soon as he posted that. That code does work, I just hate using if else. Thank you. So.. How about those timers? Quote: I also have this around the direction switch to slow the frames down: if (frame_count++ > frame_delay) { f_count--; switch (dir) { // ... } } But it acts REALLY sketchy and I usually have to set the delay to 150 or so to slow it down even a bit. Allegro timers came to mind.
|
X-G
Member #856
December 2000
|
Quote: You can only move in one direction. ... that seems like a pretty natural limitation, doesn't it? -- |
Jakub Wasilewski
Member #3,653
June 2003
|
enum {NORTH = 1, EAST = 2, SOUTH = 4, WEST = 8}; enum {NORTHEAST = NORTH | EAST, NORTHWEST = NORTH | WEST, SOUTHWEST = SOUTH | WEST, SOUTHEAST = SOUTH | EAST}; int up = (key[KEY_UP]) ? NORTH : 0; int down = (key[KEY_DOWN]) ? SOUTH : 0; int left = (key[KEY_LEFT]) ? WEST : 0; int right = (key[KEY_RIGHT]) ? EAST : 0; walk (up | down | left | right); Well, if you don't like ifs . --------------------------- |
julian_boolean
Member #8,201
January 2007
|
Thank you! That works wonders! |
Paul whoknows
Member #5,081
September 2004
|
Quote: Well, if you don't like ifs .
Actually you are using ifs, and that style of coding looks a bit cryptic IMHO. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
julian_boolean
Member #8,201
January 2007
|
It was suppose to be like this originally:
But I like Jakub's way of doing it. Very clean in my eyes. |
Paul whoknows
Member #5,081
September 2004
|
I prefer to separate logic and input, something like this:
____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Jakub Wasilewski
Member #3,653
June 2003
|
Quote: Actually you are using ifs, and that style of coding looks a bit cryptic IMHO. Technically not ifs, but conditional expressions. However, if you want to be anal about this we can get by without them . Now, who will stand up to the challenge and obfuscate the code even further? --------------------------- |
julian_boolean
Member #8,201
January 2007
|
Edit: Actually NM. I'm not sure what I want right now. I do know that when a player is not walking (not pressing any of the arrow keys) OR attacking (not pressing the A key) that idle() needs to be called. If the A key is pressed, the player shouldn't be able to walk or be idle UNTIL it completes the attack (finishes cycling through the frames.) Edit again: Okay, holy crap. I think an animation class is needed here. If I did make one, would I make it a friend class of cPlayer? I wanted to do something like this: cAnimation animate; animate.walk_cycle();
|
Paul whoknows
Member #5,081
September 2004
|
Quote: However, if you want to be anal about this we can get by without them Quote: I think an animation class is needed here. If I did make one, would I make it a friend class of cPlayer? I don't know, post your cPlayer class first. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
julian_boolean
Member #8,201
January 2007
|
It's okay, I think I know what I'm going to do now. Thanks everyone! |
SkaxCo
Member #8,323
February 2007
|
enum {NORTH = 1, EAST = 2, SOUTH = 4, WEST = 8}; enum {NORTHEAST = NORTH | EAST, NORTHWEST = NORTH | WEST, SOUTHWEST = SOUTH | WEST, SOUTHEAST = SOUTH | EAST}; int up = (key[KEY_UP]) ? NORTH : 0; int down = (key[KEY_DOWN]) ? SOUTH : 0; int left = (key[KEY_LEFT]) ? WEST : 0; int right = (key[KEY_RIGHT]) ? EAST : 0; walk (up | down | left | right); Just wondering, how does the second line work? Actually, a lot of it is confusing. Um... 1: fine. |
Onewing
Member #6,152
August 2005
|
Quote: Just wondering, how does the second line work? Those are binary or's. NORTH = 1 or (00000001) and EAST = 2 or (00000010). Or'd together NORTH | EAST = 3 or (00000011). ------------ |
julian_boolean
Member #8,201
January 2007
|
On second thought, here's some code for the animation class I was working on:
Basically, animate_cycle() would be used for walking or idling, so outside the class I could go: void cPlayer::idle() { draw_action = IDLE; // Drawing is done in a function elsewhere. animate_cycle(); } Then animate_half_cycle() would be used for actions a player can do that doesn't use as many frames, like sitting or swimming. And finally, animate_attack() does just that, it animates the attack of a player or monster. But it could also be used for other things I guess, like maybe having an animation of drinking a potion or something. Any suggestions on how I could greatly improve this? |
|