Character running - SOLVED

Started by Kinoko, Thu 17/06/2004 06:58:32

Previous topic - Next topic

Kinoko

I've implemented the script that allows action-style movement into my script and it works perfectly. My character walks when direction keys are held down with no problems.

Now, though, I'd like to be able to have the character run when I hold down the 'A' button. Basically, if I hold down 'A', and then press a direction key. I want him to run, OR if I am holding a direction key (ie walking) and while doing that, I hold down 'A', I want him to start running from the walk.

I realise you cannot change a characters speed while he is moving, so I was trying to achieve this by having him stop for a fraction of a second and then start running (a little jerkiness is a small sacrifice, I think). So, I've been mussing around with the code for awhile and I can get him to run when the 'A' button is pressed, but only after it has been let go. If I walk and then hold the A button, EGO stops until I release 'A' and then runs.

Also, if I let go of the direction button after that (stopping the character) and then hit a direction button again, he'll still be in run mode. I'm not sure exactly where I should reset his speed back to normal.

Here's the entire repeatedly execute script in my global script:
Quote
function repeatedly_execute() {
Ã,  // put anything you want to happen every game cycle here

// --- keyboard control ---
int CharId, Direction, dx, dy;
// neue Richtung ermitteln
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;
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;
// 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);
}
}

Ã,  if (IsKeyPressed(65)==1) { //RUNNING - 'A' button held down
Ã,  Ã, 
Ã,  Ã,  Ã, if (Direction == DIR_UP_LEFT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = -DIR_DISTANCE; dy = -DIR_DISTANCE; }
Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_UP) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = 0; dy = -DIR_DISTANCE; } // 8 Up arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_UP_RIGHT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = DIR_DISTANCE; dy = -DIR_DISTANCE; } // 9 PgUp (numeric pad)
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_LEFT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = -DIR_DISTANCE; dy = 0; } // 4 Left arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_RIGHT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = DIR_DISTANCE; dy = 0; } // 6 Right arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_DOWN_LEFT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = -DIR_DISTANCE; dy = DIR_DISTANCE; } // 1 End (numeric pad)
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_DOWN) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = 0; dy = DIR_DISTANCE; } // 2 Down arrow
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, else if (Direction == DIR_DOWN_RIGHT) {
Ã,  Ã,  Ã, StopMoving (EGO);
Ã,  Ã,  Ã, SetCharacterSpeed (EGO, 10);
Ã,  Ã,  Ã, dx = DIR_DISTANCE; dy = DIR_DISTANCE; } // 3 PgDn (numeric pad)
Ã,  Ã,  Ã, 
Ã,  Ã,  Ã, MoveCharacterStraight (EGO, character [EGO].x + dx, character [EGO].y + dy);
Ã,  Ã,  Ã, }
Ã,  Ã, 
}

I'm sure it's as messy as hell but I've been steadily making progress up until now. I can't really think of how to get around this though I'm sure it's obvious. Can anyone help?

Scorpiorus

#1
Something like that should make the trick, but let us know how (and if) it works:

int isRunning = 0;

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

// --- keyboard control ---
int CharId, Direction, dx, dy;
// neue Richtung ermitteln
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;
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;


if (IsKeyPressed(65)==1) { //RUNNING - 'A' button held down

   if (isRunning==0) {
      StopMoving(GetPlayerCharacter());
      SetCharacterSpeed(GetPlayerCharacter(), RUN_SPEED);
      isRunning = 1;
      Direction = DIR_STOP;
   }

} else {

   if (isRunning==1) {
      StopMoving(GetPlayerCharacter());
      SetCharacterSpeed(GetPlayerCharacter(), WALK_SPEED);
      isRunning = 0;
      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);
}
}

}

EDIT: to add StopMoving(GetPlayerCharacter()).

Kinoko

#2
Pending...

Scorpiorus

I've tested the script and just wanted to mention that there is also the StopMoving(GetPlayerCharacter()); function should be invoked before changing the speed. I updated the previous post to reflect that fact. :)

Kinoko

I just managed to test it (had that other typing problem before stopping me from testing this one) and was about to say the exact same thing ^_^

It works GREAT now, thanks heaps! Scorpiorus, you're aaaaaaallright. ^_-

Kinoko

*groan* I have to dredge this one up again.

