Skip to main content
Removed extraneous code. Converted big blocks of code into explanatory prose, in an attempt to keep the question from being closed. There's a legitimate question here which is relevant to lots of users; it's just hidden behind all the code blocks.
Source Link
Trevor Powell
  • 21.5k
  • 1
  • 63
  • 96

I am developing a small game. I am using SDL. I, and am unable to do continuous motion for my object when a key is pressedheld down.

My Code:

while( quit == false){

    while( SDL_PollEvent( &event ) )
    {

        avtr.handle_input(event);
        //If the user has Xed out the window
        if( event.type == SDL_QUIT )
        {
            //Quit the program
            quit = true;
        }
    }


    drawWorld();
    // Draw Avatar
    drawAvatar(avtr, avatar, screen);


    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
}

Drawing Avatar function

void drawAvatar( Avatar avtr, SDL_Surface* source, SDL_Surface* destination )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = avtr.getXPos();
offset.y = avtr.getYPos();
offset.h = avtr.getHeight();
offset.w = avtr.getWidth();

SDL_FillRect( destination, &offset, SDL_MapRGB( destination->format, 0x00, 0x77, 0x00 ) );
}

handle_inputI'm calling SDL_PollEvent() to retrieve all events during a frame, and passing each resulting SDL_Event structure into this function:

void Avatar::handle_input(SDL_Event keyInput){
//If a key was pressed
if( keyInput.type == SDL_KEYDOWN )
{
    //Adjust the velocity
    switch( keyInput.key.keysym.sym )
    {
    case SDLK_LEFT: Move(1); break;
    case SDLK_RIGHT: Move(2); break;
    }
}

The problem I am facing is, if that when I am pressinghold down the right keyright or left keyleft arrow keys, the avatar only moves once instead of moving continuously for as long as I hold down the key. How can I make the motion continue for as long as the key is held down?

I am developing a small game. I am using SDL. I am unable to do continuous motion for my object when a key is pressed.

My Code:

while( quit == false){

    while( SDL_PollEvent( &event ) )
    {

        avtr.handle_input(event);
        //If the user has Xed out the window
        if( event.type == SDL_QUIT )
        {
            //Quit the program
            quit = true;
        }
    }


    drawWorld();
    // Draw Avatar
    drawAvatar(avtr, avatar, screen);


    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
}

Drawing Avatar function

void drawAvatar( Avatar avtr, SDL_Surface* source, SDL_Surface* destination )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = avtr.getXPos();
offset.y = avtr.getYPos();
offset.h = avtr.getHeight();
offset.w = avtr.getWidth();

SDL_FillRect( destination, &offset, SDL_MapRGB( destination->format, 0x00, 0x77, 0x00 ) );
}

handle_input function

void Avatar::handle_input(SDL_Event keyInput){
//If a key was pressed
if( keyInput.type == SDL_KEYDOWN )
{
    //Adjust the velocity
    switch( keyInput.key.keysym.sym )
    {
    case SDLK_LEFT: Move(1); break;
    case SDLK_RIGHT: Move(2); break;
    }
}

The problem I am facing is, if I am pressing the right key or left key, the avatar only moves once instead of moving continuously.

I am developing a game using SDL, and am unable to do continuous motion for my object when a key is held down.

I'm calling SDL_PollEvent() to retrieve all events during a frame, and passing each resulting SDL_Event structure into this function:

void Avatar::handle_input(SDL_Event keyInput){
if( keyInput.type == SDL_KEYDOWN )
{
    //Adjust the velocity
    switch( keyInput.key.keysym.sym )
    {
    case SDLK_LEFT: Move(1); break;
    case SDLK_RIGHT: Move(2); break;
    }
}

The problem is that when I hold down the right or left arrow keys, the avatar only moves once instead of moving continuously for as long as I hold down the key. How can I make the motion continue for as long as the key is held down?

Source Link
Vabs
  • 143
  • 1
  • 3

Unable to detect continuous keypress event in SDL

I am developing a small game. I am using SDL. I am unable to do continuous motion for my object when a key is pressed.

My Code:

while( quit == false){

    while( SDL_PollEvent( &event ) )
    {

        avtr.handle_input(event);
        //If the user has Xed out the window
        if( event.type == SDL_QUIT )
        {
            //Quit the program
            quit = true;
        }
    }


    drawWorld();
    // Draw Avatar
    drawAvatar(avtr, avatar, screen);


    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
}

Drawing Avatar function

void drawAvatar( Avatar avtr, SDL_Surface* source, SDL_Surface* destination )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = avtr.getXPos();
offset.y = avtr.getYPos();
offset.h = avtr.getHeight();
offset.w = avtr.getWidth();

SDL_FillRect( destination, &offset, SDL_MapRGB( destination->format, 0x00, 0x77, 0x00 ) );
}

handle_input function

void Avatar::handle_input(SDL_Event keyInput){
//If a key was pressed
if( keyInput.type == SDL_KEYDOWN )
{
    //Adjust the velocity
    switch( keyInput.key.keysym.sym )
    {
    case SDLK_LEFT: Move(1); break;
    case SDLK_RIGHT: Move(2); break;
    }
}

The problem I am facing is, if I am pressing the right key or left key, the avatar only moves once instead of moving continuously.