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

2016/5/15

putting all actors in an array

Psychpsyo Psychpsyo

2016/5/15

#
I basically want to save all actors of a class in one array and remove them.
danpost danpost

2016/5/15

#
Psychpsyo wrote...
I basically want to save all actors of a class in one array and remove them.
From the world class, you can use the 'getObjects(Class clss)' method to list those actors. A list is like an array and there are methods available to convert between the two.
Psychpsyo Psychpsyo

2016/5/15

#
danpost wrote...
Psychpsyo wrote...
I basically want to save all actors of a class in one array and remove them.
From the world class, you can use the 'getObjects(Class clss)' method to list those actors. A list is like an array and there are methods available to convert between the two.
Thanks but what are the methods to convert lists to arrays?
danpost danpost

2016/5/16

#
Psychpsyo wrote...
Thanks but what are the methods to convert lists to arrays?
1
2
3
// a list of actors of type SomeActorClass
java.util.ArrayList<SomeActorClass> actors = (java.util.ArrayList<SomeActorClass>)getObjects(SomeActorClass.class);
SomeActorClass[] someActors = actors.toArray(new SomeActorClass[0]);
or, more simply:
1
SomeActorClass[] someActors = ((java.util.ArrayList<SomeActorClass>)getObjects(SomeActorClass.class)).toArray(new SomeActorClass[0]);
This will not look so bulky if you import 'java.util.ArrayList' and shorten the class name -- for example, using Actor type to list all actors in world:
1
Actor[] actors = ((ArrayList<Actor>)getObjects(null)).toArray(new Actor[0]);
Psychpsyo Psychpsyo

2016/5/16

#
Thanks!
You need to login to post a reply.