Search code examples
c++eventssdl

SDL Event - can't get the sprite to move down or right


I have stumbled upon a problem where when I move my sprite through the use of the arrow keys- it will only move up or left but when I try to move him right or down the screen, it won't move at all.

I have tried by replacing in the event loop: "SDLK_RIGHT:" to the same as the SDLK_LEFT case is now when that does work and it works perfect, the movement is still going left since the code says it, but as soon as I try to remove the negative number it doesn't move.

What I know floats should accept positive values as well :/

Code of the method:

    // Updates the sprites position in the game
void Sprite::tick( SDL_Event event )
{       

        if( event.type == SDL_KEYDOWN )
        {
            switch( event.key.keysym.sym )
            {
            case SDLK_LEFT:
                {
                    velocityX = -0.1f;
                    break;
                }
            case SDLK_RIGHT:
                {
                    velocityX = 0.5f;
                    break;
                }
            case SDLK_UP:
                {
                    velocityY = -0.1f;
                    break;
                }
            case SDLK_DOWN:
                {
                    velocityY = 0.5f;
                    break;
                }
            }
        }
        else if( event.type == SDL_KEYUP )
        {
            switch (event.key.keysym.sym)
            {
            case SDLK_LEFT: velocityX = 0.0f; break;
            case SDLK_RIGHT: velocityX = 0.0f; break;
            case SDLK_UP: velocityY = 0.0f; break;
            case SDLK_DOWN: velocityY = 0.0f; break;
            default: break;
            }
        }


    if(alive)
    {
        setBoundingbox(x_pos, y_pos, width, height);

//      if( inMidAir )
    //  {
        //  velocityX += 0.1f;
        //}


        x_pos += velocityX;

        if( ( x_pos < 0 ) || ( x_pos + width > 870 ))
        {
            x_pos -= velocityX;
        }

        y_pos += velocityY;

        if( ( y_pos < 0 ) || ( y_pos + height > 480 ) )
        {
            y_pos -= velocityY;
        }

    }// End of alive statement

}

and the event loop:

// Game loop
while( quit == false )
{
    while( SDL_PollEvent( &event ))
    {
        player.tick( event );
if( event.type == SDL_QUIT )
        {
            quit = true;
        }

    }

    applySurface(0, 0, background, screen);
    applySurface(player.get_xpos(), player.get_ypos(), 
        player.get_Surface(), screen);
    SDL_Flip(screen);
}

I hope someone can see why I can't move as I want


Solution

  • If your x_pos and y_pos are integers, then subtracting a small float from them (even 0.1f) is roughly equivalent to subtracting 1, as truncating back to an int effectively rounds down.

    That is, if x_pos equals 100, then 100 - 0.1f will evaluate to 99.9f, but get truncated to 99 as an int.

    But, when you try to move to the right, you have 100 + 0.5f, which evaluates to 100.5f, but gets truncated back to 100 as an int.

    That's why you can move up and to the left (both decreasing coordinates), but not down or to the right (increasing coordinates).

    You need to make your types consistent.