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

2019/12/19

trying to get the X and Y of every object

Yodats Yodats

2019/12/19

#
   for (int s = 0;s + 1 > getObjects(null).size();s++){
           String name = getObjects(null).get(s).getClass().getCanonicalName();
           String saver = name + " x: "+ "y: ";
        }
so, I'm trying to get all my objects, and make a string of each like: "wall x: 1 y: 2" but I can't get the X of an object I have gotten with getObjects(null). same for Y of course
danpost danpost

2019/12/19

#
Firstly, the loop will never work. If no actors are in world, then no looping will take place. If any number of actors are in the world, the loop is broken out of immediately by the conditional expression (with s starting at zero, s+1, or 1 will be less than or equal to number of actors in world). Secondly, I do not see any code that attempts to acquire the x and y values of any actor's location.
Yodats Yodats

2019/12/19

#
for (int s = 0; s + 1 <= getObjects(null).size(); s++){
           String name = getObjects(null).get(s).getClass().getCanonicalName();
           String saver = name + " x:"+ getObjects(null).get(s).getX()+  "y: "+ getObjects(null).get(s).getY();
so, the loop starts now and I added in my failed attempts to get the coordinates, but they don't work. And sorry if I'm bad at coding, I'm pretty much a beginner. I also want to add that I think the problem is that looking for objects in class null just don't have the properties of an actor
danpost danpost

2019/12/19

#
Yodats wrote...
<< Code Omitted >> so, the loop starts now and I added in my failed attempts to get the coordinates, but they don't work. And sorry if I'm bad at coding, I'm pretty much a beginner. I also want to add that I think the problem is that looking for objects in class null just don't have the properties of an actor
You should only use getObjects once in this snippet. You can assign the returned list to a List variable, or even better, use the for-each loop structure:
for (Object obj : getObjects(null))
As far as the properties of the objects in the returned list, they are there; however, it is not known to look in the Actor class for the getX and getY methods. To have these methods be found, the objects need to be cast as of type Actor (via typecasting):
Actor actor = (Actor) obj;
After executing this line, you can use 'actor.getX()' and 'actor.getY()', as well as 'actor.getClass().getCanonicalName()'.
Yodats Yodats

2019/12/19

#
Thank you really much danpost
You need to login to post a reply.