I’m working on a space shooter and have a problem with control of my
spaceship. I’m useing SDL_KEYDOWN to control the input, but this don’t works,
if player press two keys (SDLK_UP and SDLK_LEFT) the player loose the
controll over the ship… it can move up and left (it should do so), but it
can also start moveing down and left or something else strange…
-------CODE------
//this is the main game loop.
while( game_state != GAME_EXIT )
{
switch(game_state)
{
case GAME_INIT:
{
Init_Game();
game_state=GAME_SPLASHUP;
} break;
case GAME_SPLASHUP:
{
Key_pressed();
Draw_Splashup();
PlaySound();
} break;
case GAME_MENU:
{
} break;
case GAME_STARTING:
{
InitGameGraphics();
game_state=GAME_RESTART;
} break;
case GAME_RUN:
{
Key_pressed();
myShip.move();
if(fireing){
if(lastfire+firerate <= SDL_GetTicks()){
fire();
lastfire=SDL_GetTicks();
}
}
koll();
DrawGameGraphics();
PlaySound();
} break;
case GAME_RESTART:
{
game_state=GAME_RUN;
} break;
case GAME_OVER:
{
Key_pressed();
DrawGameOver();
} break;
case GAME_EXIT:
{
} break;
}
}
— end of main gameloop----
//and this is my keypressed function.
void Key_pressed()
{
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
game_state=GAME_EXIT;
}
if(event.type == SDL_KEYDOWN){
if(game_state == GAME_SPLASHUP || game_state == GAME_OVER){
if(event.key.keysym.sym == SDLK_SPACE){
Restart_Game();
game_state=GAME_STARTING;
}
}
//gamekontrollen buggar utav bara fan h?r, ett skepp som flyger
// p? diagonalen tycks inte f?lja n?gra f?rnuftiga regler
// ?r det SDL som strular?
if(event.key.keysym.sym == SDLK_ESCAPE){
game_state=GAME_EXIT;
}
if(event.key.keysym.sym == SDLK_RCTRL){
fireing=true;
}
if(event.key.keysym.sym == SDLK_UP){
myShip.North=true;
myShip.South=false;
}
if(event.key.keysym.sym == SDLK_RIGHT){
myShip.West=true;
myShip.East=false;
}
if(event.key.keysym.sym == SDLK_LEFT){
myShip.East=true;
myShip.West=false;
}
if(event.key.keysym.sym == SDLK_DOWN){
myShip.South=true;
myShip.North=false;
}
}
if(event.type == SDL_KEYUP){
if(event.key.keysym.sym == SDLK_RCTRL){
fireing=false;
}
if(event.key.keysym.sym == SDLK_UP){
myShip.North=false;
}
if(event.key.keysym.sym == SDLK_RIGHT){
myShip.West=false;
}
if(event.key.keysym.sym == SDLK_LEFT){
myShip.East=false;
}
if(event.key.keysym.sym == SDLK_DOWN){
myShip.South=false;
}
}
}
}
----END OF CODE-------
Recursive, adj.; see Recursive
//Perra