(SDL) Input question
I was wondering , I am making a 2D Sidescrolling game , and I want to make the players ship shoot when the Spacebar is released , not when it is pressed. I know you can do this in java but I was wondering how you could do it in SDL using C++.
My Current Project Angels 22 (4E5)
i tried but it didn't seem to work.
This is how I tried
This is how I tried
if(event.type == SDL_KEYUP){if(keys[SDLK_SPACE]){ addBullet();}}
My Current Project Angels 22 (4E5)
Quote: Original post by Sir Sapo
i tried but it didn't seem to work.
This is how I tried
*** Source Snippet Removed ***
Does "keys' represent the state of the keys? If so, keys[space] may be set to false depending on where in your code you poll the keyboard. Try removing the inner "if".
Not giving is not stealing.
I am pretty new to SDL so I am just going off of a tutorial right now. This is where keys is first in the program.
keys = SDL_getKeyState(NULL)
keys = SDL_getKeyState(NULL)
My Current Project Angels 22 (4E5)
Here is my entire Game Loop...
while(done == 0) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } keys = SDL_GetKeyState(NULL); if(event.type == SDL_KEYDOWN) { if ( keys[SDLK_UP] ) { player.accelY(-3); } if ( keys[SDLK_DOWN] ) { player.accelY(3); } if ( keys[SDLK_LEFT] ) { player.accelX(-3); } if ( keys[SDLK_RIGHT] ) { player.accelX(3); } } if(event.type == SDL_KEYUP) { if(keys[SDLK_SPACE]){addBullet();} } GameLoop(); }
My Current Project Angels 22 (4E5)
Don't mix the event handling with SDL_GetKeyState like that. Events only fire when a key is pressed or released, but later you're checking the state to see if the key's been constantly held down or not.
Firstly, just remove the 'if(event.type == SDL_KEYDOWN)' bit before checking the 'keys' array. That's totally irrelevant and redundant here.
Secondly, move the 'if(event.type == SDL_KEYUP)' section into the while loop. Otherwise you'll never check for that space-bar release unless it happened to be the last event of that frame.
Firstly, just remove the 'if(event.type == SDL_KEYDOWN)' bit before checking the 'keys' array. That's totally irrelevant and redundant here.
Secondly, move the 'if(event.type == SDL_KEYUP)' section into the while loop. Otherwise you'll never check for that space-bar release unless it happened to be the last event of that frame.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement