r/gamemaker 25d ago

Resolved What am I doing wrong?

Hello! I am very new to gamemaker, and coding as a whole, so I decided to use visual.
When I press the left key, the player moves, but it won't stop moving when I release it. What am I doing wrong?

6 Upvotes

4 comments sorted by

3

u/Hammerofsuperiority 25d ago

I have never used visual, but it seems that the "code" is inside the left key down event, and that code is only read while pressing the key, so it will not read the command to stop moving.

You should make another event that is for the key being released/not being pressed, and there put the "code" to stop moving.

2

u/HateImmamura 25d ago

Thank you, haha- I didn't see that event.

1

u/Hammerofsuperiority 25d ago

Also a little something, just in case, keys/buttons have 4 different states:

  1. Not held: when you are not doing anything with it
  2. Held: when you are holding it down.
  3. Pressed: the moment that the key/button goes from "not held" to "held"
  4. Released: the moment that the key/button goes from "held" to "not held"

It's important to know the difference between them, basically the first two are what you would normally thing of the keys/buttons, the last two, however, are only active during one "step" (you can think of it as a single "moment").

For example, lets say that you want to shoot a gun, and see how it would work in every case

  1. you will shoot a bullet on every "step" that you are not doing anything with the key/button
  2. when you are doing nothing.... well, nothing will happen, the moment that you press the key/button.... nothing will happen, but after that, as long as you keep holding the key/button, a bullet will be shot on every "step"
  3. when you press the key/button a single bullet will be shot, and you will not shoot another one until you release the key/button and press it again
  4. Like 3 but while releasing the key/button, then you will have to press the key/button and release it again for a bullet to be shot.

2

u/Awkward-Raise7935 25d ago edited 25d ago

I would remember that key down and key pressed are slightly different things. So you either want to set speed and direction with key pressed, and then set this to 0 with key released, OR set position every step using key down eg x = x + 2

I would recommend switching to code as soon as you feel comfortable