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

2017/6/2

Code for getting an object on the screen?

Alola_Sandslash Alola_Sandslash

2017/6/2

#
What is the code for getting the only object of its kind on the screen? Similar to getOneObjectAtOffset but like anywhere on the screen.
danpost danpost

2017/6/2

#
Alola_Sandslash wrote...
What is the code for getting the only object of its kind on the screen? Similar to getOneObjectAtOffset but like anywhere on the screen.
For in a World subclass, like from act method:
ActorClassName obj = (ActorClassName) getObjects(ActorClassName.class).get(0);
For in an Actor subclass, like from act method:
ActorClassName obj = (ActorClassName) getWorld().getObjects(ActorClassName.class).get(0);
If there is a possibility that that type of actor is not in the world, then the following check needs to be made first:
if (getObjects(ActorClassName.class).size() == 0)
// or
if ( ! getObjects(ActorClassName.class).isEmpty() )
to ensure that the list returned contains the actor.
Alola_Sandslash Alola_Sandslash

2017/6/2

#
It cannot find the getObjects method...
danpost danpost

2017/6/2

#
Alola_Sandslash wrote...
It cannot find the getObjects method...
Then, you are probably in an Actor subclass and need to use 'getWorld().getObjects...'.
You need to login to post a reply.