How do I make an object go in front or behind another object in Game Maker?


The key is something called depth.

Every object in Game Maker has a depth. When you create a new object, the default depth will be 0. If you put objects in a room, and all of the objects are at a depth of 0, there wont be much control over which object goes in front. In this case, the newest object you place in the room will be in front of the other objects.

To get custom depth for objects, change the depth number in object's properties. What number should you put in? It all depends on which objects you want in the front and which ones should be behind them. Check this out:

Let's say I have an object called objPlayer and set the depth to:

Then I have another object called objTree and set the depth to:

This means that in the game, when there is an objPlayer object and an objTree object, if they pass each other, the objPlayer will be in front of the objTree object. Why is that?

Because the lower the number, the more it will be in the FRONT. The higher the number, the more the object will be in the BACK! This is because object with the higher depth value are created first, and the objects with lower numbers are created last.

Also remember that the numbers don't have to stay in the 10's. If we take the above example and add one more object called objTree2 and set the depth to:

This means that now the 2nd tree object will be IN FRONT of all other objects with depth higher than itself, because it now has the lowest depth value.

Depth is very important when it comes to layering objects in a 2D game, and it becomes VERY important when doing 3D games because you will need objects to draw at different times so they can display correctly on the screen! Nice!

Page updated: March 23rd, 2015 @ 6:21 AM Eastern Time

Copyright 2010-2023
All Rights Reserved.
Our other websites: