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

2021/11/22

How to call a Actor in world

mik. mik.

2021/11/22

#
How am i supposed to call Actor in world. In need to do some changes in thier image so i need to call it. I wrote Actor actor = new Actor(); and Actor actor=getObjects(Actor.class); But none of them workd
Gabe1098 Gabe1098

2021/11/22

#
Maybe
Actor actor = getWorld().getObjects(Actor.class);
danpost danpost

2021/11/22

#
mik. wrote...
How am i supposed to call Actor in world. In need to do some changes in thier image so i need to call it.
Try:
Actor actor = (Aktor)getWorld().getObjects(Aktor.class).get(0);
where Aktor is the name of the class extending Actor. Note that getObjects returns a List object -- not an Actor object, although the list will contain, if any exists, Actor objects. If there is a chance none of that type actor are in the world, you will need to make sure that the list returned is not empty before getting the first (0th) element from the list.
mik. mik.

2021/11/23

#
Thanks to both but how will i call every class in world?
danpost danpost

2021/11/23

#
mik. wrote...
how will i call every class in world?
If you want a list of all actors in the world:
java.util.List<Actor> actors =
    (java.util.List<Actor>)getWorld().getObjects(Actor.class);
However, there is usually no need to use this line, in particular. To what end would calling every class achieve?
mik. mik.

2021/11/23

#
java.util.List<Actor> actors =(java.util.List<Actor>)getObjects(Actor.class);

GreenfootImage img =new GreenfootImage(actors.getImage());
It's giving me error
danpost danpost

2021/11/23

#
If you want to change the images of all actors in your world:
for (Object obj : getObjects(Actor.class))
{
    GreenfootImage img = ((Actor)obj).getImage();
    // do whatever to image here
}
You need to login to post a reply.