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 trialAmit Ghosh
9,314 PointsInvoking InRangeOf Function
I am trying to put this into perspective, InRangeOf Functions job is to calculate the distance between the tower _location and each invader location. We are using the DistanceTo method to get the value. What I am having problem is with the implementation of that.
InRangeOf function needs a Maplocation object and a Range to check and return true or false. The if statement written here if (_location.InRangeOf(invader location, 1)). I understand this portion of it InRangeOf(invader location, 1). But how are we checking this with tower _location in the if statement. I need to clarify this so that the concepts sink into me and I can use them when required. So please be descriptive.
_location. invokes the method but _location is not a object of the tower class. What am I missing.
Thanks, Amit.
2 Answers
Steven Parker
231,275 Points
_location
is a MapLocation object that is part of the Tower class.
I'm not sure why you would think that is is not. It's defined as a private read-only field in the class.
When you call the InRangeOf method on _location
, giving it another MapLocation object to compare to, it computes the DistanceTo that other object and returns a true/false if it is close enough.
Amit Ghosh
9,314 PointsThanks I get it. I was looking for the conventional class object= new obect concept to invoke the instance method. What I missed was _location is maplocation object and its invoking a maplocation class method. Instead of doing this in maplocation class we are doing this where needed i.e. tower class.
I will add this to my repertoire.