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?