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 trialEddie Aguilar
2,393 PointsI don't understand why my class of tower will not work can someone please look over this and give me an explanation??
PLEASE HELP
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
}
}
1 Answer
Sarah Hurtgen
Treehouse Project ReviewerHi! I'm assuming that you have been following along with Pasan and this is just a snippet of your code? If you have struct Point and class Enemy defined exactly as Pasan did from the previous videos - I think the issue is just a typo.
In your lines
func isInRange(of enemy: Enemy) ->Bool{
let availablePositions = position.points(inrange: range)
the "r" in "inrange" needs to be capitalized:
let availablePositions = position.points(inRange: range)
If you then uncomment out your
// return false
to be
return false
It should then work as expected. If you do not have Point and Enemy defined according to the videos, I'm guessing the issue is coming from there.
Hope that helps!
Sarah Hurtgen
Treehouse Project ReviewerSarah Hurtgen
Treehouse Project ReviewerAlso, just a tip - If you find yourself getting stuck while working through the videos, keep in mind that you can download the code for the courses in the "Downloads" section of each video. Then you can compare your own code side by side to the teachers and sometimes that helps to spot any differences that could be causing issues!
Eddie Aguilar
2,393 PointsEddie Aguilar
2,393 PointsI had no idea that you could download the already written code. That was very helpful for this section. Thank you very very much