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

Kylie Forbes
Kylie Forbes
3,405 Points

"Value type of 'Tower' has no member 'fireAtEnemy'" error

Hello! When I attempt the final command at the end of this video, I receive the error "Value type of 'Tower' has no member 'fireAtEnemy'." Has anyone else experienced this? My code is pasted below.

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("Enemy vanquished!")
                }
            } else {
                print("Darn! The enemy is out of range!")
            }
            }
        func inRange(position: Point, range: Int, target: Point) -> Bool {
            let availablePositions = position.surroundingPoints(withRange: range)
            for point in availablePositions {
                if (point.x == target.x) && (point.y == target.y) {
                    return true
                }
            }
            return false
        }
    }
}

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

tower.fireAtEnemy(enemy)

2 Answers

Reed Carson
Reed Carson
8,306 Points

Your fireAtEnemy method is created inside of your init statement by accident. move one of the closing brackets from the bottom to just above the fire function and below init statement.

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

}    

  func fireAtEnemy(enemy: Enemy) {

don't forget to delete the one from the bottom after you move it

Kylie Forbes
Kylie Forbes
3,405 Points

That worked. Thank you so much!