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!

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 trial

iOS Object-Oriented Swift 2.0 Classes Helper Methods

Vanquished Enemy

How can a already vanquished enemy by vanquished again?

3 Answers

Kent Lou
Kent Lou
Courses Plus Student 2,185 Points

The 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 {
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
Boris Likhobabin
3,581 Points

Hey 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 {
            print("Enemy vanquished")

        } else {
            print("Enemy is out of range")
Reed Carson
Reed Carson
8,306 Points

If 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