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

2011/11/17

Help please

jmlisowe jmlisowe

2011/11/17

#
How do I access another actor's private variable from within another actor?
bourne bourne

2011/11/17

#
By providing an accessor method that returns it. For example: private int number = 2; public int getNumber() { return number; }
jmlisowe jmlisowe

2011/11/17

#
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 } }
jmlisowe jmlisowe

2011/11/17

#
I guess my question is how do I access the intersecting object's variable?
AwesomeNameGuy AwesomeNameGuy

2011/11/17

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

2011/11/18

#
The above reply is correct - instead of:
Actor NewActor = getOneIntersectingObject(Other.class);
You want something like:
Other NewActor = (Other)getOneIntersectingObject(Other.class);
...which will let you access the methods on "Other" that you've declared.
AwesomeNameGuy AwesomeNameGuy

2011/11/18

#
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.
You need to login to post a reply.