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

2013/4/9

Actor Not In World Error

sametguzelgun sametguzelgun

2013/4/9

#
I write the following code to create an object called Zombie: import greenfoot.*;
public class Zombie extends Actor
{
     public Zombie ()
     {
         setImage ("zombi.png");
     }
     public void act ()
     {
         SWAT SWAT SWAT = new ();
         turnTowards (swat.getX (), swat.getY ());
     }
}
but I get the 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: 663) at greenfoot.Actor.getX (Actor.java: 157) at Zombi.act (Zombi.java: 11) at greenfoot.core.Simulation.actActor (Simulation.java: 565) at greenfoot.core.Simulation.runOneLoop (Simulation.java: 523) at greenfoot.core.Simulation.runContent (Simulation.java: 213) at greenfoot.core.Simulation.run (Simulation.java: 203) What do you think the reason for this, and how can I fix it?
JetLennit JetLennit

2013/4/9

#
i think it is because you either dont have a swat in the world.. or you destroyed one and then the variable/method equals nothing due to having no x or y
JetLennit JetLennit

2013/4/9

#
now to fix it could you post the SWAT code?
sametguzelgun sametguzelgun

2013/4/9

#
JetLennit wrote...
i think it is because you either dont have a swat in the world.. or you destroyed one and then the variable/method equals nothing due to having no x or y
No, SWAT object despite the fact that the world is a mistake. That's the part I really perplexed.
sametguzelgun sametguzelgun

2013/4/9

#
JetLennit wrote...
now to fix it could you post the SWAT code?
import greenfoot.*;
public class SWAT extends Actor
{
    public SWAT()
    {
        setImage("swat.png");
    }
    public void act()
    {
        MouseInfo fare = Greenfoot.getMouseInfo();
        int x = fare.getX();
        int y = fare.getY();
        turnTowards(x,y);
        hareket();
    }
    private void hareket()
    {
        if(Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w"))
        {
            move(2);
        }
        if(Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s"))
        {
            move(-2);
        }
        if(Greenfoot.mousePressed(null))
        {
            Greenfoot.playSound("gunshot.wav");
            Mermi mermi = new Mermi();
            mermi.setRotation(getRotation());
            getWorld().addObject(mermi,getX(),getY());
        }
    }
    public int getObjectX()
    {
        return getX();
    }
    public int getObjectY()
    {
        return getY();
    }
}
JetLennit JetLennit

2013/4/9

#
int x/y = fare.getX/Y();  
shouldn't work because getX() and getY() cant be accessed in a static context
sametguzelgun sametguzelgun

2013/4/9

#
JetLennit wrote...
int x/y = fare.getX/Y();  
shouldn't work because getX() and getY() cant be accessed in a static context
I do not understand, there's x and y coordinates of the mouse. My problem is the coordinates of the object is required.
sametguzelgun sametguzelgun

2013/4/9

#
this is an open source game: http://www.greenfoot.org/scenarios/7980
Gevater_Tod4711 Gevater_Tod4711

2013/4/9

#
The problem is in the first code you posted. First in line 9: SWAT SWAT SWAT = new (); This should be SWAT swat = new SWAT(); but that's not the cause of the exception. The problem is that you try to get the location of the swat object in the world. But you have never added it into any world. The swat object is created but never added to the world and so it has got no location and you can't call getX() or getY(); If you first add the object to the world you can get it's location. But only after adding it to the world.
sametguzelgun sametguzelgun

2013/4/9

#
Gevater_Tod4711 wrote...
The problem is in the first code you posted. First in line 9: SWAT SWAT SWAT = new (); This should be SWAT swat = new SWAT(); but that's not the cause of the exception. The problem is that you try to get the location of the swat object in the world. But you have never added it into any world. The swat object is created but never added to the world and so it has got no location and you can't call getX() or getY(); If you first add the object to the world you can get it's location. But only after adding it to the world.
Thank you.
You need to login to post a reply.