Level 14 – Lesson 01 [Inheritance]

Introduction
Sometimes you will find yourself creating several objects that have the same (or very similar) game behaviour. You will probably notice that you are copying and pasting the same blocks of code into the events of each one. This is when you should take advantage of using inheritance. Inheritance allows you create objects that build off of previously made objects. You can use all the code of previously created objects and then add extra code to your object to make it extra special. And the good news is that inheritance is easy to code.


Part A – Parents and Children

Watch the video: 14-01-A
Done File: 14-01-A-Done.gmk


Part B – Overriding Events of your Parent

Watch the video: 14-01-B
Done File: 14-01-B-Done.gmk


Part C – ISA (Is A) Relationships

Watch the video: 14-01-C
Done File: 14-01-C-Done.gmk


Challenge 1 – Blockers

Start with the project file 14-01-X1-Start.
Use inheritance so that when the player contacts a barrier (wall, bigWall, woodWall, statue) the player stops moving and the sound ‘snd_hit’ is played. To do this, create and code the behavior inside of a parent object called ‘o_barrier’ and then make these four objects children of the barrier object. Remove the player collides with bigWall code already found coded and replace with your own player collides with barrier code. Add a few in the room and test.

Solution Video: 14-01-X1-Solution
Solution File: 14-01-X1-Done.gmk


Challenge 2 – Magical Drinks
Start with the project file 14-01-X1-Start.gmk
(or continue from Challenge 1 project file)
There are three magical drinks in the game: potion, superPotion, and poison. Use inheritance to set up the following behavior using a parent object.   (HP has been coded in the player object already – it draws out under the player)

potion: adds 10 hp to player
superPotion: adds 25 hp to player
poison: takes away 50 hp from player

All three potions share the following behavior: they are destroyed when picked up, the sound ‘pick_up’ is played, the player hp is changed.

*Bonus to this Challenge*
The poison has some additional behavior: after picked up and destroyed a new one is placed somewhere in the room at a random location that is at least 201 pixels away from any other potion type (potion, superPotion, or poison).   Hint: use a loop to keep picking locations until you have found a location where the nearest potionParent is more than 200 pixels away – then create a new poison.

Solution Video: 14-01-X2-Solution
Solution File: 14-01-X2-Done.gmk


Challenge 3 – DragOn

Start with the project file 14-01-X3-Start.gmk .
This project contains an object called ‘o_dragMe’. This object has been coded with simple drag and drop behavior that lets the user left click on the object to start and stop dragging. The object has been given an apple sprite so you can test it out in the room before you start.

Use this object as a parent object to the gold and coin objects that are placed in the room so that the gold and coin objects can now be dragged and dropped with left clicks too. Be careful though – the gold and coin objects have code in the create and step event (so does the dragMe object) so you will have to use event_inherited at the right time so you don’t lose behavior when you override parent event code.

*note: this drag and drop code is very simple and has a few flaws, but the focus here is you figuring out how to apply inheritance properly to the objects

Solution Video:  14-01-X3-Solution
Solution File: 14-01-X3-Done.gmk