I’ve been toying with GameMaker Studio 2 and RPG Maker MV. I felt constrained by RPG Maker MV; not so with GMS 2. When I looked up how to do something with GMS I found several ways to do it. With RPG Maker MV I would usually find warnings to “not attempt this.” RPG Maker MV is a fine product, don’t get me wrong. I just ended up liking GMS 2 more.
Game Maker Studio 2 is the successor to Game Maker Studio 1.4. For 1.4 an abundance of tutorials, books and videos exist, most of which translate fairly well to GMS 2. But there are folks who are looking for GMS 2 specific tutorials – including myself. So I’m poking through the pile of GMS 1.4 stuff and trying out the various things I want to do.
All things in Game Maker Studio have multiple ways to implement what we’re after. Today, we’re looking at the movement of an object on screen. (A: refers to the action you should take. ER: refers to the expected result of your action; a screen capture will usually follow this for comparison.)
A: Open Game Maker Studio 2
ER: Game Maker Studio 2 opens as shown below.
A: Select the GameMaker Language project.
ER: GameMaker Studio will prompt you for a filename.
A: Name the project whatever you wish; it’s irrelevant for the purposes of this tutorial.
ER: You’ll arrive at the default work space for GameMaker Studio 2.
We need to create a sprite, an associated object, and the code to move the sprite around. Before we go much further we should examine what the GMS 2 help offers us in terms of guidance.
A: Hit F1 while in the work space pictured above you’ll open the context sensitive help.
ER: The Game Maker Studio 2 help screen appears.
A: Click the Search tab on the left hand side of the screen and enter movement into the search box.
ER: The results of the search will appear. About halfway down, there’s a topic Movement and Collisions.
A: Double click Movement and Collisions.
ER: The screen will appear with the topic Movement and Collisions.
A: Read the topic and all of the subtopics to be given a foundation on how GMS 2’s internal movement works.
ER: What you should walk away with is knowledge of the following:
- The Help system in GMS 2 is very thorough and covers every imaginable topic.
- The direction variable has a valid value between 0 and 360, with 0 being right, 90 degrees being up, 180 degrees being left, and 270 degrees being down.
- Speed is a variable which specifies how many pixels an object moves.
- The friction variable specifies how many pixels the object should slow down for every tick.
- A tick is equivalent to a frame per second; game logic runs in each frame per second.
- Without a defined friction value or a counter direction an object would maintain speed and direction forever. This is most likely not what you want to happen.
- The direction variable, inherent on every object, determines the direction of movement applied at (speed) pixels, speed being another attribute of a GameMaker object.
A: Close the Help System by clicking the X on the tab it opened under.
ER: The Help System should close, leaving you at the original Workspace.
A: Create a Sprite by right clicking Sprites and choosing Create.
ER: The Sprite window will appear in the Workspace.
The Image Editor will appear with the completely transparent image of a square. We want to fill this with any color, any lines, anything that you’ll find useful for debugging or visual purposes.
A: Click the paint can (highlighted by a red rectangle) and then click the surface.
ER: The surface should fill with your currently selected color. (White, unless you changed it.)
A: Close the Image Editor by clicking the X on the tab.
ER: The Image Editor should close.
A: Close the Sprite in the Workspace.
ER: The Sprite window should close.
Now we need to create an object, which will be the center of our attention.
A: Right click the Objects in the Resources window and choose Create Object.
ER: The Object Editor will appear.
A: Change the name from object0 to o_movement (a discussion of naming will be forthcoming; accept that this is a standard amongst GMS programmers).
ER: The object name will change to o_movement.
A: Click the ellipsis next to No Sprite Image and then select the 64×64 white block we just created.
ER: The object will end up displaying as a large white block.
A: On the attached Events window click Add Event, choose Key Down from the popup menu that appears, scroll down to Letters, and then click on the W.
ER: A code window will appear where you can define the Event for the W key being pressed down.
In the code screen that appears we need to do two things: set the speed of the object, and set its direction.
A: For most, W is equivalent to up — looking back at what we learned in the Help File, we see that up is equal to 90. Speed can be whatever you’d like, although I am using 4 pixels per tick here.
direction = 90;
speed = 4;
ER: The code window is displayed in the Workspace with the above code in it.
A: We want to repeat this for the last three directional inputs, A, S, and D, using the appropriate direction for each.
ER: We’ll end up with an event handler for Key Down/A, Key Down/S, and KeyDown/D. The direction will be different in each code window.
A: Click on Add Event and choose the Create event. In the code window that appears enter the following code:
friction = 0.2
ER: The friction of the object is set.
A: Double click the room0 under the Resources toolbox.
ER: The room editor will appear.
A: Drag the o_movement object into the room; anywhere, it doesn’t matter.
ER: The object will be displayed inside the room with a 64×64 white block as it’s sprite.
A: Then hit F5 to build your project. Try to move the square in the window.
ER: The square should move corresponding to the W, A, S, and D keys.
If the square moves the wrong direction you assigned the incorrect value in the KeyPress event for whichever direction you’re holding.