This site requires JavaScript, please enable it in your browser!
Greenfoot back
dandrei279
dandrei279 wrote ...

2017/1/13

How to create a method that takes as parameter any object?

dandrei279 dandrei279

2017/1/13

#
Hello everyone! I'm working on a project and I must know if an object is in the proximity of another one. I think about creating a method that takes as parameter an object and returns `true` or `false` but I don't know exactly how to do that.
Super_Hippo Super_Hippo

2017/1/13

#
I am not sure if you really want/need to use two objects here. Would be helpful if you could describe your project. For example if you have an animal which begins to hunt another one (two different classes) if it is in a certain range, it should be enough to use the 'getObjectsInRange' method.
dandrei279 dandrei279

2017/1/13

#
It's about a rocket that flies through space. I need to know if it is near Moon or near Mars and so on.
Super_Hippo Super_Hippo

2017/1/13

#
In this case, you can use the 'getObjectsInRange' methods. I guess you could create a superclass for Moon, Mars, ... (maybe call it 'SpaceObjects' or whatever you like). Then you can use this class as the parameter in the 'getObjectsInRange' method. The method returns a list. If you only want to know if one of those objects is in a certain range, you can check if the list is empty or not. If you want to react to them (like not flying into it or landing on it), you can use a for-each on the list.
dandrei279 dandrei279

2017/1/13

#
Thanks! I'm going to try this.
You need to login to post a reply.