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

2011/6/26

Objects returned by getObjects don't have methods/variables?

thundercracker thundercracker

2011/6/26

#
I am trying to get all of the objects in the world that are an instance of my "PowerSource" class, and then move them. However, when I use getObjects(PowerSource.class), all the powerSource objects returned don't seem to have any methods or variable, as even if I try to use nodes.get(0).getX(), I get a compile error saying 'cannot find symbol - method getX()' What am I doing wrong?
Busch2207 Busch2207

2011/6/26

#
I think this might be the easiest solution:
List<PowerSource> PSList = getObjects(PowerSource.class);
for(PowerSource PS : PSList)
{
PS.setLocation(PS.getX(),PS.getY());
}
The "for" let Greenfoot go throw the List and call one Object after the other "PS". Then you can write in the {} - Brickets, what Greenfoot has to do and you can use the Objects with the name "PS". and if you only want to get the first Object in the list you write:
List<PowerSource> PSList = getObjects(PowerSource.class);
PSList.get(0).setLocation(PSList.get(0).getX(),PSList.get(0).getY());
or
List<PowerSource> PSList = getObjects(PowerSource.class);
PowerSource PS = PSList.get(0);
PS.setLocation(PS.getX(),PS.getY());
Henrik00 Henrik00

2012/10/3

#
I think you might have to use the getY() to, not just get() if that's what you did. so i think it should look like this: (getX(), getY()); But I'm no expert, if that's not right I have no idea actually.
danpost danpost

2012/10/3

#
Here is what was happening. 'getObjects(PowerSource.class)' returns a list of objects of type 'Object'. Objects of that type cannot use 'Actor' class methods or variables. A simple way without having to import the list class
for (Object obj : getObjects(PowerSource.class))
{
    PowerSource ps = (PowerSource) obj; // sub-casting
    // change location of 'ps'
}
Hope this makes sense.
You need to login to post a reply.