c++ - SDL movement stops -
i'm building spaceshooter in sdl , c++, motion working far. problem if press, lets , add together right key (now i'm pressing , right @ same time) ship stays still short time.
same happens when moving + right, if allow go sec pressed key, motion stops , have start pressing keys downwards again.
i guess problem holding , adding keypresses. nice if tell me at.
i'm using sdl2.
uint8 const *keystate = sdl_getkeyboardstate(null); keystate = sdl_getkeyboardstate(null); while(exit == false) { if( sdl_pollevent(&event) != 0 ) { if(event.type == sdl_quit) { exit = true; } if (keystate[sdl_scancode_left] ) { ship.move(-2, 0); } if (keystate[sdl_scancode_right] ) { ship.move(2, 0); } if (keystate[sdl_scancode_up] ) { ship.move(0, -2); } if (keystate[sdl_scancode_down] ) { ship.move(0, 2); } if (keystate[sdl_scancode_space]) { ship.shoot(); } } sdl_blitsurface(ship.getsurface(), null, surface, ship.getrect()); sdl_updatewindowsurface( window ); sdl_fillrect(surface, null, 0); } it not fixed that:
if (keystate[sdl_scancode_down] && keystate[sdl_scancode_right) { ship.move(2, 2); }
sdl_pollevent triggered when key state changes, , believe if press , hold key events according key repeat rate. should move keyboard checking code outside if( sdl_pollevent(&event) != 0 ):
while(exit == false) { if( sdl_pollevent(&event) != 0 ) { if(event.type == sdl_quit) { exit = true; } } // may need take business relationship elapsed time accomplish constant speed // regardless of frame rate if (keystate[sdl_scancode_left] ) { ship.move(-2, 0); } if (keystate[sdl_scancode_right] ) { ship.move(2, 0); } if (keystate[sdl_scancode_up] ) { ship.move(0, -2); } if (keystate[sdl_scancode_down] ) { ship.move(0, 2); } if (keystate[sdl_scancode_space]) { ship.shoot(); } sdl_blitsurface(ship.getsurface(), null, surface, ship.getrect()); sdl_updatewindowsurface( window ); sdl_fillrect(surface, null, 0); } read the doc, code yours explicitly named wrong there, , illustration of right code provided.
c++ sdl sdl-2
No comments:
Post a Comment