0

GameMaker Studio 2 UI showing "x = x + 4" in the Step event of the objectIn the tutorial it told me to add the sprite to the object and then add an event to the object called Step. I did this and then added the sprite to the room.

The GML code I used in the Step event is x = x + 4; to move to the right but when I run the game it does not move at all.

Why doesn't my sprite move to the right when I have this code in the object's Step event?

Maple
  • 103
  • 4
MM1Content
  • 5
  • 1
  • 6
  • Can we see the code please? It will help us figure out what's wrong. – OKprogrammer Feb 05 '21 at 19:17
  • 1
    Have you tried a different tutorial? This sounds like very basic functionality that should be covered in many tutorials, so checking another one can help you spot a step that might not have been explained well in the first one you tried. – DMGregory Feb 05 '21 at 19:18
  • noobprogrammer the code is x = x + 4; and DMGregory I'll try another tutorial – MM1Content Feb 05 '21 at 19:25
  • If someone asks you a question in the comments, it's generally because what was present in your post already wasn't enough to answer their question. So just restating the same content from your post usually doesn't help to move the conversation forward. Instead, try editing your question to show more of the surrounding context - show us where this line of code sits inside your step event, for example. If you need to take a screenshot to show the surrounding editor context, that can help too. – DMGregory Feb 05 '21 at 19:49
  • 1
    I solved it by adding the object instead of the sprite into the room. Now I just need to figure out how to remove the sprite from the room. – MM1Content Feb 05 '21 at 21:24
  • objects and sprites are placed in different layers, make sure you've selected the correct layer when you want to select something. – Steven Feb 16 '21 at 07:45
  • Thank you, I wasn't sure about the layer part, that helps :) – MM1Content Feb 20 '21 at 22:02
  • 1
    Hello! Your question has been reopened; if you found the solution (as per one of your previous comments), you should post an answer re-stating this; visitors tend to look at the answer text rather than in the comments for clues on how to solve similar issues they have. – Vaillancourt Sep 29 '21 at 13:14

2 Answers2

1

As mentioned in the comments, you have placed the sprite in the room instead of the object itself. The object is the one that contains the code and functionality, so replace the sprite with the object and then your function will work.

Keep in mind that sprites and objects are placed on a different layer in a room.
Objects are placed on an 'instances' layer. and standalone sprites are placed on an 'Assets' layer.

Steven
  • 891
  • 6
  • 19
0

That is not a player moving script, but this is:

if (keyboard_check(vk_left)) x = x - 3;
if (keyboard_check(vk_right)) x = x + 3;
if (keyboard_check(vk_up)) y = y - 3;
if (keyboard_check(vk_down)) y = y + 3;

This snippet works for top-down movement. In the first line of code, it basically says 'if you press the left arrow button key on your keyboard, you will move left'.

liggiorgio
  • 4,636
  • 6
  • 25
  • 37
John
  • 1