While Loops3:55 with Jeremy McLain
One way to work with every object in an array is with a while loop.
The difference between ++ on the left side of a variable versus ++ on the right side of a variable - Pay attention to the code example for a demonstration of how these two are different.
[MUSIC] 0:00 Now that we finished up the Invader class, let's move on to the Tower class. 0:04 So far the Tower class is completely empty, let's fill it out. 0:09 First each Tower will need to have a location on the map, so 0:13 let's add a field for that. 0:16 Nothing outside the class needs to know the location of the Towers, 0:18 so we won't create a MapLocation property. 0:21 This will just be a field, because each tower needs to have a location on the map, 0:25 it should be a parameter in the constructor. 0:30 And we'll need to use it to set the location field. 0:32 Now most tower defense games don't allow placing towers on the path. 0:43 We could add some validation here to ensure the towers can't be 0:48 placed on paths. 0:51 In fact, you know what? 0:53 You already saw an example of this validation pattern when we created 0:54 the MapLocation class. 0:57 I'll leave that as a challenge for you to do on your own. 0:59 Now we need to think about what other behaviors and attribute Towers will need. 1:03 One behavior I can think of is the ability to shoot at invaders. 1:08 Let's create a method called FireOnInvaders. 1:13 This Method will go through all of the Invaders on the map and 1:16 check to see if they are in range. 1:19 If they are, the Tower will shoot at them and decrease their health. 1:21 This method will need to have access to all the Invaders on the map. 1:26 So we'll pass them in as an array. 1:31 Now we'll need to determine which invaders are in range. 1:33 To do that we'll need to look at each invader in the array. 1:37 For that we'll need a loop. 1:41 Let's look at how we would use a while loop for this. 1:44 If you're unfamiliar with while loops check out the teacher's notes for 1:47 a link to a Treehouse course that covers them. 1:50 We'll need to loop through each index in the array. 1:54 So we'll need a variable to keep track of where we are in the array. 1:57 Let's create an integer variable called index here and initialize it to zero. 2:01 Now let's create a while loop that will stop when it gets to the end of the array. 2:07 To do that, we'll check if the index is less than the length of the array. 2:11 The last index in an array is always one less than the length of 2:19 the array because we start counting at zero. 2:22 If index equals the length of the array, 2:25 then we know that it's time to exit the loop. 2:28 Now we can use the index variable to index into the array. 2:31 We'll assign it to a variable so we don't have to index into the array 2:35 every time we want to reference this specific Invader. 2:38 Now that we have an Invader from the array, 2:42 we can do whatever we want with it here. 2:45 Finally we'll need to add one to the index variable so 2:47 that the next time through the loop we're getting the next invader in the array. 2:50 This way, we can do something with each of the items in the array. 2:54 This is called incrementing the index variable by one. 2:59 Incrementing is so common that there is some syntactic sugar for it. 3:03 Instead of saying index plus equals one, we can just type index++. 3:07 This is shorthand for adding one to the index variable. 3:13 Sometimes you may see the ++ at the start of the variable name. 3:17 There's a very subtle difference between these two forms. 3:21 I've included a description of these differences in the teacher's notes. 3:24 In most case, you want to put the ++ on the right side of the variable. 3:28 Our while loop as it's written will loop through each item in the array 3:32 one at a time. 3:36 This allows us to do something with each item by adding some code right here where 3:38 I've typed this comment. 3:41 This works perfectly, but I want to show you another way to write this 3:43 that's both easier to write and to understand. 3:48 Let's take a short break and then we'll learn about for loops. 3:51
You need to sign up for Treehouse in order to download course files.Sign up