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

2014/3/27

Error message when hit worldEdge

JesperWR JesperWR

2014/3/27

#
Hello.. Iam nearly done with my game, but i got this last problem. When my Obstacle.class hits the worldEdge, it crashes. I get this message: 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.getY(Actor.java:170) at Obstacle.atWorldEdge(Obstacle.java:42) at Obstacle.act(Obstacle.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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.getY(Actor.java:170) at Obstacle.atWorldEdge(Obstacle.java:42) at Obstacle.act(Obstacle.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) My whole code for my Obstacle class:
public class Obstacle extends Actor
{
    private int vSpeed = 5; // vSpeed = 5
    private Counter counter;

    public Obstacle(Counter pointCounter)
    {
        counter = pointCounter;
    }

    public void act() 
    {
        fall();
        checkOut();
        atWorldEdge();
        checkEdge();
        //spawn();
        createObstacle();
    }       

    /**
     * Får X- og Y-koordinater + vSpeed
     */
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
    }

    /**
     * Boolean om Obstacle class er ved Y-koordinaten 499 (nederste væg)
     */
    public boolean atWorldEdge()  
    {  
        if(getY() == 499)
            return true;  
        else  
            return false;
    }

    /**
     * Hvis boolean == true, så fjerner Obstacle class
     */
    private void checkOut()    
    {    
        if (atWorldEdge() == true)    
        {    
            getWorld().removeObject(this);    
        }    
    }

    /**
     * 
     */
    public void checkEdge()
    {
        if (this.atWorldEdge() == true)
        {
            World world;
            world = getWorld();
            world.removeObject(this);
            counter.add(1);
        }
    }

    /**
     * 
     *public void spawn()
    {
    int x = Greenfoot.getRandomNumber(getWorld().getWidth() - 1);
    int y = 2;

    if (Greenfoot.getRandomNumber(1000) >= 10)
    {
    Obstacle obstacle = new Obstacle(counter);
    getWorld().addObject(new Obstacle(counter), x, y);
    }
    }*/

    private void createObstacle()
    {
        Obstacle newObstacle;
        newObstacle = new Obstacle(counter); 

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = 1;

        if (Greenfoot.getRandomNumber (1000) < 10)
        {
            world.addObject(newObstacle, x, y);
        }
    }
}
danpost danpost

2014/3/27

#
You are calling two methods from the 'act' method that do pretty much the same thing. The two methods are 'checkOut' and 'checkEdge'. The only difference between the two seems to be that the 'checkEdge' method will increment a counter. Remove the method and its call from the 'act' method that you do not want. Then, in the 'act' method, move the method call of the remaining one ('checkOut' or 'checkEdge')vto the end of the 'act' method. Also, you can remove the 'atWorldEdge' method call from your 'act' method.
JesperWR JesperWR

2014/3/27

#
Thank you! That helped a lot!! No error message anymore. :) :) When the 'Obstacle' hits the edge it disappeares, like it should, but it does not spawn more than a few then it stops and does not spawn more of 'obstacle'. I'm so glad you had the time to help me btw!! :) Code is now:
    private int vSpeed = 5; // vSpeed = 5
    private Counter counter;

    public Obstacle(Counter pointCounter)
    {
        counter = pointCounter;
    }

    public void act() 
    {
        fall();
        //spawn();
        createObstacle();
        checkEdge();
    }       

    /**
     * Får X- og Y-koordinater + vSpeed
     */
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
    }

    /**
     * Boolean om Obstacle class er ved Y-koordinaten 499 (nederste væg)
     */
    public boolean atWorldEdge()  
    {  
        if(getY() >= 499) //HER ER PROBLEMET!!!
            return true;  
        else  
            return false;
    }

    /**
     * 
     */
    public void checkEdge()
    {
        if (this.atWorldEdge() == true)
        {
            World world;
            world = getWorld();
            world.removeObject(this);
            counter.add(1);
        }
    }

    /**
     * 
     *public void spawn()
    {
    int x = Greenfoot.getRandomNumber(getWorld().getWidth() - 1);
    int y = 2;

    if (Greenfoot.getRandomNumber(1000) >= 10)
    {
    Obstacle obstacle = new Obstacle(counter);
    getWorld().addObject(new Obstacle(counter), x, y);
    }
    }*/

    private void createObstacle()
    {
        Obstacle newObstacle;
        newObstacle = new Obstacle(counter); 

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = 1;

        if (Greenfoot.getRandomNumber (1000) < 12)
        {
            world.addObject(newObstacle, x, y);
        }
    }
}
danpost danpost

2014/3/27

#
You probably just need to adjust the chance of spawning (at line 78). Either decrease the number of possibilities in the random number generated (from '1000') or increase the range of valid values (from '12') to spawn in. BTW, you may also want to put a limit on the number of Obstacle objects allowed in your world.
JesperWR JesperWR

2014/3/27

#
How would you add a limiter on 'obstacle' in the world. I've seen one of your other comments, but i could not figure it out :/
danpost danpost

2014/3/27

#
Use ' && getWorld().getObjects(Obstacle.class).size() < maxNumberOfObstaclesAllowed ' to the condition on line 78. Actually, you are needlessly creating an Obstacle every act cycle before checking if you need one at all. Your 'createObstacle' method should probably look more like this:
private void createObstacle()
{
    if (getWorld().getObjects(Obstacle.class).size() < 20 /** sample value */ && Grenfoot.getRandomNumber(1000) < 12) getWorld().addObject(new Obstacle(), Greenfoot.getRandomNumber(getWorld().getWidth())
, 1);
}
JesperWR JesperWR

2014/3/27

#
Alright danpost, thank you very much for your time :)
You need to login to post a reply.