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

2016/10/16

Actor not in World

georgette georgette

2016/10/16

#
Hello. I'm new to Greenfoot and Java. I will appreciate any help anyone can provide. I created a lab with six actors, none of the actors are returning syntax errors, however, the message I am receiving is: "the world constructor threw an exception, see terminal window for more details". This is the message in the terminal window: "java.lang.llegalStateException. Actor not in world. An attempt was made to use the actor's location while not in the world." Sorry for the long message, below is a copy of the code I entered for the actor Robo, which Greenfoot cannot find, therefore the world is not being constructed at all. I hope this paste's well!
Super_Hippo Super_Hippo

2016/10/16

#
Post the code for your world subclass. Btw, you used a quote-tag in the end, so the code wasn't shown.
danpost danpost

2016/10/17

#
This is the code that did not display in your initial post (you used an ending quote tag instead of an ending code tag; so, it did not show).
public class Robo extends Actor
{
    /**
     * Act - do whatever the Robo wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    private Counter counter;

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

    public void act() 
    {
    }

    public void createNewBullet()
    {
    }

    public void checkKeys()
    {
    }

    {

        checkKeys();
        //isTouching();
        createNewBullet();

        if ( isAtEdge()  )
        {
            turn(-5);
        }  
        //if (Greenfoot.getRandomNumber(100) < 3
        {
            // addObject(new Bullet(), 100, Greenfoot.getRandomNumber(100));

        }

        {
            if (Greenfoot.isKeyDown("up"))
            {
                turn(4); 
            }

            if (Greenfoot.isKeyDown("down"))
            {
                turn(-4);    
            }

            if (Greenfoot.isKeyDown("right"))
            {
                move(-5);    
            }

            if (Greenfoot.isKeyDown("left"))
            {
                move(5);    
            }
        }

        {
            if ( isTouching(Person.class) )
            {
                counter.add(5);
            }

            if (isTouching(Person.class))
            {

                //removeTouching(Smiley.class);
                counter.add(1);
                Greenfoot.playSound("fanfare.wav");

            }   

            if (counter.getValue()>= 5) {
                Greenfoot.stop();
            } 

            {

                if (Greenfoot.isKeyDown("L"))
                {
                    Bullet newBullet;
                    newBullet = new Bullet();
                    World world;
                    world = getWorld();

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

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

                    world.addObject(newBullet, x, y);

                }    // Add your action code here.
            }
danpost danpost

2016/10/17

#
It appears your bracketing (using curly brackets) is off. As is, line 27 starts a new block that is an undeclared method. It looks like you inserted some lines inadvertently. The act method currently has no code. However, lines 29 through 31 (or maybe through 36) appear to be lines that should be in the act method. Place the appropriate codes in the appropriate methods and clean up the brackets so they are paired properly.
georgette georgette

2016/10/18

#
@danpost, I added code to the act method, placed the appropriate code in the appropriate methods and calmed down with my use of the brackets. Finally, after 3 days of getting no where my world is constructed! Thank you for all your help, I really appreciate it!
You need to login to post a reply.