I am trying to write a script that when you hold shift the player will run and if you release it the player walks.
This is what I have written:
function repeatedly_execute() {
if (IsKeyPressed(404) == 1) {
cEgo3D.LoadDefaultAnimation(eNormal, 30, 40, "data\\witch.ms3d");
if (cEgo.WalkSpeedX == 1) {
cEgo.SetWalkSpeed(3,3);
}
}
else if (IsKeyPressed(404) == 0) {
cEgo3D.LoadDefaultAnimation(eNormal, 1, 10, "data\\witch.ms3d");
if (cEgo.WalkSpeedX == 3) {
cEgo.SetWalkSpeed(1, 1);
}
}
}
The only problem is that if I release or press shift while the player is moving, the game crashes. I have tried putting the command "StopCharacterMoving" before set walk speed but then you have to make the character walk twice and it dosen't work very smoothly. Is there a way to get this to work right?
You can't change the walk spped of a character while he's moving. Although, you can change the animation speed. You might try:
int anim = player.AnimationSpeed; // Sets integer "anim" to current Animation Speed
function repeatedly_execute(){
if (IsKeyPressed(404)){
player.AnimationSpeed = (anim - 1); // Decreases Animation Speed by 1
}
if (!IsKeyPressed(404)){
player.AnimationSpeed = anim; // Recalls default Animation Speed
}
}
The anim speed works backward, because it's actually the animation delay time. It kinda make him go in fast-forward, but so does just increasing the walk speed. Mind you this only works if your AnimationSpeed is >1 to start with, otherwise it will go to zero, and he'll stop moving.
EDIT: Not really sure if (!IsKeyPressed(404)) actually works. You might have to stick with (IsKeyPressed(404) == 0) I've been doing more Javascript than AGS lately.
As an experiment you could also click Don't automatically move in walk mode and have a custom walk function that stores where he is moving to. Then when shift is pressed it does a stopMoving, changes the walk speed, then continues the move until he reaches his desination.
the thing is there will most likely be a noticable pause when he stops, changes speed, then recalculates the waypoints to his destination.
Like I said an experiment, Cib's solution would probably work better.
I tried both of those Ideas and a few more and nothing seems to work. Ive decided It would be a whole lot easier for me and the player to only have to push the button once instead of holding it down.
Ive got a much simpler script now.
function repeatedly_execute() {
if(IsKeyPressed(404) == 1) {
if(cEgo.WalkSpeedX == 1) {
cEgo.StopMoving();
cEgo.SetWalkSpeed(3, 3);
cEgo3D.LoadDefaultAnimation(eNormal, 30, 40, "data\\witch.ms3d");
}
else {
cEgo.StopMoving();
cEgo.SetWalkSpeed(1, 1);
cEgo3D.LoadDefaultAnimation(eNormal, 1, 10, "data\\witch.ms3d");
}
}
}