1 00:00:00,370 --> 00:00:03,460 We learned about for loops in the previous video. 2 00:00:03,460 --> 00:00:04,740 We wrote this for a loop so 3 00:00:04,740 --> 00:00:08,710 that we could do something with all of the invaders in the invaders array. 4 00:00:08,710 --> 00:00:12,880 It turns out that looping through all the items of a collection such as an array is 5 00:00:12,880 --> 00:00:18,180 so common that there is yet another type of loop that's designed to do just that. 6 00:00:18,180 --> 00:00:19,760 It's called a for each loop. 7 00:00:21,000 --> 00:00:24,120 I'll write what that looks like below our existing four a loop, so 8 00:00:24,120 --> 00:00:25,570 that we can compare the two loops. 9 00:00:30,700 --> 00:00:34,870 With the four each loop we get rid of the loop counter variable entirely. 10 00:00:35,880 --> 00:00:39,740 This loop will always run if there's at least one item in the array. 11 00:00:40,780 --> 00:00:41,990 Each time through the loop, 12 00:00:41,990 --> 00:00:45,870 the invader variable will be set to the next item in the array. 13 00:00:45,870 --> 00:00:47,770 So the first time through the loop, 14 00:00:47,770 --> 00:00:50,270 invader will contain the first invader in the array. 15 00:00:51,370 --> 00:00:56,070 The second time through the loop it will contain the second item, and so on. 16 00:00:56,070 --> 00:00:57,680 Pretty neat? 17 00:00:57,680 --> 00:00:58,270 Using for 18 00:00:58,270 --> 00:01:03,241 each loops makes it clear that we want to loop through every item in the collection. 19 00:01:03,241 --> 00:01:06,770 We'll use this for each loop to look at each invader in the array and 20 00:01:06,770 --> 00:01:08,650 determine if it's in range of the tower. 21 00:01:09,670 --> 00:01:11,820 We already know where the invader and 22 00:01:11,820 --> 00:01:16,700 the towers are located because they both have map location member variables. 23 00:01:16,700 --> 00:01:21,260 We can use the distance to method to see how far they are from each other and 24 00:01:21,260 --> 00:01:24,650 then check if the distance is less than the shooting range of the tower. 25 00:01:25,780 --> 00:01:29,240 Why don't we write a method that does this comparison for us? 26 00:01:29,240 --> 00:01:33,210 We could put this method in the tower or the invader classes. 27 00:01:33,210 --> 00:01:37,570 However if we consider that this method is really only determining if 28 00:01:37,570 --> 00:01:41,080 two map locations are within a certain distance of each other 29 00:01:41,080 --> 00:01:43,730 I think it's best to put it in the map location class. 30 00:01:46,140 --> 00:01:48,230 Let's call this method in range of. 31 00:01:48,230 --> 00:01:56,840 It will take another map location as a parameter and an integer range. 32 00:01:56,840 --> 00:02:01,050 Here we can call the distance to method we already wrote to get the distance between 33 00:02:01,050 --> 00:02:05,130 the map location object this method was called on and the one that's passed in. 34 00:02:07,580 --> 00:02:12,370 Then we'll check if that distance is less than or equal to the range passed in. 35 00:02:12,370 --> 00:02:14,385 We'll return the result of this comparison. 36 00:02:14,385 --> 00:02:20,032 Now back here in the tower class we can write if our 37 00:02:20,032 --> 00:02:25,830 location is in range of 38 00:02:25,830 --> 00:02:31,800 the invaders location using a range of one. 39 00:02:33,470 --> 00:02:38,090 This one here is the width of one grid square. 40 00:02:38,090 --> 00:02:40,820 If the invader's in range, we'll decrease its health. 41 00:02:40,820 --> 00:02:43,740 This shows that the tower was able to cause some damage to the invader. 42 00:02:44,840 --> 00:02:47,640 And we'll have the invaders health decreased by one. 43 00:02:48,700 --> 00:02:53,330 As the code stands right now, the towers can fire on every invader in range, 44 00:02:53,330 --> 00:02:55,370 even if they've already been neutralized or 45 00:02:55,370 --> 00:02:56,940 even if they've made it to the end of the path. 46 00:02:58,000 --> 00:03:02,220 Let's fix this by only allowing them to shoot at invaders that are still active 47 00:03:02,220 --> 00:03:06,000 by checking the is active property we previously added to the invader class. 48 00:03:07,200 --> 00:03:11,020 So now the towers can only decrease the health of an invader 49 00:03:11,020 --> 00:03:14,840 if they're both active and they're in range. 50 00:03:14,840 --> 00:03:17,460 Finally let's handicap the towers a bit 51 00:03:17,460 --> 00:03:21,370 by only letting them shoot at one tower at a time. 52 00:03:21,370 --> 00:03:25,470 We can do this by forcing the foreach loop to exit after it's found and 53 00:03:25,470 --> 00:03:27,590 shot one enemy. 54 00:03:27,590 --> 00:03:31,780 For loops and for each loops just like while loops, can use the break and 55 00:03:31,780 --> 00:03:32,910 continue statements. 56 00:03:33,910 --> 00:03:36,620 Remember the continue statement like this causes the loop 57 00:03:36,620 --> 00:03:38,230 to immediately go back to the beginning. 58 00:03:39,340 --> 00:03:42,590 In the case of a for each loop, the continue statement 59 00:03:42,590 --> 00:03:46,530 causes the loop to move on to the next item in the array or collection. 60 00:03:48,020 --> 00:03:50,720 We don't want to use the continue statement right here though. 61 00:03:50,720 --> 00:03:53,640 I'm just showing it here, so that you can see how it works. 62 00:03:53,640 --> 00:03:57,470 Instead we want to break out of the loop using the break statement. 63 00:03:58,590 --> 00:04:00,030 There is nothing after the loop here. 64 00:04:00,030 --> 00:04:02,330 So the method returns to the code that called it.