   Four direction walking
  I'm making a roleplaying game in 2d and have some problems with making the character walking. Making the character change x and y direction is working fine, but the problem occurs when I add a walk effect. I managed to solve the first problem and make the character actually move his legs when walking in all directions but when you press many keys at the same time there are some conflicts.One example is when you walk upwards and press the right key at the same time and then release the up button. If you do that the character just glides.Here is some of the code i'm using:    xPos+=xVel;   if ((xPos&lt;0) || (xPos+playerBox[frame].w &gt; MAP_WIDTH)) {   xPos-=xVel;    }   yPos+=yVel;   if ((yPos&lt;0) || (yPos+playerBox[frame].h &gt; MAP_HEIGHT)) {   yPos-=yVel;    }   if (yVel!=0 || xVel!=0) {  // if the player is moving up  if (yVel&lt;0 && direction==1) { if (frame==5)frame=3; elseframe++;  }    // if the player is moving down  if (yVel&gt;0 && direction==3) { if (frame==2)frame=0; elseframe++;  }// if the player is moving right  else if (xVel&gt;0 && direction==2) { if (frame==7)frame=6; elseframe++;  }// if the player is moving left  else if (xVel&lt;0 && direction==4) { if (frame==9)frame=8; elseframe++;  }}The event code looks like this:  case SDLK_LEFT: { xVel-=speed/3; frame=8; direction=4; break; }  case SDLK_UP: { yVel-=speed/3; frame=3; direction=1; break; }  case SDLK_RIGHT: { xVel+=speed/3; frame=6; direction=2; break; }  case SDLK_DOWN: { yVel+=speed/3; frame=0; direction=3; break; }The player rect currently consists of 9 elements, the first three (0-2) is the graphics when the character is walking down, 3-5 = walking up, 6-7 = walking right, 8-9 = walking left;thankful for any help, if you don't know any way to do it with this code, how would you do it?   
  You could give some keys priority. So when you test for input, if a key IS pressed, then set a bool value to true (ie bool keyfFound) so that you will not change it, or just break of of the loop. Hope that helps   ;  Thanks for the fast reply, that gave me an idea I can use. I will post here again if it works.   ;  No, it won't work no matter what I do, another more annoying problem that occurs is that the whole spritesheet shows itself sometimes. I tried to log every key in bool variables like this:// class membersbool leftIsDown;bool rightIsDown;bool downIsDown;bool upIsDown;case SDLK_LEFT: { xVel-=speed/3; frame=8; leftIsDown=true; break; }case SDLK_UP: { yVel-=speed/3; frame=3; upIsDown=true; break; }case SDLK_RIGHT: { xVel+=speed/3; frame=6; rightIsDown=true; break; }case SDLK_DOWN: { yVel+=speed/3; frame=0; downIsDown=true; break; }If I add an if statement that forbids moving in more than one direction at a time, the keyup even handler makes it move at the opposite side because it executes anyway.   ;  The problem in this case seems you need to have a way to tell when the user lets go of the button, I think SDL has a way of doing this from the examples I've seen, try this out:SDL_Event keyevent;    //The SDL event that we will poll to get events.while (SDL_PollEvent(&keyevent))   //Poll our SDL key event for any keystrokes.{  switch(keyevent.type)  {    case SDL_KEYDOWN:    { switch(keyevent.key.keysym.sym) {   case SDLK_LEFT:   { // only execute onceif(leftIsDown == false) {  xVel-=speed/3;  frame=8;  leftIsDown=true;  break;}   }   // other case statements } // end switch    }    case SDL_KEYUP:    { switch(keyevent.key.keysym.sym) {   case SDLK_LEFT:   {  leftIsDown=false;   }   // other case statements }    }  } // end switch} // end while;Quote:Original post by nobodynewsThe problem in this case seems you need to have a way to tell when the user lets go of the button, I think SDL has a way of doing this from the examples I've seen, try this out:Yes, i'm also using keyup, the problem is that I don't know how I should do the handling of the bool variables in the show function.This is the code for my KeyUp right now:    if (event.type == SDL_KEYUP) {   switch (event.key.keysym.sym) {  case SDLK_LEFT: {  xVel+=speed/3;  leftIsDown=false;  break;   }  case SDLK_UP: {  yVel+=speed/3;  upIsDown=false;  break;   }  case SDLK_RIGHT: { xVel-=speed/3;  rightIsDown=false;  break;   }  case SDLK_DOWN: {  yVel-=speed/3;  downIsDown=false;  break;   }  } }And this is the code for the KeyDown:    if (event.type == SDL_KEYDOWN) {   switch (event.key.keysym.sym) {  case SDLK_LEFT: {  if (leftIsDown==false) { xVel-=speed/3; frame=8; leftIsDown=true;break;  }  }  case SDLK_UP: {  if (upIsDown==false) {  yVel-=speed/3;  frame=3;  upIsDown=true; break;   }  }  case SDLK_RIGHT: { if (rightIsDown==false) {xVel+=speed/3; frame=6; rightIsDown=true; break; }  }  case SDLK_DOWN: {  if (downIsDown==false) {yVel+=speed/3; frame=0; downIsDown=true;break;  }  }It's almost the same as before but I posted it anyways. What I think is wrong is the code in the player show function. Do you know any way to check for the bool variables that could prevent the movements to become buggy. The closest i've come to make it work, is that the character walks normally but glides if you press one or two keys in a wrong direction at the wrong time. I atleast got the error where the whole sprite sheet got visible when pressing two keys at the same time and releasing the other while walking.This is the code in the player show function I use right now, i've changed it many times but this is the current code:    if (yVel!=0 || xVel!=0) {// if the player is moving upwards  if (upIsDown) { if (frame==5)frame=3; elseframe++;  }  // if the player is moving downwards  if (downIsDown) { if (frame==2)frame=0; elseframe++;  }    // if the player is moving right  if (rightIsDown) { if (frame==7)frame=6; elseframe++;  }    // if the player is moving left  if (leftIsDown) { if (frame==9)frame=8; elseframe++;  }   }    }Do you have any idea how I could make this code work?   ;  You need a way to prioritize the keys.  So if you press UP and then DOWN, your character will go down cause a variable  will tell your draw function that your guy should be drawn facing down since it was pressed last.  The part I'm not sure how to do is remember the order you pressed the keys.For example, if you press DOWN, UP, RIGHT, what happens when you let go of UP and then RIGHT?  I'm curious about this as well.   ;  I didn't read the code, so I might be misunderstanding the question but if not this should help:You have two values (or a vector)  (x, y)At the beginning of the frame, (x, y) = (0, 0) (Set them both to zero)If left is pressed : x = x - 1If right is pressed : x = x + 1If up is pressed : y = y - 1If down is pressed : y = y + 1Then when you decide what direction the character should be facing, look at this vector.  If he is moving in the X direction then x will be non-zero (either -1 or 1).  If both left and right are pressed, these will cancel out and be 0.  Y movement is the same.Now that we know what directions in both X and Y the character is walking, you can choose the walk sprite for that direction.I hope this was what you were looking for.Edit: It seems like in the code you are not checking all possible directions each frame.  This would be your problem.  The above idea is similar to what you are doing now, but it chooses the correct direction every frame.This can be extended for the diagonals if you only have four anims (not eight) by looking for the previous direction as well, and choosing between two anims depending.Just make sure to always look at all eight every frame (or even better, put them in some sort of lookup table so that you only have to look at one)[Edited by - JBourrie on May 26, 2006 12:01:23 PM];Quote:Original post by fooman_69You need a way to prioritize the keys.  So if you press UP and then DOWN, your character will go down cause a variable  will tell your draw function that your guy should be drawn facing down since it was pressed last.  The part I'm not sure how to do is remember the order you pressed the keys.For example, if you press DOWN, UP, RIGHT, what happens when you let go of UP and then RIGHT?  I'm curious about this as well.If i Press DOWN and UP he stops, but if I also push RIGHT at the same time, the whole spritesheet appears.Quote:Original post by JBourrieI didn't read the code, so I might be misunderstanding the question but if not this should help:You have two values (or a vector) (x, y)At the beginning of the frame, (x, y) = (0, 0) (Set them both to zero)If left is pressed : x = x - 1If right is pressed : x = x + 1If up is pressed : y = y - 1If down is pressed : y = y + 1Then when you decide what direction the character should be facing, look at this vector. If he is moving in the X direction then x will be non-zero (either -1 or 1). If both left and right are pressed, these will cancel out and be 0. Y movement is the same.Now that we know what directions in both X and Y the character is walking, you can choose the walk sprite for that direction.I hope this was what you were looking for.The information about which direction he is facing is not the problem. I'm done with the code that actually moves the character, i've also made the character move his legs by changing sprite when a certain key is pressed, the problem is that when I press many keys at the same time things can get buggy.When the up key is pressed the frame is set to 3 and when the player is moving up it changes between frame 3 and 4 with this if statement:if (upIsDown) {    if (frame==5)   frame=3;    else   frame++;}In the beginning I had this code instead:if (yVel<0) {    if (frame==5)   frame=3;    else   frame++;}When I added the direction variable into the program which had the values 1, 2, 3 and 4 depending on which direction. The problem where the whole spritesheet appeared, disappeared. However, the error where the character glides when pushing many buttons in a certain way still remained. If I remember right the code for each if statement looked like this back then:if (yVel<0 && direction==1) {    if (frame==5)   frame=3;    else   frame++;}While the input code looked like this:  case SDLK_UP: { yVel-=speed/3;  frame=3;  direction=1; break;  }From that point on I didn't manage to make it become any better. It's not that I haven't clipped out every movement of the character into the playerBox rect and it's not that the frame numbers or anything like that is wrong.I know what you mean with priorizing the keys but I have no idea how to implement it other than making making && if statements, but that didn't seem to work either in this case because I tried it before.   ;  I only briefly looked through the code but I would keep all the speed and frame modifcations out of the event loop so all you key ups and down will only modify the bool variables. Then in the main game loop have an if else statement for the directions which will give the keys proirity. e.g.if(UP){ //modify frame, change speed}else if (RIGHT){ //modify frame, change speed}else if(DOWN){ //modify frame, change speed}else if (LEFT){ //modify frame, change speed}This way, when the player presses UP and RIGHT, the character will only move UP. Of course this is taking into assumption that you only have 4 four and this is still a crude way of giving keys priority but hell it works. :)   