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

2020/12/12

getWorld and getObject

Patdumas77 Patdumas77

2020/12/12

#
Im trying to figure out how to use getWorld and getObject, to reference from one class to another. I dont know if they are supposed to placed in a particular place to be used such as in act method or before act method. Please give an example to reference a class using these two api.
19598 19598

2020/12/18

#
getWorld() must be called from the actor class, and returns a World object. You can use it to do things like getWorld().getBackground() or getWorld().stop(). The getObjects return an Actor object, so you could do something like getWorld().getObjects() to get a List of Actor objects, and then do get() on the object you want from that list. It would look something like this: getWorld().getObjects().get(0).
danpost danpost

2021/1/6

#
19598 wrote...
getWorld().stop()
stop is not a method in the World class and cannot be called on a World object.
The getObjects return an Actor object
It returns a java.util.List object (as you noted in your next sentence).
so you could do something like getWorld().getObjects() to get a List of Actor objects, and then do get() on the object you want from that list. It would look something like this: getWorld().getObjects().get(0).
The List object must not be empty when using get on it; and the int index of get ( get(int) ) must be less than the length of the list. That is, the size of the list, in most cases, should be checked before calling get on it.
You need to login to post a reply.