Thank you for the quick response and i have that exact code in another Actors code, but it wont let me access it for some reason. I'm not sure of the proper syntax for pointing to that specific actor's method or something.
Actor NewActor = getOneIntersectingObject(Other.class);
if (NewActor != null)
{
//building off your method this is what i would have.
if ( NewActor.getNumber() == 2 )
{
//code
}
}
I would guess that its because it's a refrence to an Actor object. The compilor thinks that it can only use methods in the Actor class, and the Actor class has no getNumber() method. You have to use a more specific reference variable that pertains to the class you wrote that has that particular method.
Well I was re-reading my origonal post and I guess I could be more clear. Basically, whenever you have a variable that refrences an object, like Actor NewActor; the compilor thinks that NewActor is an Actor Object, and will only allow you to call Actor methods on it. Now, if the method is overridden (like act() for example), the Java Virtual Machine will call the correct act method, (the one from the subclass), so you don't have to worry about the program calling the correct overridden method, but what you can't do is call any methods that Actor doesn't have with that NewActor in your code or it will confuse the compilor. We can call act() because Actor has an act() method, but we can't call getNumber() because Actor doesn't have it. So that's why NewActor.getNumber() won't work. Now, if you are absolutely sure you know what kind of object NewActor is going to be at runtime, you can cast it to the correct class in your code. In this case we know what kind of object we will be refrenceing, so even though the method getOneIntersectingObject() can return anything that is an Actor, we know exactly what subtype it's going to be so we can cast it to the correct type so the compilor knows too. For example, say your class that has the getNumber method is called "SomeClass" you can say SomeClass NewActor = (SomeClass) getOneIntersectingObject(SomeClass.class); just like in mjrb4's example. So basically the method returns an Actor object, except we know it's really an object of "SomeClass", so we use the (SomeClass) cast to tell the complior that it really is an object of "SomeClass". Now that the compilor knows that NewActor is really an instance of "SomeClass" it will allow you to call the getNumber() method. There is a little bit more I can say about casting, not much really, but this should be enough for you to do whatever you want to do for now.