Moving sprites.
Well I''ve sort of got my first game up and running now, but I''ve encountered problems when moving sprites. I''m using the following code in the WindowProc function:
case WM_KEYDOWN: //Key pressed.
if (KeyboardHandler(wParam)) //If ''Q'' key pressed on menu screen.
DestroyWindow(hwnd); //End game.
break;
The KeyboardHandler(wParam) function calls separate functions depending on what stage the game is in, ie playing, menu etc. This is what I''m using to handle keypresses in the game:
BOOL GameKeyboardHandler(WPARAM keyPress)
{
BOOL result = FALSE; //Return true if game is to exit.
switch(keyPress)
{
case VK_ESCAPE:
result = TRUE; //Exit game.
break;
case VK_UP:
ObjectManager.move(0, 1, 0); //Move first tank forward.
break;
case VK_DOWN:
ObjectManager.move(0, -1, 0); //Move first tank backward.
break;
case VK_LEFT:
ObjectManager.turn(0, -1); //Turn first tank left.
break;
case VK_RIGHT:
ObjectManager.turn(0, 1); //Turn first tank right.
break;
case ''P'':
ObjectManager.fire(0); //First tank fires missile.
break;
case ''W'':
ObjectManager.move(1, 1, 1); //Move second tank forward.
break;
case ''S'':
ObjectManager.move(1, -1, 1); //Move second tank backward.
break;
case ''A'':
ObjectManager.turn(1, -1); //Turn second tank left.
break;
case ''D'':
ObjectManager.turn(1, 1); //Turn second tank right.
break;
case ''Q'':
ObjectManager.fire(1); //Second tank fires missile.
break;
}
return result;
} //End KeyboardHandler(WPARAM keyPress).
Basically the problem is when the up key is held for either tank, they move forward but if anyother key is pressed, like the other player moving, turning, firing etc it stops the first tank moving momentarily and then starts moving it again. Am I possibly handing the keypresses incorrectly or is this something that could be fixed by using DirectInput? The game is available at http://www.angelfire.com/realm/fap/ if you want to see what I''m talking about.
A solution is to create an array to store all possible keys that are down. When you get a WM_KEYDOWN message - make that keys index true. When you get the WM_KEYUP make it false. Have your Keyboard handler process that.
i.e.
if(KeysDown[''w''] == TRUE)
MoveForward();
if(KeysDown[''s''] == TRUE)
MoveBackward();
etc.
Jack
i.e.
if(KeysDown[''w''] == TRUE)
MoveForward();
if(KeysDown[''s''] == TRUE)
MoveBackward();
etc.
Jack
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement