Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we have a good understanding of structs and classes, let's take a step back and examine our object graph and the relationships between our objects.
-
0:00
Before we move on to the next bit, let's take a step back.
-
0:03
We covered a lot in the last video, most of which wasn't anything new conceptually.
-
0:08
We just put a lot of stuff together that we've learnt in the past few courses
-
0:12
and videos.
-
0:14
We started by creating a simple structure, Point.
-
0:18
We use Point to represent a coordinate point.
-
0:21
And it contains two simple stored properties X and
-
0:24
Y that allow us to place it on a hypothetical map.
-
0:28
Point also has a single method surrounding points that given a range
-
0:32
returns an array containing all the possible points surrounding it.
-
0:37
Point is an object that then form the basis for two new types.
-
0:42
The first one was a class that represented an enemy in our game.
-
0:46
The enemy needs to occupy a space on the map.
-
0:49
And we model this by giving it a stored property of type Point
-
0:53
since we can use custom types as building blocks for other objects.
-
0:58
An enemy also has a life which is a variable stored property and
-
1:01
can be changed.
-
1:03
We don't change this property directly, instead we added a method to our class,
-
1:08
Decrease Health, that reduces the health of the enemy by some factor.
-
1:13
When we want to affect the enemy's life, we simply call the decrease Health method.
-
1:18
This is a common pattern that you will soon learn, where we try to use methods to
-
1:22
interact with our objects instead of manipulating the properties directly.
-
1:27
The second class we created was a Tower.
-
1:30
Like enemy, towers also occupy a position on the map.
-
1:34
So we give it an instance of Point as a stored property.
-
1:38
Here's where things get interesting.
-
1:40
Towers can fire at enemies, so
-
1:42
we created an instance method that takes an enemy as a parameter.
-
1:47
It checks whether the enemy is in range of the tower, and
-
1:50
if it is, it calls the instance method,
-
1:52
Decrease Health, on that instance of enemy until it is vanquished.
-
1:56
As I've mentioned so far, all these functions we wrote, and
-
2:00
the properties are objects contained, are instance methods and properties.
-
2:04
So to put any of this to use, we need to create an instance,
-
2:07
which is what we do at the bottom.
-
2:09
Once an instance is created the values we pass in
-
2:12
through our initializer methods are substituted where we use them.
-
2:17
For example, since the fire method takes an enemy as a parameter,
-
2:22
we can pass in several different enemies and different points on the map.
-
2:26
And see how our game works.
-
2:28
So why don't you give this a try to see if you understand what's going on so far.
-
2:33
Create an instance of enemy and place it far away from the tower.
-
2:37
Then call the fire method on that tower and pass in this new enemy as an argument.
-
2:42
What happens?
-
2:44
What we just wrote while not complex code
-
2:47
can be hard to understand at first because there's simply a lot going on.
-
2:51
We've never created objects before.
-
2:53
Much less objects that interacted with each other.
-
2:56
Re-watch watch this video, if you have to.
-
2:58
Because getting this mental model in place of how objects interact with
-
3:02
one another through stored properties and instance methods is very important
-
3:07
because in the next few videos, we're going to build on top of it.
You need to sign up for Treehouse in order to download course files.
Sign up