Game Maker Studio 2: Object Movement (Alternate)

Today we’re going to look at a method of creating a control scheme using only one events, rather than six.

The key event is the Step event.  The Step event fires every tick, once.  When you go to create it by clicking Add Event -> Step you’ll want to ensure you select Step and not Begin Step or End Step.

A:  Create a new project, or open your work project.
ER:  The project opens into the Workspace.

A:  Create a new Sprite by clicking Sprites in the Resources toolbox and choosing Create Sprite.
ER:  The Sprite window appears.

A:  Click Edit Image on the Sprite window.
ER:  The Edit Image screen appears.

A:  Click the Paint Bucket in the Toolbox, then click anywhere on the image to fill it.
ER:  The translucent square becomes a solid white.

A:  Close the Edit Image screen by clicking the X in the tab.
ER:  The Edit Image screen closes.

A:  In the Resources toolbox right click Objects and choose Create Object.
ER:  The object editor appears.

A:  Click the ellipsis (…) next to No Sprite to bring up the Sprite Selection window.
ER:  The Sprite Selection window should appear.

A:  Select the solid white box in  the Sprite Selection Window by double clicking it.
ER:  The sprite selection box will close, leaving the Object Editor window as the primary focus of the Workspace.

A:  Click Add Event, cursor down to Step, then select Step from the resulting menu.

ER:  The Code Window will appear.

A:  Enter the following code in the window.  (Explanations as to what’s occurring are below.)

ER:  The code window contains the above and there are no red warning symbols in the left margin.

So what’s going on in the above code?  First we’re setting friction for the object; here we maintain the 0.2 value we used for the previous movement method.  The friction setting determines how fast the object slows down once input driving it has disappeared.

Second, we’re creating a new variable objectSpeed.  This determines how many pixels the object moves in the appropriate direction.  Honestly, this belongs in the Create Event but I wanted to demonstrate a single script solution for movement.

Third, we’re executing checks and storing the result in a variable.  We’re checking whether the A, D, S, and/or W keys are being pressed.  These boolean indicators are then used to determine whether or not we should modify the x and y position of the object with the objectSpeed.

The general format of the if command is if (condition) { true code; } else { false code; }.  Condition can be any statement which results in a True or False variable.  We could combine the *_PRESSED indicator assignments directly into the if statement if we had to simplify this code further, but the current structure makes it explicit what’s occurring.



Game Maker Studio 2: Object Movement

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:  Click on New under the Getting Started section of the landing page.
ER:  The prompt for Drag and Drop or GameMaker Language will appear.

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.

A:  On the resulting window click Edit Image.
ER:  The Edit Image window should appear.

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.

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:

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.


Amazon Associates now available in Missouri

Good news, everyone!

Amazon pulled the Associates program for all members residing in Missouri when Missouri passed a bill making each Amazon Associate an affiliated entity with Amazon, thus giving Amazon a physical presence in the state, thus meaning that Amazon had to collect sales tax.

Well now that Amazon is collecting state sales tax everywhere there’s no reason to hold it against us Missourians (or any other state which passed a similar bill) any longer.  And they’re not – the affiliate program is open to Missourians again.  You just have to reapply.