SDL2 - Keyboard events for character movement have unexpected behavior

Viewed 38

I'm currently working on a small game and recently tried to introduce smoother movements for the player using velocity variables: when a key is pressed, the velocity is increased in the appropriate direction; and, when the same key is released, the velocity goes back to 0. However, when moving on the y axis, the character won't stop moving as it should when the key is released, which is weird considering everything works as intended when moving on the x axis.

Here's the function responsible for keyboard events:

void Game::processEvent(){                                                 
       switch(Engine::Instance().event.type){                                                                                               
         case SDL_KEYDOWN:                                                      
           switch(Engine::Instance().event.key.keysym.sym){                     
             case SDLK_LEFT: Entities[0]->vx=-0.0001f; break;  //Entities is a vector filled with pointers to objects like enemies, the player etc..               
             case SDLK_RIGHT: Entities[0]->vx=0.0001f; break;  //This is how velocity is set               
             case SDLK_UP: Entities[0]->vy=-0.0001f; break;                     
             case SDLK_DOWN: Entities[0]->vy=0.0001f; break;                    
           }                                                                    
           break;                                                               
         case SDL_KEYUP:                                                        
             switch(Engine::Instance().event.key.keysym.sym){ //Reset velocity in a direction only if the player was previously moving in that direction                  
               case SDLK_LEFT: if(Entities[0]->vx>0) Entities[0]->vx=0.0f; break; 
               case SDLK_RIGHT: if(Entities[0]->vx<0) Entities[0]->vx=0.0f; break;
               case SDLK_UP: if(Entities[0]->vy<0) Entities[0]->vy=0.0f; break; 
               case SDLK_DOWN: if (Entities[0]->vy>0) Entities[0]->vy=0.0f; break;
             }                                                                  
             break;                                                                                                                         
         default:                                                               
             break;                                                               
      } 
    }

And the function used to update objects' position:

    void Entity::updatePos(){                           
    x += vx;    //Velocity is added to the sprite's coords                                      
    y += vy;                                          
    dstrect = {static_cast<int>(x*800), static_cast<int>(y*800), static_cast<int>(w*800), static_    cast<int>(h*800)}; //Workaround to make sprites' coords and dimensions always relative to the screen, not great I know XD that's why x and y are floats between 0 and 1.
    } 

I've been scratching my head for a while as to why vy isn't reset properly when vx is. Any idea?

1 Answers

EDIT: Removing the 'if' statements before setting the velocity to 0 solved the problem, guess it was unnecessary in the first place.

Which give us instead:

case SDL_KEYUP:                   
switch(Engine::Instance().event.key.keysym.sym){
case SDLK_LEFT: Entities[0]->vx=0; break;
case SDLK_RIGHT: Entities[0]->vx=0; break;
case SDLK_UP: Entities[0]->vy=0; break;
case SDLK_DOWN: Entities[0]->vy=0; break;
default: break;  

(I can't flag this as the answer yet, sorry 'bout that)

Related