Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: Jam Torkberg on Wed 03/09/2003 23:25:20

Title: Controlling movement with keyboard
Post by: Jam Torkberg on Wed 03/09/2003 23:25:20
Edit by strazer: Superseded by the KeyboardMovement script module (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=22724).

--

Telling the character to move about with simple keystrokes is easy enough.  Tapping left, for example, can be used to make the character walk as far to the left as possible, until he hits the edge of a walkable area.

But what if one wants to use the keyboard in a more digital sense, moving the character if the button is held down and having the character stop if the button is released.

I've tried several methods to accomplish this, but to no avail.  In all cases, holding down the button causes the character to stay in place and jerk about.  I can see why this would happen, I just can't see how to fix it.

Thoughts?
Title: Re:Controlling movement with keyboard
Post by: Ishmael on Thu 04/09/2003 11:23:54
I noticed this too... Every time a nwe MoveCharacter is ran, the characteris stopped. I see MoveCharacterPath does the same.... I see it logical that MoveCharacterPath should work, since you can us it to add waypoints to the characters path. If you use it in cutscene or so, does it stop the character and start it moving to the new point, without finishing the current set path?
Title: Re:Controlling movement with keyboard
Post by: Scorpiorus on Thu 04/09/2003 16:27:34
Yes, every MoveCharacter() call first stops the previous movement. You may find it useful to use a-v-o's movement script: (EDIT: See Hollister Man's reply lower down for the complete script)

-Cheers
Title: Re:Controlling movement with keyboard
Post by: Jam Torkberg on Fri 05/09/2003 06:39:58
Well, I've added a-v-o's code and it works perfectly.  But I'm a picky bastard, and have to take things one step further.

My thought is to have things such that if you the player presses two arrow keys at the same time the character will move diagonally.  For example, is one presses the up and right arrow keys at the same time the charater move up and to the right (fuh).

Here is an expert from a-v-o's code
Quoteif ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0)) Direction = DIR_UP_LEFT;    // 7 Home (numeric pad)
   else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP;         // 8 Up arrow
   else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0)) Direction = DIR_UP_RIGHT;   // 9 PgUp (numeric pad)
   else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT;       // 4 Left arrow
   else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP;       // 5 Stop (numeric pad)
   else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT;      // 6 Right arrow
   else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0)) Direction = DIR_DOWN_LEFT;  // 1 End (numeric pad)
   else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN;       // 2 Down arrow
   else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0)) Direction = DIR_DOWN_RIGHT; // 3 PgDn (numeric pad)
   else Direction = DIR_STOP;


And here is the same, with my additions:
Quoteif ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_UP_LEFT;
   else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP;        
   else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (377) > 0))) Direction = DIR_UP_RIGHT;  
   else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT;      
   else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP;      
   else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT;      
   else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_DOWN_LEFT;
   else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN;      
   else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (377) > 0))) Direction =DIR_DOWN_RIGHT;
else Direction = DIR_STOP;

So here's the problem.  The above works, but only for up and left.  AND!!! If I were to switch, say, the code for up-left with that of up-right, than up right would be the only combination that works.  In other words, it is only the first bit of code.  I don't know if all the ||s and &&s are fouling things up, or the “else if”s, but something is going on.

It is not the end of the world if these never works, but I am mostly just curious as to why it only half works, for reference in later coding escapades.  Any thoughts?
Title: Re:Controlling movement with keyboard
Post by: Scorpiorus on Fri 05/09/2003 13:31:39
Quoteif ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_UP_LEFT;
else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP;
else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (377) > 0))) Direction = DIR_UP_RIGHT;
else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT;
else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP;
else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT;
else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_DOWN_LEFT;
else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN;
else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (377) > 0))) Direction =DIR_DOWN_RIGHT;
else Direction = DIR_STOP;
When you press UP+RIGHT AGS first checks else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP; line and since the UP key (372) is pressed it goes UP. The else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (377) > 0))) Direction = DIR_UP_RIGHT; code is not even get checked.

So you need another IFs order:

First diagonals:
Quote
if ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_UP_LEFT;
else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0) || ((IsKeyPressed (372) > 0) && (IsKeyPressed (377) > 0))) Direction = DIR_UP_RIGHT;
else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (375) > 0))) Direction = DIR_DOWN_LEFT;
else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0) || ((IsKeyPressed (380) > 0) && (IsKeyPressed (377) > 0))) Direction =DIR_DOWN_RIGHT;

