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

2015/1/5

Actor not in world! Please Help

SomeRandomStuff SomeRandomStuff

2015/1/5

#
When I play my game I always am getting this message if I hit a ship
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.getY(Actor.java:170)
	at BritishShip.act(BritishShip.java:19)
	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)
Here is the source code for the ship
    public void act() 
    {
      setLocation(getX(), getY()+1);
      checkColision();
      if( getY()>=getWorld().getHeight()-0)
      {
          getWorld().removeObject(this);
      }
    }        
    private void checkColision()
    {
        Actor a = getOneIntersectingObject(CannonBall.class);
        if (a != null)
        {
            Scene world = (Scene) getWorld();
            world.removeObject(this);
        }
    }
       
Can you help me as soon as possible? Thanks
xFabi xFabi

2015/1/5

#
Since your Collision Method is called before the if Method, your Ship is deleted before the if Method. It then Tries to Get the ships y and x coordinates, which it can't because there's no ship => error Try using if(getWorld().getObjects() != null) in front of your other if method (probably written wrong, I just know there's a method like this, Dan may post a correct solution :) )
SomeRandomStuff SomeRandomStuff

2015/1/5

#
Dosent work, here is what I put
private void checkColision()
    {
     Actor a = getOneIntersectingObject(CannonBall.class);
     if(getWorld().getObjects() != null)
     if (a != null)
       {
            Scene world = (Scene) getWorld();
            world.removeObject(this);
       }
    }
xFabi xFabi

2015/1/5

#
Firstly, there's a { missing for the first if Second, as i said, I don't exactly know how to use the methods And third, can you please share the error with us ^_^ Edit: I think you also need to put a ';' behind the getObjects() but I'm not sure..
SomeRandomStuff SomeRandomStuff

2015/1/5

#
ok here
method getObjects in class greenfoot.World cannot be applied to given types;
required:java.lang.Class;
found: no arguments;
reason: actual and formal argument lists differ in length
this is the error
xFabi xFabi

2015/1/5

#
Try this If(!getWorld().getObjects(classname.class).isEmpty) { If... } Replace classname with the name of your Ship's class
danpost danpost

2015/1/5

#
From your original post code, line 4 calls a method that could potentially remove the actor from the world. If it does, then 'getY' in line 5 must fail because there is no world to get a y coordinate from. You must ensure the actor is still in the world before executing 'getY' on line 5. The proper conditional check would be 'if (getWorld() != null)'. 'getWorld' is an Actor object method that returns the world the actor is in (or 'null' if not currently in a world).
SomeRandomStuff SomeRandomStuff

2015/1/7

#
danpost wrote...
From your original post code, line 4 calls a method that could potentially remove the actor from the world. If it does, then 'getY' in line 5 must fail because there is no world to get a y coordinate from. You must ensure the actor is still in the world before executing 'getY' on line 5. The proper conditional check would be 'if (getWorld() != null)'. 'getWorld' is an Actor object method that returns the world the actor is in (or 'null' if not currently in a world).
Thank you danpost! Everything is now working! I appreciate you helping me!
You need to login to post a reply.