How do i move with arrow keys?
Hey everyone, I wanna make a game, but i cant for 2 reasons:
i cant move with the arrows and i dont know 2d c++
I wanna make it so when sumone presses the up arrow, it makes the characters Y go up like 2 or so, like:
if(//up arrow here!)
playery=playerY+3;
sumthin like that but i dont know what to put for the up arrow!
And if anyone knows a good site on teaching 2d programming, id be much apreciated! Thanks!
Yay opengl! im on me way to maybe making a REAL bad mario kart knock off! ACE!
Use GetAsyncKeyState(VIRTUAL KEY) to detect a key press
Virtual Key Codes
VK_UP - up arrow
VK_DOWN - down arrow
VK_LEFT - left arrow
VK_RIGHT - right arrow
so to detect if up arrow has been pushed
if(GetAsyncKeyState(VK_UP) & 0x80)
{
//key has been pushed
}
There are many other ways of doing it aswell such as through the windows message handler or direct input
A good 2d site is:
http://www.2dgame-tutorial.com/
Good luck
Virtual Key Codes
VK_UP - up arrow
VK_DOWN - down arrow
VK_LEFT - left arrow
VK_RIGHT - right arrow
so to detect if up arrow has been pushed
if(GetAsyncKeyState(VK_UP) & 0x80)
{
//key has been pushed
}
There are many other ways of doing it aswell such as through the windows message handler or direct input
A good 2d site is:
http://www.2dgame-tutorial.com/
Good luck
U rock!
Hey thanks a lot, but one last question, what is the numbers after for?
Also, how would i asign that to a variable like
char? Playermoveleft=GetAsyncKeyState(VK_UP) & 0x80; ?
Or what?
[edited by - Quino on July 11, 2003 11:51:39 PM]
Hey thanks a lot, but one last question, what is the numbers after for?
Also, how would i asign that to a variable like
char? Playermoveleft=GetAsyncKeyState(VK_UP) & 0x80; ?
Or what?
[edited by - Quino on July 11, 2003 11:51:39 PM]
Yay opengl! im on me way to maybe making a REAL bad mario kart knock off! ACE!
The numbers afterwards are just the bits you are testing if GetAsyncKeyState(VIRTUAL KEY) contains.
Heres a tutorial on bitwise operators:
http://koti.mbnet.fi/iluwatar/progr/bitwise.htm
Dont worry though you usually only need to test 0x80, so you can usually leave that as it is in your code.
I dont quite understand why you would want to assign that to a variable, but if you want to set a flag:
BOOL HasUpKeyBeenPushed = FALSE;
if(GetAsyncKeyState(VK_UP) & 0x80)
{
HasUpKeyBeenPushed = TRUE;
}
Otherwise just do what you need to do inside the call:
if(GetAsyncKeyState(VK_UP) & 0x80)
{
PlayerYPos++;
}
If you need more help using GetAsyncKeyState then just look at MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getasynckeystate.asp
However if you did want to assign the return value to a variable, this is what it would return:
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.
Hope that answered your questions
[edited by - Xtreme11 on July 12, 2003 1:22:49 AM]
Heres a tutorial on bitwise operators:
http://koti.mbnet.fi/iluwatar/progr/bitwise.htm
Dont worry though you usually only need to test 0x80, so you can usually leave that as it is in your code.
I dont quite understand why you would want to assign that to a variable, but if you want to set a flag:
BOOL HasUpKeyBeenPushed = FALSE;
if(GetAsyncKeyState(VK_UP) & 0x80)
{
HasUpKeyBeenPushed = TRUE;
}
Otherwise just do what you need to do inside the call:
if(GetAsyncKeyState(VK_UP) & 0x80)
{
PlayerYPos++;
}
If you need more help using GetAsyncKeyState then just look at MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getasynckeystate.asp
However if you did want to assign the return value to a variable, this is what it would return:
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.
Hope that answered your questions
[edited by - Xtreme11 on July 12, 2003 1:22:49 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement