Advertisement

key_down

Started by May 22, 2000 02:56 AM
9 comments, last by gameprogrammerwiz 24 years, 6 months ago
ok, im useing a macro for my keydown/keyup functions in my game: #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) ========== now, im doing animations here, and the animations work fine, but not while the user is holding down one of the keys. here is the function that reads the keys. void game_main(void) { LPDIRECTDRAWSURFACE pdds; RECT rcRect; HRESULT ddrval; static int px = screen_width, py = screen_height; static int movementspeed = 10; static int player_left = 0; static int player_top = 0; int sprite_left, sprite_top; int centering_factor_x, centering_factor_y; if(KEY_DOWN(VK_ESCAPE)) { PostMessage(window_handle,WM_QUIT,0,0); } if(KEY_DOWN(VK_RIGHT)) { direction = 4; walk_frame = 6; if ((player_left+=movementspeed)>(x_bound-player_y_size)) player_left = (x_bound-player_y_size); } if(KEY_DOWN(VK_LEFT)) { direction = 3; walk_frame = 4; if ((player_left-=movementspeed)<0) player_left = 0; } if(KEY_DOWN(VK_UP)) { direction = 1; walk_frame = 0; if ((player_top-=movementspeed)<0) player_top = 0; } if(KEY_DOWN(VK_DOWN)) { direction = 2; walk_frame = 2; if ((player_top+=movementspeed)>(y_bound-player_x_size)) player_top = (y_bound-player_x_size); } if(direction == 1) //north { if(walk_frame == 0) { walk_frame = 1; goto skip; } if(walk_frame == 1) { walk_frame = 0; goto skip; } } if(direction == 2) //south { if(walk_frame == 2) { walk_frame = 3; goto skip; } if(walk_frame == 3) { walk_frame = 2; goto skip; } } if(direction == 3) //east { if(walk_frame == 4) { walk_frame = 5; goto skip; } if(walk_frame == 5) { walk_frame = 4; goto skip; } } if(direction == 4) //west { if(walk_frame == 6) { walk_frame = 7; goto skip; } if(walk_frame == 7) { walk_frame = 6; goto skip; } } skip: centering_factor_x = (screen_width>>1)+(player_y_size>>1); centering_factor_y = (screen_height>>1)+(player_x_size>>1); px = player_left + centering_factor_x; if (px > x_bound) px = x_bound; if (px < screen_width) px = screen_width; py = player_top + centering_factor_y; if (py > y_bound) py = y_bound; if (py < screen_height) py = screen_height; render_scene(px,py); sprite_left = player_left-(px-screen_width); sprite_top = player_top-(py-screen_height); rcRect.top = 0; rcRect.left = 0; rcRect.right = player_x_size; rcRect.bottom = player_y_size; pdds = lpddsplayer[walk_frame]; ddrval = lpddsback->BltFast(sprite_left, sprite_top, pdds, &rcRect, DDBLTFAST_WAIT / DDBLTFAST_SRCCOLORKEY); lpddsprimary->Flip(NULL,DDFLIP_WAIT); } =========================================================== now, this isnt so much of a DirectX question because i think my problem is with my KEY_DOWN function. when you press any of the arrow keys to move the player around the screen, the player sticks on the current animation frame, so my question is: is there another function that i can use for key_down that will CHECK TO SEE IF THE KEY IS DOWN EVERY TIME THE game_main(); FUNCTION IS RUN? because i think that the macro that i''m useing only checks to see if the key is down ONCE and then doesnt check to see if the user is still holding it down until they actually let up on the key. i''m sorry that this is so confusing, if you want to help me but dont understand my question...email me (or leave a post here) for the full source to see for yourself. thanks, -mike
==============================
whats a signature?
htm[s]l[/s]
ok, forget about emailing me unless you want the source...if you want to see my problem firsthand, click here to download the program.

NOTE: My problem is NOT the fact that the animations keep going after you let up on the keys, my problem is that the animations arent working WHILE the user is holding down one of the arrow keys.
==============================
whats a signature?
htm[s]l[/s]
Advertisement
Your problem seems to be that every time your Game_Main goes around and the player is still, say, holding the RIGHT arrow key, the walkframe is set to 6. You need a BOOL that says whether an animation is going on or not, and if it is, don''t reset the walkframe to the first value in it''s animation cycle.
ok, ive gotten one of the 4 animations to work...then i tyed to get the other 3 to work, but they dont...for some reason, the animation only works when he's moving up now, but i have the same code for all of the other directions:
===========================================================
if(KEY_UP(VK_RIGHT))
{
we_on = 0;
}
if(KEY_UP(VK_LEFT))
{
ww_on = 0;
}
if(KEY_UP(VK_UP))
{
wn_on = 0;
}
if(KEY_UP(VK_DOWN))
{
ws_on = 0;
}

if(KEY_DOWN(VK_ESCAPE))
{
PostMessage(window_handle,WM_QUIT,0,0);
}

if(KEY_DOWN(VK_RIGHT))
{
direction = 4;
if(we_on = 0)
{
we_on = 1;
walk_frame = 6;
}
if ((player_left+=movementspeed)>(x_bound-player_y_size)) player_left = (x_bound-player_y_size);
if(we_on = 1) { goto whatdir; }
}

if(KEY_DOWN(VK_LEFT))
{
direction = 3;
if(ww_on = 0)
{
ww_on = 1;
walk_frame = 4;
}
if ((player_left-=movementspeed)<0) player_left = 0;
if(ww_on = 1) { goto whatdir; }
}

if(KEY_DOWN(VK_UP))
{
direction = 1;
if(wn_on = 0)
{
wn_on = 1;
walk_frame = 0;
}
if ((player_top-=movementspeed)<0) player_top = 0;
if(wn_on = 1) { goto whatdir; }
}

if(KEY_DOWN(VK_DOWN))
{
direction = 2;
if(ws_on = 0)
{
ws_on = 1;
walk_frame = 2;
}
if ((player_top+=movementspeed)>(y_bound-player_x_size)) player_top = (y_bound-player_x_size);
if(ws_on = 1) { goto whatdir; }
}

whatdir:

if(direction == 1) //north
{
if(walk_frame == 0) { walk_frame = 1; goto skip; }
if(walk_frame == 1) { walk_frame = 0; goto skip; }
}

if(direction == 2) //south
{
if(walk_frame == 2) { walk_frame = 3; goto skip; }
if(walk_frame == 3) { walk_frame = 2; goto skip; }
}

if(direction == 3) //east
{
if(walk_frame == 4) { walk_frame = 5; goto skip; }
if(walk_frame == 5) { walk_frame = 4; goto skip; }
}

if(direction == 4) //west
{
if(walk_frame == 6) { walk_frame = 7; goto skip; }
if(walk_frame == 7) { walk_frame = 6; goto skip; }
}

skip:
//i do the blitting here...
===========================================================
why are the animations only working when the player goes up?

Edited by - gameprogrammerwiz on May 22, 2000 6:21:04 PM
==============================
whats a signature?
htm[s]l[/s]
Your code is pretty sloppy wirh all those gotos, which should be avoided unless all else fails.
ok, why does everyone HATE goto? it works fine without any bugs.
==============================
whats a signature?
htm[s]l[/s]
Advertisement
goto is bad because it leads to spagetthi code. When your code gets more complex, you''d have trouble keeping track of all those gotos.

If it works for you, sure, you could keep on using it. But if you''re working in a team, I don''t think your team mates would be very happy jumping back and forth through the source code (especially if there are many labels).

Just my $0.02


========================================================
If something sounds stupid but works, it's not stupid
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
hmm...well, i was only planning on using those two gotos anyway. and i work better alone, because i''m so impatent.

anyway...can anyone tell me how to fix this problem? i have no idea why it isnt working exept for the moving upwards animation.
==============================
whats a signature?
htm[s]l[/s]
hmm, not too sure why it''s only playing one animation,
so I just re-wrote the code section you gave the way I would do it. Try this out:

//I took out the control flags again, I use brute force
// instead ;-)

if(KEY_DOWN(VK_ESCAPE))
{
PostMessage(window_handle,WM_QUIT,0,0);
}

if(KEY_DOWN(VK_RIGHT))
{
direction = 4;
if ((player_left+=movementspeed)>(x_bound-player_y_size)) player_left = (x_bound-player_y_size);
}

if(KEY_DOWN(VK_LEFT))
{
direction = 3;
if ((player_left-=movementspeed)<0) player_left = 0;
}

if(KEY_DOWN(VK_UP))
{
direction = 1;
if ((player_top-=movementspeed)<0) player_top = 0;
}

if(KEY_DOWN(VK_DOWN))
{
direction = 2;
if ((player_top+=movementspeed)>(y_bound-player_x_size)) player_top = (y_bound-player_x_size);
}

//label ''whatdir:'' was here

if(direction == 1) //north
{
if (walk_frame != 0) walk_frame = 0;
else walk_frame = 1;
}
else if(direction == 2) //south
{
if (walk_frame != 2) walk_frame = 2;
else walk_frame = 3;
}
else if(direction == 3) //east
{
if (walk_frame != 4) walk_frame = 4;
else walk_frame = 5;
}
else if(direction == 4) //west
{
if (walk_frame != 6) walk_frame = 6;
else walk_frame = 7;
}

// label ''skip:'' was here so go do your blitting.

Try this one out. It *should* work.


Was that your fart or mine?
----------"i think that all this talking and such is paining my head to astounding annoyance" - Erick"Quoting people in your tag is cool. Quoting yourself is even cooler" - SpazBoy_the_MiteyDisco Love For Everyone
HEEEEEYYYY! it works! thank you soooo much! i''ll make sure to add you in the ''credits'' list in my game, as well as all of the other nice people who helped me.
==============================
whats a signature?
htm[s]l[/s]

This topic is closed to new replies.

Advertisement