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

2014/1/18

how to return one actor the other's x and y/rotation

deathburrito16 deathburrito16

2014/1/18

#
1 last question. then I'll leave you alone.
danpost danpost

2014/1/18

#
Get a reference to that actor you want the info from and call the 'get' methods that return that info on that actor. It only be cast as actor, so if you get that reference by way of an Actor class method you should not need to typecast the returned object (actor); otherwise (if you use a World class method), you will need type typecast the returned object at least as an Actor object. For example: ((Actor)getWorld().getObjects(ActorClassName.class).get(0)).methodName(); 'getWorld().getObjects(...).get(0)' returns an object, not an Actor, so you could not call Actor class method on it directly. NOTE: the 'get(0)' call will fail if not actors of that type are in the world. Typecasting it with '(Actor)...' tells the system that it is indeed an Actor object and Actor class method can be then used on the object.
deathburrito16 deathburrito16

2014/1/20

#
whoa whoa whoa. slow down. so, the example of the code you gave me, that would go in the class that I want to get the info, right? and what would I put for methodName()?
danpost danpost

2014/1/20

#
deathburrito16 wrote...
whoa whoa whoa. slow down. so, the example of the code you gave me, that would go in the class that I want to get the info, right? and what would I put for methodName()?
The name of the method you are calling in the ActorClassName class for that ActorClassName object.
deathburrito16 deathburrito16

2014/1/20

#
I tryed this, and it worked, but like you said, it didn't work when there was no object of the class in the world. Is there a way to check if it is not null first?
danpost danpost

2014/1/21

#
if (getWorld().getObjects(ActorClassName.class).size() > 0) or if (!getWorld().getObjects(ActorClassName.class).isEmpty()) They both will return true if there are any actors of that class in the world; however, the second one is more efficient in that it will not count all the objects of that type, but immediately return as soon as one is found (if any are).
deathburrito16 deathburrito16

2014/1/21

#
thanks. that helped lot.
You need to login to post a reply.