Hi there.
The game I am doing now requires the player character to walk and run.
I've used Rui and Strazer's Keyboard Movement Module to make my character able to walk, but now I can't make him run.
I try using this code at On_key_press function:
while (IsKeyPressed (403) == 1) {
player.AnimationSpeed = (player.animspeed + 2);
}
So when I press Shift and left or right, game crashes.
What am I doing wrong?
What error message do you get?
Anyway, there're a couple of possible things:
Firstly, you seem to have mixed up two styles of coding, as you have both player.AnimationSpeed and player.animspeed - pick one and use it consistantly (if you're using V2.7+, player.AnimationSpeed). I don't think this will have caused the crash, but it's a better habit to be in.
Also, I don't think you'd use IsKeyPressed() in on_key_press like that, it'd probably be better in repeatedly_execute.
However, if you do that, the speed will keep increasing as long as the key is held, which could get a bit silly. Perhaps this would work better:
// top of Module script
bool running;
// in rep_ex
if (IsKeyPressed(403)) {
if (running == false) {
player.AnimationSpeed += 2;
running == true;
}
}
else if (running == true) { // 403 NOT pressed
player.AnimationSpeed = 4; // Or whatever the normal 'Walk' speed is
running = false;
}
You could also use player.AnimationSpeed, and set a maximum value, e.g.
if (IsKeyPressed(403)) {
if (player.AnimationSpeed < 10) {
player.AnimationSpeed += 2;
}
}
else player.AnimationSpeed = 4; // 403 NOT pressed
Finally - you might want to change the characters Walking speed as well as / instead of their Animation speed.
Cool, now everything works. Thank you