The rest:
Quote
else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP;
else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT;
else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP;
else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT;
else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN;
else Direction = DIR_STOP;

How does it go?

-Cheers
Title: Re:Controlling movement with keyboard
Post by: Jam Torkberg on Fri 05/09/2003 17:44:25
Ah, I see what you are saying, and it had not even occurred to me.  It is all so clear now.  You see, the trouble is that, having learned to use this stuff in bits and pieces, some in the manual, some on the web, I have no solid beginning to ending understanding of the language.

Anyway, none of that is really important.  What is important is that you fellows are coding fiends and the whole system works now.  Many thousands of thanks to everyone, and let us hope the subject is closed.
Title: Re:Controlling movement with keyboard
Post by: Teh Crabe on Fri 05/09/2003 22:46:19
I played with the script for a bit, but I'm unsure of its usefulness without being paired with a collision detection system.
Title: Re:Controlling movement with keyboard
Post by: Jam Torkberg on Fri 05/09/2003 23:32:13
Hmm, that may be a good point.  I certainly hope I don't build a game around it have have it implode due to the character flying abou thte screen.

But, for the moment, a simple walkable area seems enough to keep the sprite based persona from falling off the edhe of the screen.
Title: Re:Controlling movement with keyboard
Post by: Teh Crabe on Sat 06/09/2003 00:36:37
Well, what I mean is you need some kind of collision detection for interacting with things.  If you still need to point and click with a mouse to interact, using the keyboard to walk is a waste.  I'm assuming you would have an action key or keys which you would press.  If the character were standing over a hotspot when the key was pressed, your interaction would occur.  Right now, hotspots detect the mouse cursor, not the character.

I'm new to this too, and could be wrong, but that's the impression I get.  You need a way to track when the character is within a hotspot.
Title: Re:Controlling movement with keyboard
Post by: Jam Torkberg on Sat 06/09/2003 00:55:30
Regions, my friend!  Regions make the world a fine and dandy place.

At least, I imagine so in my mind.  I have been using them without incident thus far.  But that does not mean things will not go fubar at the drop of a hat.
Title: Re:Controlling movement with keyboard
Post by: Hollister Man on Thu 18/12/2003 19:18:36
Thanks to AVO, I got the script.  Here it is for all to enjoy:

The player can use keys 1-9 on the numeric keypad or the corresponding arrow keys to control the movement of the player character (pc). The pc moves as long as the key is pressed and stops moving when the key is released.

At the top of the Main global script:

// ------ keyboard control ------
#define DIR_DISTANCE 10000
#define DIR_DOWN_LEFT 1
#define DIR_DOWN 2
#define DIR_DOWN_RIGHT 3
#define DIR_LEFT 4
#define DIR_STOP 5
#define DIR_RIGHT 6
#define DIR_UP_LEFT 7
#define DIR_UP 8
#define DIR_UP_RIGHT 9
int PrevDirection;


Within the game_start() function:

// ------ keyboard control ------
PrevDirection = DIR_STOP;


Within the repeatedly_execute() function:

// --- keyboard control ---
int CharId, Direction, dx, dy;
// neue Richtung ermitteln
if ((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0)) Direction = DIR_UP_LEFT; // 7 Home (numeric pad)
else if ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP; // 8 Up arrow
else if ((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0)) Direction = DIR_UP_RIGHT; // 9 PgUp (numeric pad)
else if ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT; // 4 Left arrow
else if ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP; // 5 Stop (numeric pad)
else if ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT; // 6 Right arrow
else if ((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0)) Direction = DIR_DOWN_LEFT; // 1 End (numeric pad)
else if ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN; // 2 Down arrow
else if ((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0)) Direction = DIR_DOWN_RIGHT; // 3 PgDn (numeric pad)
else Direction = DIR_STOP;
// Vergleich mit aktueller Richtung
if (PrevDirection != Direction)
{
PrevDirection = Direction;
CharId = GetPlayerCharacter ();
if (Direction == DIR_STOP) { StopMoving (CharId); } // 5 Stop (numeric pad)
else
{
if (Direction == DIR_UP_LEFT) { dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; } // 7 Home (numeric pad)
else if (Direction == DIR_UP) { dx = 0; dy = -DIR_DISTANCE; } // 8 Up arrow
else if (Direction == DIR_UP_RIGHT) { dx = DIR_DISTANCE; dy = -DIR_DISTANCE; } // 9 PgUp (numeric pad)
else if (Direction == DIR_LEFT) { dx = -DIR_DISTANCE; dy = 0; } // 4 Left arrow
else if (Direction == DIR_RIGHT) { dx = DIR_DISTANCE; dy = 0; } // 6 Right arrow
else if (Direction == DIR_DOWN_LEFT) { dx = -DIR_DISTANCE; dy = DIR_DISTANCE; } // 1 End (numeric pad)
else if (Direction == DIR_DOWN) { dx = 0; dy = DIR_DISTANCE; } // 2 Down arrow
else if (Direction == DIR_DOWN_RIGHT) { dx = DIR_DISTANCE; dy = DIR_DISTANCE; } // 3 PgDn (numeric pad)
MoveCharacterStraight (CharId, character [CharId].x + dx, character [CharId].y + dy);
}
}


