Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: ncw911 on Sat 06/07/2013 07:19:06

Title: Keyboard Movement help(ANSWERED)
Post by: ncw911 on Sat 06/07/2013 07:19:06
Im currently using the "keboard movement module" and I've encountered a problem. I have the movement set to "pressing" and everything seemed to work fine until I recently realized a problem.

If I hold the right key, my character walks right as expected. If while im still holding the right key, I also hold the Left key, my character will turn and walk left. If I were to let go of the Left key while I am still holding the Right key, my character will turn and walk right again. This is the desired movement and not the problem, however, it does not work this way vice versa.

"for example"

If I hold the Left key, my character walks left. If while im still holding the Left key, I also hold the Right key, my character just continues to walk left, and will not turn to walk right as I intend for it to do.
'
so, I am wondering if there is a fix to this problem, that would make my character (who is walking left) turn to walk right if I hold the Right button while I am still holding the Left button from earlier.

I hope I explained the problem well, I'd appreciate the help.


Pretty sure this doesnt belong in the Modules & Plugins forum, not sure how to move it......
Title: Re: Keyboard Movement help
Post by: Khris on Sat 06/07/2013 10:59:48
Yeah, the built-in module is biased like that due to the order of checks for keys being pressed.

Remove the entire repeatedly_execute() and replace it with:

Code (ags) Select
int _dx, _dy;
int left_frames, right_frames, up_frames, down_frames;

function repeatedly_execute() {

//--------------------------------------------------
// Pressing mode
//--------------------------------------------------

if ((IsGamePaused() == true) || (KeyboardMovement_Mode != eKeyboardMovement_Pressing) || (IsInterfaceEnabled() == false) || (player.on == false)) return 0;
  // if game is paused, module or mode disabled, interface disabled or player character hidden, quit function
   
  bool left, up, right, down;
   
  if (IsKeyPressed(KeyboardMovement_KeyUpLeft)) { up = true; left = true; }
  if (IsKeyPressed(KeyboardMovement_KeyUp)) up = true;   
  if (IsKeyPressed(KeyboardMovement_KeyUpRight)) { up = true; right = true; }
  if (IsKeyPressed(KeyboardMovement_KeyRight)) right = true;   
  if (IsKeyPressed(KeyboardMovement_KeyDownRight)) { down = true; right = true; }
  if (IsKeyPressed(KeyboardMovement_KeyDown)) down = true;   
  if (IsKeyPressed(KeyboardMovement_KeyDownLeft)) { down = true; left = true; }
  if (IsKeyPressed(KeyboardMovement_KeyLeft)) left = true;
 
  if (IsKeyPressed(KeyboardMovement_KeyStop)) { up = false; left = false; down = false; right = false; }
 
  if (left) left_frames++; else left_frames = 0;
  if (right) right_frames++; else right_frames = 0;
  if (left && right) {
    left = left_frames < right_frames;
    right = !left;
  }
  if (up) up_frames++; else up_frames = 0;
  if (down) down_frames++; else down_frames = 0;
  if (up && down) {
    up = up_frames < down_frames;
    down = !up;
  }
 
  int dx = right * DISTANCE - left * DISTANCE;
  int dy = down * DISTANCE - up * DISTANCE;
 
  if (dx != _dx || dy != _dy) player.WalkStraight(player.x + dx, player.y + dy, eNoBlock); // walk player character to the new coordinates
 
  _dx = dx; _dy = dy;
}
Title: Re: Keyboard Movement help
Post by: ncw911 on Sat 06/07/2013 17:11:32
Thanks for the help! Its working fine now.