GMS 2 [Solved] Player moving super fast when holding down two keys at once

D

DaPonyMon

Guest
Hi, I'm new to gamemaker studio, and this is my first post on the forums.

The problem I have been encountering uses the following code:
Code:
//Movement
if (keyboard_check(ord("W")))
{
    instance_change(obj_player_up,false);
    obj_player_idle.y -= global.playerspeed;
}
if (keyboard_check(ord("S")))
{
    instance_change(obj_player_down,false);
    obj_player_idle.y += global.playerspeed;
}
if (keyboard_check(ord("A")))
{
    instance_change(obj_player_left,false);
    obj_player_idle.x -= global.playerspeed;
}
if (keyboard_check(ord("D")))
{
    instance_change(obj_player_right,false);
    obj_player_idle.x += global.playerspeed;
}

if (keyboard_check_released(ord("W")) && keyboard_check_released(ord("S")) && keyboard_check_released(ord("A")) && keyboard_check_released(ord("D")) or (keyboard_check(ord("W")) && keyboard_check(ord("S"))) or (keyboard_check(ord("A")) && keyboard_check(ord("D"))))
{
    instance_change(obj_player_idle,false);
}
This controls movement, and is located in the step event along with my sprinting code, and the step event is stored in the idle player which is a parent object of player right, left, up and down. The problem is, when I press W and D, W and A, S and A or S and D, the player just goes super speedy and ignores all collisions and barriers around the room. Also, when looking on debugger, I noticed playerspeed was not changing, and the FPS dropped from around 2300 to around 3, sometimes even 0.
Another problem I get is that whenever I press A and D or W and S, the player stands in a single spot and won't allow me to do anything.

Any help is greatly appreciated, as I have been trying to crack this code for around a week now with no luck, and since I'm a beginner programmer I can't really do much. Any tips on how to format or design my code better would also be appreciated.
 
A

Alex_Beach

Guest
So first off, there's no reason to run at 2300 fps, that's just putting tons of strain on your system. The debugger is not showing speed because you aren't using speed. Your movement code directly moves the object's x and y positions, which is not considered speed. I've never set up movement like this, but since you're changing objects so much (when you hold 2 keys, your code will make them change objects 2 separate times each frame, thus running a new step code on the new object as well), they are probably stacking their speeds this way. Instead of making separate objects for your different walking animations, just use: sprite_index=SpriteName. This will change your current sprite of your object. That way you will only need 1 object to control movement.
 
D

DaPonyMon

Guest
So first off, there's no reason to run at 2300 fps, that's just putting tons of strain on your system. The debugger is not showing speed because you aren't using speed. Your movement code directly moves the object's x and y positions, which is not considered speed. I've never set up movement like this, but since you're changing objects so much (when you hold 2 keys, your code will make them change objects 2 separate times each frame, thus running a new step code on the new object as well), they are probably stacking their speeds this way. Instead of making separate objects for your different walking animations, just use: sprite_index=SpriteName. This will change your current sprite of your object. That way you will only need 1 object to control movement.
Thanks for the reply! I've implemented it and it works smoothly now.
 
Top