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

2018/12/12

Identify an actor

misg misg

2018/12/12

#
Hi all, I am a newby in Greenwood! Just want to ask how I can identify an actor? Let us assume there are 4 actors of the same in the world. How can I know and identify what is the 1,2,3 and the 4th of them? Is there a chance to have an activity if the actor is the actor number 2? Thanks! Michael
danpost danpost

2018/12/12

#
misg wrote...
how I can identify an actor? Let us assume there are 4 actors of the same in the world. How can I know and identify what is the 1,2,3 and the 4th of them? Is there a chance to have an activity if the actor is the actor number 2?
You could list them or give each a unique state; otherwise they would not be essentially identifiable. There may still be a way that could work; but it is not guaranteed to work in future versions of greenfoot. An undocumented behavior in the way greenfoot lists the actors that are placed into a world may be used to identify a specific actor. That is:
1
java.util.List actors = getObjects(Some.class);
currently lists the actors of type Some in the order in which they were added into the world. So,
1
((Some)getObjects(Some.class).get(1)).doActivity();
would have the second Some object added into the world do an activity.
You need to login to post a reply.