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 trial

iOS Object-Oriented Swift 2.0 Classes Helper Methods

Ced I
Ced I
999 Points

Code seems to break when creating an enemy out of the range of the Tower

When I modified the laserTower class to not have an override function in it. It simply replaces the range and power with 100. And then when I create an instance of a laserTower and a superEnemy (can be an enemy too; it shouldn't matter), and use the .fireAtEnemy function on the superEnemy, it seems to not be running. Perhaps it's caught in an infinite loop?

Here is my code:

class Enemy {
   var life:Int = 2
   let position: Point

    init(x: Int, y: Int) {
        self.position = Point(x: x, y: y)
    }

    func decreaseHealth(factor: Int) {
        life -= factor
    }
}

class Tower {
    let position: Point
    var range: Int = 1
    var strength: Int = 1

    init(x: Int, y: Int) {
        self.position = Point(x: x, y: y)
    }

    func fireAtEnemy(enemy: Enemy) {
        if inRange(self.position, range: self.range, target: enemy.position) {
            while enemy.life > 0 {
                enemy.decreaseHealth(self.strength)
                print("POW! Enemy health is now \(enemy.life)")
            }
            print("Enemy health is now 0. The enemy has been vanquished!")
        } else {
            print("The enemy was out of range...")
        }
    }

    func inRange(position: Point, range: Int, target: Point) -> Bool {
        let surroundingPoints = position.surroundingPointsWith(withRange: range)
        for point in surroundingPoints {
            if (point.x == target.x && point.y == target.y) {
                return true
            }
        }
        return false
    }
}

// Class Inheritance!
class SuperEnemy: Enemy {
    let isSuper: Bool = true

    override init(x: Int, y: Int) {
        super.init(x: x, y: y)
        self.life = 50 // we're only able to change this b/c the life field is a VAR instead of a LET

    }
}

class LaserTower: Tower {
    override init(x: Int, y: Int) {
        super.init(x: x, y: y)
        self.range = 100
        self.strength = 100
    }
}



let tower = Tower(x: 0, y: 0)
let laserTower = LaserTower(x: 5, y: 5)
let enemy1 = Enemy(x: 1, y: 1)
let enemy2 = Enemy(x: -12, y: 15)
let superEnemy1 = SuperEnemy(x: 10, y: 10)

tower.fireAtEnemy(enemy1)
tower.fireAtEnemy(enemy2)
laserTower.fireAtEnemy(superEnemy1)
tower.fireAtEnemy(superEnemy1)

[MOD: edited code block]