Let’s consider how coordinates work in Game Maker.
I will open the room (Room1) and if you move the mouse to the upper left corner, the coordinates x = 0, y = 0 will be there. If you move to the right/left, the x coordinate will change, and if you move up/down, the y coordinate will change.
This is the basic information you need to know by coordinates. Now let’s try to add an object and move it
The object can be added to the room by any available coordinates. I will create an object. But for the object to be visible in the room, it needs a sprite. You can simply paint the object with some color. To do this, on the object page, you can click
New Sprite or Ctrl+Shift+N. A sprite window will open immediately where you can click
Edit Image, then a window will open in which you can draw your sprite
But a sprite will be created with the name Sprite1 and in the objects folder. It should be renamed and moved to the Sprites folder. I will rename it to
spr_vars so that it is clear that the sprite refers to my object called
Now you can open the room and drag the object there
We also modify the object so that it prints the coordinates in which it is located.
I will add the event
Create to the object in which I will specify the initial coordinates
x = 0 y = 0
I will also add the
Draw event in which I will display the coordinates. I will use the
draw_text which is a method already known from previous posts. Before displaying the text, you need to add
draw_self() to draw the object itself. And then two execution of the
draw_text method for the X and Y coordinates. As the first argument, I pass x, which is equal to the x coordinate of the object. The same with y, but I shift the text by the y coordinate so that the text does not overlap with each other. And I print the following text “X: 0”. To do this, I add the string “X:” to the x coordinate. But since the x coordinate is an int, I first need to convert it to the string type.
draw_self() draw_text(x, y+5, "X:" + string(x)) draw_text(x, y+20, "Y:" + string(y))
Press F5 to start the game
Now let’s add the ability to move the object and see the coordinates of the object. I’ll do it with the
Global Left Down type event. why do you need global? To be able to click anywhere in the room. If you use
Left Down, the click will be counted only if it is made on the object. And I will add the following code
x = mouse_x y = mouse_y
This code changes the coordinates of the object to the coordinates of the mouse cursor
Every time we click, the upper left corner of the object is placed at this point. This is because the origin is set there. Origin is specified at the sprite level and can be changed. To do this, you need to open the sprite and you will immediately see that the origin is set to the upper left corner. With the help of the mouse, you can move it to any point or choose one of the existing options such as Top Left, Top Center, and others.
If I move the origin to another point and start the game and move the object, then exactly the place specified as the origin will fit in this point
But now, as you can see, the text that we display has shifted. Because it displays according to origin. Therefore, to redraw the text inside the object, the coordinates in the
draw_text method need to be changed
For example, this is the case if the origin is in the center
draw_self() draw_text(x-20, y-15, "X:" + string(x)) draw_text(x-20, y, "Y:" + string(y))