Need help with a keyboard movement problem... [SOLVED]

Started by Snake, Wed 23/03/2011 03:25:58

Previous topic - Next topic

Snake

Hello, there.

Even though it's been a while since I last posted for help here, my problems are still just as elementary.

In an attempt to keep myself busy, I am playing with an idea I've had for a project I've been planning.

Instead of using a module I am instead "emulating" platform movement by simply moving the character left/right and animating the jumping.

All is surprisingly going well, except for one little problem that is bugging me:

While walking, the player will animate in the opposite direction if the new directional key is pressed before the previous directional key is released.

Example: Holding left arrow, press and hold right arrow whilst still holding left arrow. Let go of left arrow whilst still holding right arrow and the player is still animating in the left direction.

Here's my code:
-- Please take note that this is one of the many variations of code that I've tried that have had the same result --
-- Also, never mind the jumping section. It's been incomplete since this problem arose --

Thanks for any help

PS
If you would like me to comment the code a little better for you, just ask and I will do so...
Code: ags

// main global script file

//********************
int facing=1;
//Which direction P1
//is facing:
//1= LEFT, 2= RIGHT
//********************
int running=0;
//Is P1 running?
//0= No, 1= Yes
//********************
int jumping=0;
//Is P1 jumping?
//0= No, 1= Yes
//********************
int duck=0;
//Is P1 ducking?
//0= No, 1= Yes
//********************


// called when the game starts, before the first room is loaded
function game_start() 
{
  player.FaceLocation(0, player.y, eNoBlock);
}

// put anything you want to happen every game cycle in here
function repeatedly_execute() 
{

if (running==0)
{
  if (facing==1){
    player.Loop=1;
  }
  else if (facing==2){
    player.Loop=2;
  }
}

if (IsKeyPressed(eKeyLeftArrow))
{
  facing=1;
  player.x-=2;
  if ((jumping==1)||(running!=0)){}
  else {
    player.Animate(12, 3, eRepeat, eNoBlock, eForwards);
    running=1;
  }
}

if (IsKeyPressed(eKeyRightArrow))
{
  facing=2;
  player.x+=2;
  if ((jumping==1)||(running!=0)){}
  else {
    player.Animate(13, 3, eRepeat, eNoBlock, eForwards);
    running=1;
  }
}

if (IsKeyPressed(eKeyUpArrow))
{
  if (facing==1){
    if (jumping==1){}
    else if (jumping==0){
      jumping=1;
      player.Animate(10, 2, eOnce, eNoBlock, eForwards);
    }
  else if (facing==2){
    if (jumping==1){}
    else if (jumping==0){
      jumping=1;
      player.Animate(11, 2, eOnce, eNoBlock, eForwards);
    }
  }
  }
}

if ((((IsKeyPressed(eKeyLeftArrow)==false)&&(IsKeyPressed(eKeyRightArrow)==false)&&(IsKeyPressed(eKeyUpArrow)==false)&&(IsKeyPressed(eKeyDownArrow)==false))))
{
  if (facing==1){
    player.UnlockView();
    running=0;
  }
  else if (facing==2){
    player.UnlockView();
    running=0;
  }
}

}
 
// put here anything you want to happen every game cycle, even when the game is blocked
function repeatedly_execute_always() 
{

}

// called when a key is pressed. keycode holds the key's ASCII code
function on_key_press(eKeyCode keycode) 
{
  if (IsGamePaused()) keycode = 0; // game paused, so don't react to keypresses
  if (keycode == eKeyCtrlQ) QuitGame(1); // Ctrl-Q
  if (keycode == eKeyF9) RestartGame(); // F9
  if (keycode == eKeyF12) SaveScreenShot("scrnshot.pcx");  // F12
  if (keycode == eKeyCtrlS) Debug(0,0); // Ctrl-S, give all inventory
  if (keycode == eKeyCtrlV) Debug(1,0); // Ctrl-V, version
  if (keycode == eKeyCtrlA) Debug(2,0); // Ctrl-A, show walkable areas
  if (keycode == eKeyCtrlX) Debug(3,0); // Ctrl-X, teleport to room
}

function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
}

function dialog_request(int param) {
}

Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Khris

I always do it like this, instead of

Code: ags
if (IsKeyPressed(eKeyLeftArrow))


I use

Code: ags
if (IsKeyPressed(eKeyLeftArrow) && !IsKeyPressed(eKeyRightArrow))


The other way is to separate keypress detection and movement:

-detect pressed keys
-determine actual movement depending on current movement & pressed keys
-move character

Snake

Thanks, Khris, but that didn't work. It does nothing different.

I think the problem may lie where I have the animate code?

I am going to try again and rewrite the code...
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Sephiroth

#3
I think it may come from the running variable, it will only be reset to 0 when you release all the keys, so unless you do that, the if (running != 0) will prevent you from executing the player.animate code.

Maybe you don't even need to check if(running!=0)

-if he's running then continue running  in new direction
-if he's not running then start running in new direction

Both options should lead to player animating in new direction right?


Snake

Thank you for suggesting that the problem may have been coming from the if(running!=0) variable check, Sephiroth!

I worked with it and made a couple new variables to check directions, split the code up into a couple different sections and etc, ... and it now works beautifully.

Thank you once again for opening my eyes to that piece of code...

Now it's on to finishing jump and duck! I had them working flawlessly the other night, so hopefully I can remember how I did it without wasting too much time (I had wiped the entire thing clean and started from scratch previous to the code given on my first post).
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Sephiroth

Glad I could help, I like this kind of gameplay more than point & click, so I wish you luck with it, anyway you can post more code if you want us to have a look at it.

SMF spam blocked by CleanTalk