yorde
yorde

Reputation: 3

SDL x and y movement

Why wont this code let me move up or left?

while( quit == false )
    { 


    apply_surface( 0,0, back,screen );
    apply_surface( playerx,playery,player, screen );


    SDL_Flip( screen );

        //While there's an event to handle
        while( SDL_PollEvent( &event ) )
        {
            if(event.type == SDL_KEYDOWN)
            {
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:playery=playery-10;
                case SDLK_DOWN:playery=playery+10;
                case SDLK_LEFT:playerx=playerx-10;
                case SDLK_RIGHT:playerx=playerx+10;
                }
            }       

Upvotes: 0

Views: 383

Answers (1)

Attila
Attila

Reputation: 28802

You need to use a break after each case in the switch Otherwise the execution "falls through" to the next line, potentially cancelling your action (and indeed SDLK_UP and SDLK_LEFT are the ones where the cancellation happens)

Upvotes: 3

Related Questions