## 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.

# Error: EXC_BAD_ACCESS (code = 2, address = 0x7fff4f6c1f98) in my Tower Defense code

I am following the steps in the videos in my Xcode. It showed an error: Error: EXC_BAD_ACCESS (code = 2, address = 0x7fff4f6c1f98)

at line (at the end of the code) let laserTower = LaserTower(x: 2, y: 2)

and I can't seem to get around it. Any help?

My codes:

class Point {

```let x: Int

let y: Int

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

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

///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
}
```

}

// 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 strength: 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: strength)
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
}
}
```

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

```override func fire(at enemy: Enemy) {
while enemy.life >= 0 {
enemy.decreaseLife(by: strength)
}
print("Enemy destroyed!")
}
```

}

let laserTower = LaserTower(x: 2, y: 2)

let enemy = Enemy(x: 1, y: 1)

let tower = Tower(x: 0, y: 0)

let superEnemy = SuperEnemy (x:1, y: 1)

superEnemy.life

tower.fire(at: enemy)

tower.fire(at: superEnemy)

laserTower.fire(at: superEnemy)