Legacy GM i have a question about movement in gml

A

Arsenio delacruz

Guest
so i was coding the movement for my player and i ran in to a weird problem

if (right) {hspd = 5}
else if (left) {hspd = -5}

if (!right) || (!left) {hspd = 0}


x += hspd
this was the original code that i had when i first was making my movement code. when i went to run the game tho the player object would not move at all unless i press both the left key and the right key at the same time and would only move to the right.

after i look at the code i tried switching the code to this

if (right) {hspd = 5}
else if (left) {hspd = -5}

if (!right) && (!left) {hspd = 0}


x += hspd

after the change the player was able to move left and and right. the thing is i am not sure why.
 
W

whale_cancer

Guest
I think you need to look at this Expressions. You seem to just be following a tutorial.

if (!right) || (!left) {hspd = 0}
This means if you are not pressing left or right you will not move. This will trigger if you are only pressing left or right.

if (!right) && (!left) {hspd = 0}
This only sets your speed to zero if you aren't pressing left and not pressing right at the same time.
 
A

Arsenio delacruz

Guest
i will admit that where some of the knowledge i have is from. but the weird thing is i am getting the opposite effect from my new code. what would be a proper way to write out the code so that the player can move left and right. but will stop once you are not pressing the keys?
 
W

whale_cancer

Guest
Getting code written for you won't actually help you learn. I would go back and look at tutorials and make sure you are understanding why the tutorial's author is doing what they are doing at any given time. That being said, this should work...
Code:
if  (right) && (!left) //if we are pressing right and not left
{
hspd = 5;
}
else if (left) && (!right) //if we are pressing left and not right
{
hspd = -5;
}
else //if neither of the above is true (if we press both or neither left/right)
{
hspd = 0;
}

x += hspd
 
W

whale_cancer

Guest
Code:
hspd = (right - left) * 5;
x += hspd;
I don't think this kind of code is good for someone who is new. It obviously will work, it's just not obvious to many newcomers why. I had this exact issue with a friend I was trying to help through learning GML, it was frustrating when they would just throw code together they found but didn't understand.
 

HayManMarc

Member
It's easy to explain.

The keyboard_check returns a 1 if true or a 0 if false.

If right is pressed (true = 1) and left is not pressed (false = 0), then hspd = (1 - 0) * 5 = 1 * 5 = 5.

If right is not pressed (false = 0) and left is pressed (true = 1), then hspd = (0 - 1) * 5 = -1 * 5 = -5.

If both are pressed (both true = 1), then hspd = (1 - 1) * 5 = 0 * 5 = 0.

If both are not pressed (both false = 0), then hspd = (0 - 0) * 5 = 0 * 5 = 0.
 
A

Arsenio delacruz

Guest
i do agree with you if i have every one do the work for me i will not learn a thing. it more i did not under stand why || did not work and && did. but i think i see why. just means i need to go back and redo my code. i do thank you all for taking the time and helping me out.
 
Top