   [SDL] Events Problems
  Hi there again! I'm having a problem with events. I have a code like this to handle inputs and to show blocks:while ( SDL_PollEvents( &event ) ){    if ( event.type == SDL_QUIT )    {   quit = true;    }   block.HandleInput();}block.Show();And the HandleInput() definition is:Blocks::HandleInput(){    if ( active == true )    {   if ( event.type == SDL_KEYDOWN )   {  switch ( event.key.keysym.sym )  { case SDLK_LEFT: xVel = xVel - BLOCK_SIZE; break; case SDLK_RIGHT: xVel = xVel + BLOCK_SIZE; break;  }   }   if ( event.type == SDL_KEYUP )   {  switch ( event.key.keysym.sym )  { case SDLK_LEFT: xVel = xVel + BLOCK_SIZE; break; case SDLK_RIGHT: xVel = xVel - BLOCK_SIZE; break;  }   }    }}Show() definition is something like this:Blocks::Show(){    x = xVel;    y = yVel;    ApplySurface( x, y, block, screen );}And finally, my CheckCollision() function is something like this:Blocks::CheckCollision(){   if ( activa == true )    {   if ( y + temp2->h == GROUND )   {  yVel -= yVel;  xVel -= xVel;activa = false;  return true;   }    }    return false;}Ok. The problem comes when block falls on the ground and a new block appears. Because block's constructor sets xVel and yVel to 0.. the new block has xVel = 0 and yVel = 0, if you're pressing a key or not! Then.. suppose you're pressing the left key and a new block appears. If you release the left key, the xVel of that block will be set to 23. So the block will move itself to the right without worrying if you're pressing a key or not.How can I solve it? I tried to set a temporary xVel and another with yVel to copy them to the new block before it appears. It works with the first three blocks but the next block has the same problem again! help me please. I'm getting frustrated :(Thank u in advance for your help![Edited by - GutyGu on July 10, 2006 7:49:55 PM]
  From how I understand your code, I'd say checking for the key state in block's constructor or where a new block gets created would be the easiest way to make it work. SDL_GetKeyState.;  Thank you! it works better now. But at a random point, let's say block number 25-30, it happens again! Do I have to limit keyboard rate or something like that? it's strange. It works fine until block number 20 or 25, or 30 sometimes.Thank you for your answer!   ;  Have a flag in each block. Set that flag to true on the keydown event. On the keyup event, only change the velocity if the flag is true. This stops the problem where you release a key just as a new block is created and it starts moving.Also, check those lines of code: "y = xVel;" looks wrong to me and so does "xVel -= yVel;" Maybe that's just because I don't understand what the code is meant to do, in which case more comments would help.It will also help if you are very clear about the conditions under which you expect a block to move and when you don't expect a block to move. It's hard to see what you intend, and you seem to be using position and velocity interchangably.   ;  Sorry for my bad typing, I fixed that errors on my post! :)How can I put a flag on my blocks? they're objects and at this time I've only CheckCollision() that tests if blocks are on the ground or not. If they are, a new block appears. My game loop is something like this ( I don't post my code because it's in spanish and it's an awful code.. lol ):- Timers.Start()- Check if some block collides with the ground. If it does, create a new block- Update screen ( put background and block that have been fallen )- Update Area ( Update the bidimensional array where I save the status of the blocks on the play area. If it's a block in, for example, X = 4 and Y = 3, I put PlayArea[4][3] = true. If it's not, I put false;- Manage handle input- Show blocks- Fps capCan you show me an example of how to using flags with my blocks? thank you so much!   ;  I got it. I didn't know what "flag" means :PThank you for all!   ;  Simply use a bool for a flag, something like this:// just as an example, don't know if this works how you intend it:class Block    : isMovingLeft(false), isMovingRight(false){    private:  bool isMovingLeft;  bool isMovingRight;}// then in handleinput:Block::HandleInput(){    if ( active == true )    {   if ( event.type == SDL_KEYDOWN )   {  switch ( event.key.keysym.sym )  { case SDLK_LEFT: xVel = xVel - BLOCK_SIZE; isMovingLeft = true; break; case SDLK_RIGHT: xVel = xVel + BLOCK_SIZE; isMovingRight = true; break;  }   }   if ( event.type == SDL_KEYUP )   {  switch ( event.key.keysym.sym )  { case SDLK_LEFT: // Here you make it clear what is the precondition if (isMovingLeft)  {xVel = xVel + BLOCK_SIZE;isMovingLeft = false; }    break; case SDLK_RIGHT: if (isMovingRight) {xVel = xVel - BLOCK_SIZE;isMovingRight = false; } break;  }   }    }};  Ahhhh that's better! I was using flags in a bad way. Now it works fine! thank u for the example!have a good coding day :)   