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 Class Inheritance Overriding Properties

The count for superEnemy.life displays 2 not 50

The problem for me is as clear as the headline for the question: The count for superEnemy.life displays 2 not 50

What am I doing wrong?

let x1 = 0
let y1 = 0

let coordinate1: (x: Int, y: Int) = (0,0)
coordinate1.x

struct Point {
    let x: Int
    let y: Int

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

    /// Returns the surrounding points in range of
    /// the current one
    func points(inRange range: Int = 1) -> [Point]{
        var results = [Point]()

        let lowerBoundOfXRange = x - range
        let upperBoundOfXRange = x + range

        let lowerBoundOfYRange = y - range
        let upperBoundOfYRange = y + range

        for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange {
            for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange {
                let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)
                results.append(coordinatePoint)
            }
        }

        return results
    }
}

let coordinatePoint = Point(x: 0, y: 0)

coordinatePoint.x
coordinatePoint.points()

// Enemies

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

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

    func decreaseLife(by factor: Int) {
        life -= factor
    }
}

class SuperEnemy: Enemy {

    let isSuper: Bool = true

    override init(x: Int, y: Int) {
        super.init(x: x, y: y)
        self.life = 50
    }
}

// Towers

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

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

    func fire(at enemy: Enemy) {
        if isInRange(of: enemy) {
            enemy.decreaseLife(by: strenght)
            print("Gotcha")
        } else {
            print("Darn! Out of range!")
        }
    }

    func isInRange(of enemy: Enemy) -> Bool {
        let availablePositions = position.points(inRange: range)

        for point in availablePositions {
            if point.x == enemy.position.x && point.y == enemy.position.y {
                return true
            }
        }

        return false
    }
}


let tower = Tower(x: 0, y: 0)
let enemy = Enemy(x: 1, y: 1)
let superEnemy = Enemy(x: 1, y: 1)
superEnemy.life

tower.fire(at: enemy)

1 Answer

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Linus Karlsson,

Really close mate, but when you instantiate your superEnemy ... use the SuperEnemy class not Enemy.

Happy coding, :dizzy:
Dave

You're absolutely right, Dave! I noticed my mistake by myself after posting this which made me happy to at least problem solve on my own.

Thank you for your help!