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

2015/1/11

Actor not in world? How can I fix it? :)

Pe1per Pe1per

2015/1/11

#
I have a problem! I try to programm a game for school. In my game there is a tank which can fire missles. I make a Object at the location of the missle with getWorld().addObject ( new Smoke(), getX(), getY()); The missle should also remove at the Edge of the World and when it hits a brickwall. But every time a missle hits a brickwall theres an error and I don't know why! But the error is in contact with the AtWorldEdge method Error: 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:681) at greenfoot.Actor.getX(Actor.java:157) at schuss1.AtWorldEdge(schuss1.java:33) at schuss1.act(schuss1.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private void makeSmoke() //setzt Rauch an Position von Schuss
   {      
       count--;
        
       if(count == 0) { //wenn count null ist, sonst ist zuviel Rauch da
          getWorld().addObject ( new Smoke(), getX(), getY());
          count = 2; // setzt count wieder auf zwei
       }
   }
    
   private void AtWorldEdge()
   {
       if(getX() < 10 || getX() > 1590 || getY() < 10 || getY() > 790)
       {
           getWorld().removeObject(this);
           count = 2;
       }
   }
    
    private void checkWall()
   {
       Actor brickwall = getOneIntersectingObject(brickwall.class);
       if (brickwall != null)
       {
           getWorld().removeObject(brickwall);
           getWorld().removeObject(this);
           Greenfoot.playSound("hit.wav");
           count = 2;
       }
   }
Pe1per Pe1per

2015/1/11

#
Sorry for my English :) it's not the best
danpost danpost

2015/1/11

#
You have two methods that could potentially remove the actor from the world, the 'checkWall' and the 'AtWorldEdge' methods. Regardless of the order they are called from the act method, if the first one does remove the actor from the world, the second one will fail because both methods require the actor be in the world (the 'checkWall' method starts with a 'getOneIntersectingObject' method call which requires the actor be in the world and the 'AtWorldEdge' method starts with a 'getX' call which also requires the actor be in the world). To avoid the error, you must ensure the actor is still in the world before calling the second of the two methods. So, in the act method:
1
2
3
4
5
6
// after
checkWall();
// insert
if (getWorld() == null) return;
// before
AtWorldEdge();
Pe1per Pe1per

2015/1/11

#
Ok thank you :) Now I have another problem. I have created enemies and I don't know how I can set the Rotation of them to the Player. So that the enemys move in the direction of the player. But the player can drive around so the target for the enemies is always different.
danpost danpost

2015/1/11

#
Since the player will "continuously" move and therefore "continuously" change the target location, the code will need to be placed in the act method of the enemies. If you "always" want them to turn toward the player, you should either use the World instance 'getObjects' method to get the player object or use the Actor instance 'getObjectsInRange' method using a very large range. Whichever way you go, make sure an object is returned in the list before trying to get the object and getting its location coordinates to turn toward.
You need to login to post a reply.