Everything works fine except for running up/left. Walking up/left works, but if I try to do it while running, it won't (it was just continue to go either straight left or up).

Code: ags

// --- keyboard control ---
int CharId, Direction, dx, dy;
// neue Richtung ermitteln
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;
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;

if (IsKeyPressed(65)==1) { //RUNNING - 'A' button held down

Ã,  Ã, if (isRunning==0) {
Ã,  Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  Ã,  SetCharacterSpeed(GetPlayerCharacter(), 12);
Ã,  Ã,  Ã,  isRunning = 1;
Ã,  Ã,  Ã,  Direction = DIR_STOP;
Ã,  Ã, }

} else {

Ã,  Ã, if (isRunning==1) {
Ã,  Ã,  Ã,  StopMoving(GetPlayerCharacter());
Ã,  Ã,  Ã,  SetCharacterSpeed(GetPlayerCharacter(), 6);
Ã,  Ã,  Ã,  isRunning = 0;
Ã,  Ã,  Ã,  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);
}
}

Gilbert

#6
Hmmm did it not work in all of the below key combinations, or just one of them? (I think you understand what they mean)

1) A+L+U
2) A+LU (ie. Home)

If it only failed on 1), it's possible that it's a hardware limitation (in some keyboards it may not be able to recognized the whole combination if 3 or more keys are pressed at the same time).

--EDIT--
I think I misread something, it's seems that the 'A' key toggle between walk and run mode, not running while it's held. Forget above lines then.

Kinoko

Ah yeah, the A key needs to be held to run, but holding A doesn't make the character run. A along with a directional key need to be held for the character to run.

Gilbert

Actually I didn't read the whole thing fully, but I have some questions on it, wouldn't the current arrangement of the "if (IsKeyPressed(65)==1) " part made the character always stops when "A" was being pressed, even if the direction keys are pressed with it? Maybe I had misread something.

Scorpiorus

Kinoko: Hmm, I gave it a test and all seem to work fine. I think Gilbert is right, it may be the keyboard can't handle a combination of three certain keys that are being held down at the same time. Try setting another key instead of 'A' or replacing the whole if (IsKeyPressed(65)==1) with if(IsButtonDown(RIGHT)==1) to test if works with a right mouse button being held down.

Gilbert: There is a isRunning variable that prevents the speed changing script to run multiple times.

Kinoko

Hmm, seems you're right. I didn't realise the keyboard worked that way before now. My downstairs keyboard is fine but my other computer has the problem. It won't work when I push 'A', left arrow and up arrow at the same time. Using the numberic keypad is fine though. Thanks both of you, anyway.

Hollister Man

Sorry to post in a solved thread, but is either the ctrl or alt button used?  These only activate the isbuttonpressed action, and are frequently wired differently so as to be used for exactly this kind of action (well, sorta)
That's like looking through a microscope at a bacterial culture and seeing a THOUSAND DANCING HAMSTERS!

Your whole planet is gonna blow up!  Your whole DAMN planet...

Kinoko

I have to pull this old thread up once more. It all works fine, but I've noticed that if a character is walking and another key is pressed (say to attack or something), the character will stop and do that new action (which is great) but then won't continue walking.

Basically, instead of the game engine detecting whenever the direction keys are held down (and another action isn't being performed), it's only detecting when the direction keys are actually pushed. If I'm walking, do another action, but am still holding down a direction key after the action has completed, I'd like the character to continue walking. As it is, it feels very jerky and is rather annoying to constantly have to repush direction keys in battle.

Pumaman

This is probably due to the PrevDirection variable you have, that stops it restarting the move if it's in the same direction. Perhaps you could try this:

REPLACE:

if (PrevDirection != Direction)

WITH:

if ((PrevDirection != Direction) || (character[EGO].walking == 0))

Kinoko

Thanks heaps, that worked perfectly.

R4L

I just want to know. Do any functions have to be imported?

Kinoko

You should just have to stick that final code in your repeatedly_execute section and voila. So, no.

prowler

i know this has nothing to do with your problem, but i was thinking, maybe instead of A (65 is it?) you could declare an int which the player would have the possibility to redefine?

i hate games where you can't redefine your keys; especially when there's a lot of them...

just a suggestion  :)

Kinoko

No problem there, I am considering that for the future. For now though, I'm just getting my basics worked out.

SMF spam blocked by CleanTalk