   SDL Holding Keys
  I got another question. I am trying to make it so that when a key is held, it's value becomes true if it is true a square will move. It becomes false when the key is depressed. For some reason, nothing moves. Here is the code:void Square::handle_input(){    //If a key was pressed    if( event.type == SDL_KEYDOWN )    {   //Adjust the velocity   switch( event.key.keysym.sym )   {  case SDLK_UP: upkey = true; break;  case SDLK_DOWN: downkey = true; break;  case SDLK_LEFT: leftkey = true; break;  case SDLK_RIGHT: rightkey = true; break;   }    }    //If a key was released    else if( event.type == SDL_KEYUP )    {   //Adjust the velocity   switch( event.key.keysym.sym )   {  case SDLK_UP: upkey = false; break;  case SDLK_DOWN: downkey = false; break;  case SDLK_LEFT: leftkey = false; break;  case SDLK_RIGHT: rightkey = false; break;   }    }if (upkey == true){xVel = +10;}if (downkey == true){xVel = -10;}if (leftkey == true){yVel = -10;}if (rightkey == true){yVel = +10;}if (upkey == false){xVel = 0;}if (downkey == false){xVel = 0;}if (leftkey == false){yVel = 0;}if (rightkey == false){yVel = 0;}} The booleans are declared at the start of the code.   
  In the segment of code you posted you aren't polling for events.  Are you doing that elsewhere?   ;  Won't this:if (upkey == true){xVel = +10;}increment xVel by 10 every time handle_input is called and upkey== true?  So, if you hold down upkey, the velocity (if xVel is a velocity) each call will progress like 0, 10, 20, 30, 40... (that is accelerating rather than moving it by 10) so it should instead beif (upkey == true){xVel = 10;}to be a constant velocity?   ;  yeah do you do sdl event pumping somewhere else?   ;Nope, incrementing every time would be xVel+=10;To do xVel=+10; is probably the same as xVel=10;But you never write it like this.Quote:Original post by PrestoChungWon't this:if (upkey == true){xVel = +10;}increment xVel by 10 every time handle_input is called and upkey== true?  So, if you hold down upkey, the velocity (if xVel is a velocity) each call will progress like 0, 10, 20, 30, 40... (that is accelerating rather than moving it by 10) so it should instead beif (upkey == true){xVel = 10;}to be a constant velocity?;  Yeah it does pump events:while (GameRunning == true){fps.start();while ( SDL_PollEvent ( &event ) ){mySquare.handle_input();if ( event.type == SDL_QUIT ){GameRunning = false;} I fixed a few things but it doesn't move while pressed and it moves very weird. AKA it sometimes speeds up, slows down, or doesn't move in the right direction. I changed my FPS from capped to uncapped and that it is not frame dependent, but for some reason it feels very choppy and laggy.[Edited by - Yelnats on May 27, 2010 3:00:45 PM];  I think I know the root of the problem. It's the move function. Here take a look:void Square::move( Uint32 deltaTicks ){    //Move the square left or right    box.x += xVel * ( deltaTicks / 1000.f );while ((xVel != 0) || (yVel != 0)){    //If the square went too far to the left or right or has collided with the wallif( box.x < 0 ){box.x = 0;}else if( box.x + SQUARE_WIDTH > SCREEN_WIDTH ){   //Move back   box.x = SCREEN_WIDTH - SQUARE_WIDTH;    }while( (check_collision( box, wall )) && (xVel >0 )){box.x -= 1;}while( (check_collision( box, wall )) && (xVel <0 )){box.x += 1;} box.y += yVel * ( deltaTicks / 1000.f ); //If the square went too far to the left or right or has collided with the wallif( box.y < 0 ){box.y = 0;}else if( box.y + SQUARE_HEIGHT > SCREEN_HEIGHT ){   //Move back   box.y = SCREEN_HEIGHT - SQUARE_HEIGHT;    }while(( check_collision( box, wall )) && (yVel >0 ) ){box.y -= 1;}while(( check_collision( box, wall )) && (yVel <0 ) ){box.y += 1;}}}It is only run once during the main loop. I think that I should make it so that if xVel and yVel isn't 0 than it should run again. But whenever I make a while loop like that the program stops responding. Any ideas?   ;  Nevermind (sorry for double post) I fixed it. It was because the delta time was a float and the movement was an integer. The float to integer calculation made it impossible for fast computers to run this program.   