Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAdam Tatusko
16,589 PointsVanquished Enemy
How can a already vanquished enemy by vanquished again?
3 Answers
Kent Lou
Courses Plus Student 2,185 PointsThe code in the video is not right... He should have put the "print("Enemy vanquished!")" statement outside of that while loop, like this:
while enemy.life > 0 {
enemy.decreaseHealth(self.Strength)
}
print("Enemy vanquished!")
Because enemy has 2 health and tower has 1 strength, the while loop will execute twice. Putting the print statement inside the while loop would tell the program to print "Enemy vanquished!" in every loop execution even in the first loop when the enemy still has 1 health left.
Boris Likhobabin
3,581 PointsHey Kent, I think you are absolutely right! I had that same question while watching the video. The way you suggested works fine for me =)
func fireAtEnemy (enemy: Enemy) {
if inRange(self.position, range: self.range, target: enemy.position) {
while enemy.life > 0 {
enemy.decreaseHealth(self.strength)
}
print("Enemy vanquished")
} else {
print("Enemy is out of range")
}
}
Reed Carson
8,306 PointsIf I remember the code correctly, I dont think there was a property to hold that information. we could do
var isVanquished: Bool = false
and set it to true when its vanquished
but unless im mistaken, thats not included, and the computer has no way of keeping track of information like that if we dont give it to it explicitly. Computers are really stupid, they know nothing unless we tell them. It has no idea what vanquished means or what an enemy is