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

2014/8/12

Making one actor find the location of multiple other actors

Arichman Arichman

2014/8/12

#
I'm making some circles play tag. I want the one who is it to find the locations of all the other players. I've done that but when I start the scenario I get a run time error printed here: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Red_Player.It(Red_Player.java:22) at Red_Player.act(Red_Player.java:8) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) The 1st, 4th, and 5th lines are red and the rest is gray. here is the code in question:
Actor green = new Green_Player();
        Actor blue = new Blue_Player();
        Actor orange = new Orange_Player();
        Actor pink = new Pink_Player();
        Actor yellow = new Yellow_Player();
        Actor purple = new Purple_Player();
        
        int redX = this.getX();
        int redY = this.getY();
        int greenX = green.getX();
        int greenY = green.getY();
        int blueX = blue.getX();
        int blueY = blue.getY();
        int orangeX = orange.getX();
        int orangeY = orange.getY();
        int pinkX = pink.getX();
        int pinkY = pink.getY();
        int yellowX = yellow.getX();
        int yellowY = yellow.getY();
        int purpleX = purple.getX();
        int purpleY = purple.getY();
The rest isn't a problem as far as I know. I know that I'm missing something. It may be a list but I'm not sure. Any help is always appreciated. And all of the referenced actors are in the world. And I use "this" because it is in the "Red_Player" class. It needs to know it's own position so it can use the distance formula between it and the other actors. I assumed that "this" would only be used for the actor that the file is for. When I tried to find the position for it the way i did for the others, I got the same error but for a different line. If you need the rest of the file I'd be happy to post it. The Green_Player, Blue_Player, etc. are blank files currently if that helps.
danpost danpost

2014/8/12

#
Arichman wrote...
And all of the referenced actors are in the world.
Are they? Your referenced actors are created on lines 2 through 6, line 7 is blank, and you are trying to get their positions in the world from line 8 through 21. Theses actors (created on lines 2 through 6) are not being added into the world before trying to get their locations in the world (lines 8 through 21). In fact, these are newly created actors and have nothing to do with any actors you already have in the world. You need to get references to those actors that are already in the world, not to new ones. Provided that the code is only run when all players are in the world, you need to get each of your references through the world with something like the following:
Actor green = (Actor) getWorld().getObjects(Green_Player.class).get(0);
This assigns the variable 'green' with the first element returned from a list of all the specified type actors in the world as an Actor object.
Arichman Arichman

2014/8/12

#
Thanks! it worked. The end product will not be running without all actors present.
You need to login to post a reply.