Novice Programmer, movement question

Hello, I'm a brand new programmer who's been doing his best to learn the basics of code logic to make my platformer game. I've already got basic movement implemented, but am having trouble figuring out how to instantly stop movement/acceleration in one direction when both arrow keys are being pressed at the same time. Additionally, I'm unable to get the "right" movement to override the "left" (just the other way around). Could anyone help me with these issues? Currently, the final "do" statement is intended to stop movement while both movement keys are pressed but I apparently haven't resolved it correctly and it crashes my project.

Here's the relevant code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);

hsp = 0;
vsp = 0;
grv = 0.4;

aSpeed = 0.3;
dSpeed = 0.9;
mSpeed = 6;

Step:
if(key_left){
hsp = Approach(hsp,-mSpeed,aSpeed);
}else if(key_right){
hsp = Approach(hsp,mSpeed,aSpeed);
}else{
hsp = Approach(hsp,0,dSpeed);
}

if(key_left)&&(key_right){
do {mSpeed = 0
}
until(key_left)&&(!key_right)||(key_right)&&(!key_left)
}

I'm almost positive it could also be more optimized so I'm open to constructive criticism. I'm very untrained but eager to learn.
Thanks!
 
Last edited:

Pixel-Team

Master of Pixel-Fu
You definitely would not need the "Until" line. Technically, you could set hsp to 0 in your step event before you handle input, and if there is no input, it would be zero. Then you wouldn't even need the following:
GML:
if(key_left)&&(key_right){
do {mSpeed = 0
}
until(key_left)&&(!key_right)||(key_right)&&(!key_left)
}
but since you specifically want to stop the player when both left and right is pressed, then you could do this:

GML:
if(key_left && key_right){
hsp = 0;
}
 

Nidoking

Member
do {mSpeed = 0
}
until(key_left)&&(!key_right)||(key_right)&&(!key_left)
This is an infinite loop because nothing in the body of the loop will ever change the variables you're testing. You probably want to use the key_left && !key_right etc. to set the speed in the first place.
 
Top