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

2017/3/10

Trying to get actor location before object has been created error

Dylex Dylex

2017/3/10

#
Hi, i've implemented my own x and y variables for my acts as doubles, they need to use getX() and getY() when the actor is crated in order to place him in the right spot. I wouldn't think this would be a problem but unfortunately i am getting the "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." error. I could put parameters inside the actors constructor but i was wondering if there is a better way. Here is the code causing trouble: public Player() { GreenfootImage image = getImage(); image.scale(64, 64); setImage(image); x = getX(); y = getY(); }
Super_Hippo Super_Hippo

2017/3/10

#
Try to use this:
protected void addedToWorld(World w)
{
    x = getX();
    y = getY();
}
The addedToWorld method is automatically executed once after the object was added to the world. The constructor on the other side is executed when the object is created which is before it is added to the world, so it can't get its location at this point. You created this discussion twice. Click on "Destroy" at the other one.
You need to login to post a reply.