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

2012/4/13

Actor not in World????

PStigerID PStigerID

2012/4/13

#
I dont understand how to fix this 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:656)
	at greenfoot.Actor.getX(Actor.java:157)
	at underMouse.act(underMouse.java:15)
	at greenfoot.core.Simulation.actActor(Simulation.java:507)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
	at greenfoot.core.Simulation.runContent(Simulation.java:204)
	at greenfoot.core.Simulation.run(Simulation.java:194)
Because my World Classes says:
        addObject(new mouse(), 100, 100);
        addObject(new underMouse(),111,92);
OK - this is the code with causes the error: (Class called "underMouse"
    public void act() 
    {
           mouse m = new mouse();
            int x = m.getX();
            int y = m.getY();
            int r = m.getRotation();
            if (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("s")) {
                turnTo(r);
                moveTo(x+11,y-8); }
    }    
"mouse" code:
    public void act() 
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse != null)
                setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));
            if (Greenfoot.isKeyDown("w"))
                {
                    turnTo(getRotation());
                    move(2);
                }
            if (Greenfoot.isKeyDown("s"))
                {
                    turnTo(getRotation());
                    move(-2);
                }
            
        }    
Please help!!! Thanks! PStiger
davmac davmac

2012/4/13

#
Ok, first, the convention in Java is to use capitals at the beginning of class names. So, "underMouse" should be "UnderMouse" and "mouse" should be "Mouse". It's confusing for experienced programmers who read your code when you break that convention. Now: the problem is you're creating a new 'mouse' actor:
       mouse m = new mouse();  
... which isn't in the world. Then you are asking for its position (via getX(), getY()). Because it's not in the world, it doesn't have a position, which is why you get the error. I'm not clear on exactly what you were trying to do, so I can't tell you the best way to fix it.
PStigerID PStigerID

2012/4/13

#
Hi Davmac, Thanks for your reply. I need 2 objects on top of each other. Since they are not the same size I thought I could do it this way. Could you show me a different way? Thanks
PStigerID PStigerID

2012/4/13

#
Nevermind I got it!
NameOfWorld World = (NameOfWorld) getWorld();
            mouse m = World.getMouse();
            int x = m.getX();
            int y = m.getY();
            int r = m.getRotation();
and in the World:
        theMouse = new mouse();
        addObject(theMouse,100,100);
and
    public mouse getMouse()
    {
        return theMouse;
    }
You need to login to post a reply.