I think there could  be some optimization, but this is what AVO posted in the forums earlier.   Whatch out for non-english comments.
Title: Re:Controlling movement with keyboard
Post by: Barcik on Mon 05/01/2004 14:20:23
Duzz has released the source of his AGS FIFA game, and it may prove to be very helpful to you.

Download here. (http://www.users.bigpond.net.au/theclampdown/FIFASource.exe)
Title: Re:Controlling movement with keyboard
Post by: magintz on Thu 15/01/2004 09:36:36
Or as another option to you young mites, you can try the classic adventure style walking, where if you push the button once they move, and if you push it again they stop. All the scripting needed for this is within my Text Parser Template.
Title: Re:Controlling movement with keyboard
Post by: rtf on Sun 08/02/2004 03:05:12
http://www.sylpher.com/TutScriptsWalk.html
Title: Re: Controlling movement with keyboard
Post by: SilverWizard_OTF on Mon 05/07/2004 19:51:17
Here is a script code that Barbarian gave to me, when i had the same question.


if (keycode==372) { // up arrow
    MoveCharacterStraight(EGO, player.x, player.y-200);
    while ((IsKeyPressed(372)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}

if (keycode==375) { // left arrow
    MoveCharacterStraight(EGO, player.x-200, player.y);
    while ((IsKeyPressed(375)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}

if (keycode==377) { // right arrow
    MoveCharacterStraight(EGO, player.x+200, player.y);
    while ((IsKeyPressed(377)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}

if (keycode==380) { // down arrow
    MoveCharacterStraight(EGO, player.x, player.y+200);
    while ((IsKeyPressed(380)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}


Hope it helps.  Anyway, it helped me!
Title: Re: Controlling movement with keyboard
Post by: Hollister Man on Sun 11/07/2004 22:27:21
I made a minor change to AVO's script, so that a combination of 4-way arrows will allow you to go diagonal.


This should all go in the Repeatedly_execute function.


int CharId, Direction, dx, dy;
     if (((IsKeyPressed (371) > 0) || (IsKeyPressed (55) > 0)) || (IsKeyPressed(372)&&IsKeyPressed(375))) Direction = DIR_UP_LEFT; // 7 Home (numeric pad)
else if (((IsKeyPressed (373) > 0) || (IsKeyPressed (57) > 0)) || (IsKeyPressed(372)&&IsKeyPressed(377))) Direction = DIR_UP_RIGHT; // 9 PgUp (numeric pad)
else if (((IsKeyPressed (379) > 0) || (IsKeyPressed (49) > 0)) || (IsKeyPressed(380)&&IsKeyPressed(375))) Direction = DIR_DOWN_LEFT; // 1 End (numeric pad)
else if (((IsKeyPressed (381) > 0) || (IsKeyPressed (51) > 0)) || (IsKeyPressed(380)&&IsKeyPressed(377))) Direction = DIR_DOWN_RIGHT; // 3 PgDn (numeric pad)
else if  ((IsKeyPressed (372) > 0) || (IsKeyPressed (56) > 0)) Direction = DIR_UP; // 8 Up arrow
else if  ((IsKeyPressed (375) > 0) || (IsKeyPressed (52) > 0)) Direction = DIR_LEFT; // 4 Left arrow
else if  ((IsKeyPressed (376) > 0) || (IsKeyPressed (53) > 0)) Direction = DIR_STOP; // 5 Stop (numeric pad)
else if  ((IsKeyPressed (377) > 0) || (IsKeyPressed (54) > 0)) Direction = DIR_RIGHT; // 6 Right arrow
else if  ((IsKeyPressed (380) > 0) || (IsKeyPressed (50) > 0)) Direction = DIR_DOWN; // 2 Down arrow
else Direction = DIR_STOP;

if (PrevDirection != Direction)
{
PrevDirection = Direction;
CharId = GetPlayerCharacter ();
if (Direction == DIR_STOP) { StopMoving (CharId); } // 5 Stop (numeric pad)
else
{
if (Direction == DIR_UP_LEFT) { dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; } // 7 Home (numeric pad)
else if (Direction == DIR_UP) { dx = 0; dy = -DIR_DISTANCE; } // 8 Up arrow
else if (Direction == DIR_UP_RIGHT) { dx = DIR_DISTANCE; dy = -DIR_DISTANCE; } // 9 PgUp (numeric pad)
else if (Direction == DIR_LEFT) { dx = -DIR_DISTANCE; dy = 0; } // 4 Left arrow
else if (Direction == DIR_RIGHT) { dx = DIR_DISTANCE; dy = 0; } // 6 Right arrow
else if (Direction == DIR_DOWN_LEFT) { dx = -DIR_DISTANCE; dy = DIR_DISTANCE; } // 1 End (numeric pad)
else if (Direction == DIR_DOWN) { dx = 0; dy = DIR_DISTANCE; } // 2 Down arrow
else if (Direction == DIR_DOWN_RIGHT) { dx = DIR_DISTANCE; dy = DIR_DISTANCE; } // 3 PgDn (numeric pad)
MoveCharacterStraight (CharId, character [CharId].x + dx, character [CharId].y + dy);
}
}

Title: Re: Controlling movement with keyboard
Post by: jetxl on Tue 31/08/2004 09:29:56
My version:
http://www.2dadventure.com/ags/ArrowWalk.zip


int cleft;Ã,  //flags to see if button is still pressed
int cright;
int cup;
int cdown;



Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  //the arrowkey code . copy this in global script repeatedly execute
Ã,  if (IsKeyPressed(375)==1 && cleft==0) {
Ã,  Ã,  cleft=1;
Ã,  Ã,  MoveCharacterStraight(GetPlayerCharacter(),-1000,character[GetPlayerCharacter()].y);
Ã,  }
Ã,  if (IsKeyPressed(375)==0 && cleft==1){
Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  cleft=0;
Ã,  }

Ã,  if (IsKeyPressed(377)==1 && cright==0) {
Ã,  Ã,  cright=1;
Ã,  Ã,  MoveCharacterStraight(GetPlayerCharacter(),1000,character[GetPlayerCharacter()].y);
Ã,  }
Ã,  if (IsKeyPressed(377)==0 && cright==1){
Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  cright=0;
Ã,  }

Ã,  if (IsKeyPressed(372)==1 && cup==0) {
Ã,  Ã,  cup=1;
Ã,  Ã,  MoveCharacterStraight(GetPlayerCharacter(),character[GetPlayerCharacter()].x,-1000);
Ã,  }
Ã,  if (IsKeyPressed(372)==0 && cup==1){
Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  cup=0;
Ã,  }

Ã,  if (IsKeyPressed(380)==1 && cdown==0) {
Ã,  Ã,  cdown=1;
Ã,  Ã,  MoveCharacterStraight(GetPlayerCharacter(),character[GetPlayerCharacter()].x,1000);
Ã,  }
Ã,  if (IsKeyPressed(380)==0 && cdown==1){
Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  cdown=0;
Ã,  }
Title: Re: Controlling movement with keyboard
Post by: TerranRich on Tue 31/08/2004 19:11:47
Shouldn't pressing one arrow also stop the other movements as well? For example, if the charactrer is walking up and the player presses right, this code will cause him/her to walk up-right. Or am I wrong?
Title: Re: Controlling movement with keyboard
Post by: jetxl on Thu 02/09/2004 11:19:55
Nope. If the player presses up and then right, the character will go right. (because I didn't use "else")

This is a small and simple piece of code for 3 reasons.
1: I thought that AGS could only handle one key press. So I never tried doing diagnal. (I don't use those walking loops anyway)
2: I never look in the tech arcive and didn't knew there was  a "movement with keyboard" topic. I made this before there was a tech archive.
3: People that can't program very well (like me) won't use complexed code since they don't understand how it works. And when they do (like me) they can make their own code anyway.