Featured Post

Dodgeball Game Maker Tutorial

This will be a series of posts that will teach you step by step how to make a simple arcade game using Game Maker Studio 2. The final game w...

Thursday, September 17, 2020

Dodgeball Tutorial: Creating the player

On this chapter we will create the player object. An object is something that exists in the game, for example a player, an enemy, a pickeable item… Also there are invisible objects which can’t be seen by the player but that have some functionality as to control some logic of the game.

Player Object

To create a new object, you’ve to go to the Assets tab, and right click on the folder “objects” to display the creation menu, and select “Object” to actually create it under this folder. We will name this object “oPlayer”. By convention object names are prefixed with “o”.

Player Sprite

Now that we have a player object, we need a sprite for it, so we can see it ingame. To do this, on the Assets tab, go to the “sprites” folder and create a new sprite just as we did with the player object. We will name this file “sPlayer”. By convention the sprite names are prefixed with “s”.

Once created, the sprite editor will open and we have to click on the import button to import the player sprite. At the beginning of this tutorial there is the link to download the assets needed. Once we click on import, we will have to tell the file we want to import, so we will have to browse to where you downloaded the game assets and select the player sprite inside the characters folder. Once imported, we have to set the origin of the sprite to middle center. To do this, we go to the top left corner of the sprite editor and select “middle center” on the selector. We won't need to do this if we already configured the project to set the sprite origin at middle center by default.

Assign the player sprite to the player object

Now we can close the sprite editor and open the object editor by double clicking on the player object in the Assets tab. On the object editor we can assign the sprite for that object, so we will choose “sPlayer” for the player object.

Adding the player object to the room and running the game

Now we can add our player to the room and run the game to actually see it. A room is like the container of the game elements. By debault Game Maker created a room for us under the folder “rooms” in the Assets tab. We can right click on that room and rename it to “rPlay” (by convention room names are prefixed with “r”).

We can open the room editor by double clicking on the room in the assets tab, just as we did to open the sprite editor or the object editor. From this editor we can add objects to the room, so let’s add the player. First we have to make sure that we are on the “instances” layer. Layers are to tell the engine what needs to be drawn on top. For now, as we have not created our own layers, we will use the default layer to place our player.

With the “Instances” layer selected, just drag and drop the player object to the black room that we have. Now we can run the game to actually see our Player object running! To run the game we can press f5 or click on the arrow on the toolbar.

Once we run the project we can see our player in the room when running the game, it still does nothing but we are going to fix that now.

Controlling the player with the mouse

We are going to make the player to move where our mouse cursor is. This way we can control it and move around the room. To do this, we have to create a step event on the object editor of our player.

Object do things with events. The create event contains the logic that runs when the object is created in the room. The step event is an event that runs every frame of the game, so we want this event to update the player position each frame to be at the mouse position. Once created the step event, a text window will appear were we can put code on it. We want our object to be each frame at the mouse position, so to do this, we can do it with this two lines of code.


	x = mouse_x;
	y = mouse_y;

What we are telling to the engine here is that, the position of the player in the X axis and the Y axis is equal to the current mouse position on each axis. This way each frame we update this position and the player will move as we are moving the mouse. X and Y are built-in variables that every object in Game Maker has. It is the object position and all objects have this position properties.

Now if we run the game, we will be able to move the player with the mouse. But the game still feels boring because the player can’t interact with anything in the game, so let’s fix that by creating our first enemy on the next chapter!

No comments:

Post a Comment