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

2024/10/23

How do I find if a certain class is located at a certain location?

nubnt_ nubnt_

2024/10/23

#
Saw an example of this in a tutorial but I forgot how to write it.
danpost danpost

2024/10/23

#
nubnt_ wrote...
How do I find if a certain class is located at a certain location?
From within your subclass of World, you can use the getObjectsAt(int x, int y, Class clss) method. This will return a list of actors of given type at that location. If you don't need the actor itself, just checking if the list is empty is enough:
if (getObjectsAt(getWidth()/2, getHeight()/2, Information.class).isEmpty()) ...
otherwise:
java.util.List<Information> list = getObjectsAt(getWidth()/2, getHeight()/2), Information.class); // getting list of actors of given type at location
if ( ! list.isEmpty() ) {
    Information info = list.get(0); // getting a reference to the actor
    ...
You need to login to post a reply.