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

2017/12/27

Return Actor Name/Count

xbLank xbLank

2017/12/27

#
No context needed. Can you return an Actors index? Say we have an Actor called "Sheep". I have multiple Sheep in my World. If I click any of those it will return a String like
1
"I'm sheep number " + this.getName() or this.getCount()
I know that you could create another Actor that keeps track of that but there must be a super simple way that I am currently missing out. Appreciate your help.
danpost danpost

2017/12/27

#
I am sure this can be done in multiple ways. Probably easiest for you is to have a "new Sheep counter" field and a "create new sheep" method in your World subclass. Have the method create the sheep, passing the counter value to the new sheep, then increment the counter and return the new Sheep to the calling statement. That is:
1
2
3
4
5
6
7
8
9
10
11
12
// with field
private int sheepCounter;
 
// using in prepare method and anywhere else you create Sheep objects
Sheep sheep = createNewSheep();
// (or just replacing "new Sheep()" with "createNewSheep()" everywhere)
 
// to call this method
private Sheep createNewSheep()
{
    return new Sheep(sheepCounter++);
}
In Sheep class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// add  field
private int identificationNumber;
 
// change constructor to (or add)
public Sheep(int idNum)
{
    identificationNumber = idNum;
}
 
// add method to return id
public int getID()
{
    return identificationNumber;
}
xbLank xbLank

2017/12/27

#
That's how I was going to do it if there was no other way. I just hoped that Greenfoot had something like that implemented already. Sadly they dont. Well I guess I have to use the old fashion way then. Thanks anyways.
danpost danpost

2017/12/27

#
일리아스 wrote...
That's how I was going to do it if there was no other way. I just hoped that Greenfoot had something like that implemented already. Sadly they dont. Well I guess I have to use the old fashion way then. Thanks anyways.
You could use the 'toString' method on the objects. Maybe that would be sufficient for your needs:
1
"I'm sheep number " + this.toString()
xbLank xbLank

2017/12/27

#
You just gave me a great idea. Thank you, sir. I will post my results in the end.
xbLank xbLank

2017/12/27

#
Hope this is useful for some people.
1
2
3
4
5
6
7
8
9
private int getActorCount()
{
    for(int i = 0; i < getWorld().getObjects(<Actor>.class).size();i++)
    {
        if(this.toString().equals(getWorld().getObjects(<Actor>.class).get(i).toString()))
             return i+1;
    }
    return 0;
}
danpost danpost

2017/12/27

#
일리아스 wrote...
< Code Omitted >
You do not have to do all that. This should work just fine:
1
int ident = this.getWorld().getObjects(this.getClass()).indexOf(this)+1;
The right side is equivalent to calling your 'getActorCount' method. The above can be simplified to:
1
int ident = getWorld().getObjects(getClass()).indexOf(this)+1;
xbLank xbLank

2017/12/27

#
This is literally what I was asking for. Thank you.
You need to login to post a reply.