Keyboard Movement -- But I've researched!

Started by Captain Lexington, Wed 23/04/2008 05:02:10

Previous topic - Next topic

Captain Lexington

Okay, so I looked this all up in the archives and pretty much anyone who asked the question got deferred to the Keyboard Movement module, but I had on this computer before the wipe a very few lines of code that managed to move the character around with the arrow keys. I had gotten it off the wiki, it seems to be gone, and I don't remember it at all now.

So.

My question is how can I use cChar.Walk to move a character indefinitely in a given direction?

I'm pretty sure that's how that old code used to go.

I've the got the IsKeyPressed and stuff down, but it just isn't cutting it right now.

Code: ags
  if (IsKeyPressed(372)==1) cPluto.Walk (cPluto.x,  cPluto.y);
  if (IsKeyPressed(377)==1) cPluto.Walk (cPluto.x,  cPluto.y);
  if (IsKeyPressed(375)==1) cPluto.Walk (cPluto.x, cPluto.y);
  if (IsKeyPressed(380)==1) cPluto.Walk (cPluto.x, cPluto.y);


I know obviously this code doesn't move him becuase each command tells him to go where he is, this just what I have in my source right now.

I know this has worked before!

monkey0506

I believe one of the easiest methods would simply be using an offset of 1000 or so. The Character would seemingly walk "forever" until told to stop or until the edge of the walkable area was reached.

Captain Lexington

Well I've figured out how to fix all of the problems...

Code: ags
if (IsKeyPressed(372)==1) {
    cPluto.y--;
     if (IsKeyPressed(377)==1){
      cPluto.FaceLocation (cPluto.x+1,  cPluto.y);
    }
    else if (IsKeyPressed(375)==1) {
      cPluto.FaceLocation (cPluto.x-1,  cPluto.y);
    }
    else if (IsKeyPressed(375/377)==0){
      cPluto.FaceLocation (cPluto.x,  cPluto.y-1);
    }
  }
  if (IsKeyPressed(377)==1) {
    cPluto.x++;
    cPluto.FaceLocation(cPluto.x+1,  cPluto.y);
    }
   if (IsKeyPressed(375)==1) {
    cPluto.x--;
    cPluto.FaceLocation(cPluto.x-1,  cPluto.y);
    }
   if (IsKeyPressed(380)==1) {
    cPluto.y++;
    if (IsKeyPressed(377)==1){
      cPluto.FaceLocation (cPluto.x+1,  cPluto.y);
    }
    else if (IsKeyPressed(375)==1) {
      cPluto.FaceLocation (cPluto.x-1,  cPluto.y);
    }
    else if (IsKeyPressed(375/377)==0){
      cPluto.FaceLocation (cPluto.x,  cPluto.y+1);
    }
  }


Do tell me if there's a less clumsy method.

Khris

I'm surprised this works.
Just one thing: IsKeyPressed(375/377) is equivalent to IsKeyPressed(0).

Pumaman

The command you're probably looking for is WalkStraight, which simply tells the character to start walking in a particular direction, and not try to pathfind.

Captain Lexington

#5
@monkey: My game will take place in a scrolling room of indeterminate size, and I'm not sure I want to attack this problem again after that's been figured out.

@KhrisMUC: Thanks for the tip, but I just realized something: My current character does not (and most likely will not) have a walkcycle animation, and quick test shows that if indeed he had one the Char.x/y functions don't run it. I tried replacing the cPluto.FaceLocation with cPluto.Animate but it didn't work out quite as planned.

@Punaman: I tried the WalkStraight because the documentation said it was good for keyboard movement, but two problems arose:

a) the original problem of only being able to move a finite distance, addressed above,

and

b) the character would move only slightly and then stop, resuming motion only after the key was released.


As it is, the code posted before suits me fine for this project, but won't work for anyone else or for any future game projects of my own, so I'd like to get it settled now.

EDIT: Bits were changed.


EDIT2: I've changed from a keyboard interface to a parser-driven "go to [thing]" type deal (with of course moving to a place automatically if you try to interact with it). So basically this question is irrelevant, although I'd really like to know how that disappeared code worked, and would maybe switch back if I had it. If any of you want to figure this out for purely academic reasons, or just don't want me to make a game with such an interface, feel free to continue this thread to figure it out. If not, I'm content with writing it off to fate and locking the thread. Cheers.

SMF spam blocked by CleanTalk