Detect if key was pressed?

Oct 29, 2011 at 7:29pm
Hey, I've trying to detect if a key was pressed. Currently my code detects if a key is down but in the case of the game I'm making where the player can shoot a bullet, the bullet will disappear if the button is let go rather than continue on regardless of the state of the key now. Heres my code,its just a small part so you can see what I'm currently doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    switch (message)											
          {	      
           case WM_KEYDOWN:
		keys[wParam]=true;
		break;

          case WM_KEYUP:
		keys[wParam]=false;
		break; 
         }

       if(keys[VK_SPACE])
	{
	drawSprite(Bullet);
	Bullet.x += bulletV;
        }


Any help would be greatly appreciated :)
Oct 29, 2011 at 7:40pm
What is the problem? Does your current method not work? You could try polling for input in the main loop using a function like GetAsyncKeyState.
Oct 29, 2011 at 7:56pm
The current method checks if the key is held down, so if you keep it held down the bullet will go all the way off screen, but if you let go of the key, then the bullet would disappear . I need it to check if the key was pressed so that once its been pressed even if its let go, the bullet will keep going off the screen.
Last edited on Oct 29, 2011 at 7:57pm
Oct 30, 2011 at 11:26am
Then add a new variable that you don't reset when the button is released. Plus code to detect when the bullet disappears off screen, and resets the variable.

Actually, you will prob want an array/vector of such variables, if you want to support multiple bullets?
Last edited on Oct 30, 2011 at 11:27am
Oct 30, 2011 at 4:11pm
Ahh of course, worked like a charm :) Thanks. And yeah I already had the hit detection in, so I just made the variable false when it hits.

While on the subject, how would I go about making an array of a particular sprite? Is it just like how its done in flash?
Topic archived. No new replies allowed.