When you created added several monsters to the maze game that moved in different directions (Adding Monsters to the Maze Game), you may have found that you forgot to add an event to the player object that checked for a collision with am instance of a “vertical monster” object as well as an instance of the “horizontal monster” object. (If that is the case, the player would not have suffered any ill consequence of colliding with a vertically moving monster; to remedy this oversight (?!), you need to check for a collision with an
obj_verMonster in the player object, and add a similar set of actions as used in the collision with an
If you have lots of objects that are ‘variations on a theme’ – such as the monster objects – it can become quite confusing trying to maintain separate collision detection events for each of them.
However, there is a technique we can use whereby objects can ‘inherit’ certain properties from other ‘parent’ objects, in such a way that allows them to be treated as those parent objects in certain respects.
(Objects, instances and inheritance are all ideas taken from computing/software engineering – in particular, the object oriented programming model. I’ll cover some of the basic nitty gritty in another post, but if you’re already familiar with the basic idea, and are feeling up to a challenge, you can maybe learn a bit more from this OpenLearn unit on Modelling object-oriented software – an introduction .)
To make use of these ‘object inheritance’ properties, you will need to run Gamemaker in Advanced mode. If you are not already in Advanced Mode (?!), save your current game, go to the File menu and select ‘Advanced Mode’. Gamemaker will close and open again with a new game, so you will need to open the game you just saved back in.
Delete the vertical monster object and the corresponding collision event from the player object and create a new ‘vertical monster’ object. Give it an appropriate sprite. You should see that there are now several more options in the object editor now that we are in advanced mode. The one we are interested in at the moment is the parent property.
Set the parent of the vertical monster to be the horizontal monster.
If you were now to add the vertical monster to the game, it would behave in exactly the same way as the horizontal monster – it has inherited all its behavioural properties.
However, we can override some of those properties by explicitly defining them within the vertical monster object. In this case, the property we want to override the direction the monster moves in, so add a Create event that sets the movement direction to be up or down, with a speed greater than that of the player object.
We do not need to explicitly define the collision event between either the vertical monster and the wall, or the player object and the monster because the player essentially sees the vertical monster as a horizontal monster type and reacts to it as such.
With multiple monsters in your game, it is possible to set up many situations that may prove increasingly challenging for your player to negotiate. For example, see if you can set up a couple of rooms that are empty except for five or six monsters that just move up or down, either all in a line, or maybe out of sequence with each other (start some of them at the top of the screen, others at the bottom, and so on). The challenge here is simply to cross the room… Are there any particular configurations of monsters that make this especially difficult to achieve? To what extent does increasing the speed of the monsters make it a more or less difficult task to cross the room?