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

2011/12/4

Actor not in world?

raniye raniye

2011/12/4

#
I get an error that my actor is not in the world so it can't use locations. But in my world I have several codes for my Actor to have it in my world...: this(new haai()); // 'haai' is the name of my Actor addObject(new haai(), 10, 40) Is there anything else I should put in my world class to get my Actor IN the world so I can use location thingies.
kiarocks kiarocks

2011/12/4

#
why do you use the this(new haai()); in your world? addObject(new haai(), 10, 40) should do it.
Builderboy2005 Builderboy2005

2011/12/4

#
Just a note; haai is *not* the name of your Actor, haai is the name of your Class. And like kiarocks said, the first line you posted has nothing to do with the second line. The second line will crease a new object and insert it into the world. I'm nor quite sure what the first line is doing o.O
davmac davmac

2011/12/4

#
We need to see more of your code to help with this. In particular, what line of code is indicated by the stack trace? (Can you post the full stack trace?)
kiarocks kiarocks

2011/12/4

#
I looked at his code, he has a constructor like this:
/**
     * Default constructor. If we don't receive a game character, create one.
     */
    public level2()
    {    
        this(new haai());
    }

    /**
     * Constructor used during play - the game character is passed in.
     */
    public level2(haai Haai)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(60, 60, 10); 
        plaatsCoins();
        blokken();
        haaiTonen();
        scoreboard = new Score();
        addObject(scoreboard, 10, 1);
    }
kiarocks kiarocks

2011/12/4

#
This is the method the problem is in:
private void checkNextLevel()
    {

        if (getX() == 57 && getY() == 30)
        {
            if (level == 1) {
                level = 2;

                getWorld().removeObject(this);
                Greenfoot.setWorld(new level2(this));
            }
            else {
                level = 1;

                getWorld().removeObject(this);
                Greenfoot.setWorld(new wereld(this));
            }
        }
    }
And this is the fixed method:
private void checkNextLevel()
    {
        if (getX() == 57 && getY() == 30)
        {
            if (level == 1) {
                level = 2;
                Greenfoot.setWorld(new level2(this));
                getWorld().removeObject(this);

            }
            else {
                level = 1;
                Greenfoot.setWorld(new wereld(this));
                getWorld().removeObject(this);

            }
        }
    }
kiarocks kiarocks

2011/12/4

#
The problem is, you removed the object before it could set the new world and give its location.
raniye raniye

2011/12/4

#
Thanks for the reply, I've tested it but I still got the same error. When I was running it and I went to the spot to go to the next level the Greenfoot: Terminal Window popped up and said: "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." :-(
davmac davmac

2011/12/4

#
ranlye, when it says that it will also give a list of source code files and line numbers. This list is called a stack trace and is really important for figuring out why your code isn't working. So, again: can you post the stack trace please?
kiarocks kiarocks

2011/12/5

#
you tried pasting
private void checkNextLevel()  
    {  
        if (getX() == 57 && getY() == 30)  
        {  
            if (level == 1) {  
                level = 2;  
                Greenfoot.setWorld(new level2(this));  
                getWorld().removeObject(this);  
  
            }  
            else {  
                level = 1;  
                Greenfoot.setWorld(new wereld(this));  
                getWorld().removeObject(this);  
  
            }  
        }  
    }  
over the old method?
kiarocks kiarocks

2011/12/5

#
@davmac, this is what i get for the same program:
//Stack Trace
//
//
//
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.getOneObjectAtOffset(Actor.java:860)
	at haai.blokVoor(haai.java:140)
	at haai.moveRight(haai.java:57)
	at haai.bewegen(haai.java:43)
	at haai.<init>(haai.java:21)
	at level2.haaiTonen(level2.java:119)
	at level2.<init>(level2.java:34)
	at haai.checkNextLevel(haai.java:229)
	at haai.act(haai.java:32)
	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)
danpost danpost

2011/12/5

#
Looks to me like the error is coming from the second time it goes through the method after the world has changed. The first ime it remoes the actor, the second time it tries to get its location coordinates, but the actor isn't there anymore. You might want to give the actor a boolean variable to track whether the actor is currently in a world or not,, and use something like 'if (inAWorld) checkNextLevel;'. Of course it would be set to false in both parts of the checkNextLevel() method, and set to true when the actor is added back into the world (probaly needs to be a public variable).
kiarocks kiarocks

2011/12/5

#
That's why this method works, right?
private void checkNextLevel()    
    {    
        if (getX() == 57 && getY() == 30)    
        {    
            if (level == 1) {    
                level = 2;    
                Greenfoot.setWorld(new level2(this));    
                getWorld().removeObject(this);    
    
            }    
            else {    
                level = 1;    
                Greenfoot.setWorld(new wereld(this));    
                getWorld().removeObject(this);    
    
            }    
        }    
    }  
danpost danpost

2011/12/5

#
I am not totally sure that is right either. It appears that the 'removeObject(this)' part would be in reference to the NEW world (since it is NOW set). I would set the Actor to a reference variable, next remove the Actor, then instantiate the new world (sending the reference variable for the Actor).
raniye raniye

2011/12/5

#
Ok found the problem wasn't in the Actor 'haai' class at all. It was in my level2 World, needed to switch some lines. Now I am in my level2 World but it doesn't load my elements my Actor haai, the Actor 'zand' who is used as a wall and the Actor 'coin' which is used as coins.
You need to login to post a reply.