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

2013/5/17

Multi Worlds problem

1
2
geekykid2013 geekykid2013

2013/5/17

#
How do I move from Level 1 (Square World) to Level 2 (Circle World) when I have completed the level. I have tried different plans but I keep getting the error: cannot find symbol - constructor SquareWorld(Robot). can anyone help? Here is my code for my main character (Robot) as it moves from world to world
import java.util.List;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The Robot of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author  
 * @version (Version 1.0)
 */
public class Robot extends Actor
{

    private int points = 0;
    private Counter counter; 
    private HealthBar healthBar;

    private int deltaX; //  delat is movement in the x direction
    private int deltaY; //  delta is movement in the y direction
    private int count = 2;
    //private int CircleWorld;
    //private int SquareWorld;
    //private SquareWorld squareWorld;
    private int level;
   
    private boolean fixed = true; // ????

    public Robot(Counter c, HealthBar hb)
    {
        counter = c;
        healthBar = hb;
    }

    public Robot()
    {
      level = 1;
    }

    /**
     * Act. Move the Robot if we're not fixed to paddle.
     */
    public void act()
    {
        if (!fixed)
        {
            move();
            makeSmoke();
            checkEggs();
            checkOut();
            checkNextLevel();
            
        }
    }

    /**
     * Move the Robot. Thencheck what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }

    /**
     * Check whether we've hit one of the three walls. Reverse direction
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
        }
    }

    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {   
        if (getY() == getWorld().getHeight()-1) 
        {
            List<Paddle> paddles = getWorld().getObjects(Paddle.class);
            Robot newRobot = new Robot(counter, healthBar);
            getWorld().addObject(newRobot, paddles.get(0).getX(), paddles.get(0).getY() - 35);
            paddles.get(0).setNewRobot(newRobot);
            getWorld().removeObject(this);
        }
    }

    /**
     * Check whether we have hit an egg, and make the egg disappear if we have
     */

    private void checkEggs()
    {
        Actor egg = getOneIntersectingObject(goldEgg.class);
        if (egg != null)
        {
            getWorld().removeObject(egg);
            deltaY = -deltaY;
            counter.add(5);
        }

        egg = getOneIntersectingObject(yellowEgg.class);
        if (egg != null)
        {
            getWorld().removeObject(egg);
            deltaY = -deltaY;
            counter.add(3);
        }
        
        egg = getOneIntersectingObject(redEgg.class);
        if (egg != null)
        {
            getWorld().removeObject(egg);
            deltaY = -deltaY;
            counter.add(1);
        }
        
        egg = getOneIntersectingObject(blueEgg.class);
        if (egg != null)
        {
            getWorld().removeObject(egg);
            deltaY = -deltaY;
            healthBar.add(-10);
        }

        //if (counter.getValue() >= 60) {
        //     gameOver();
    }

    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;
            int offset = getX() - paddle.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX> 7) {
                deltaX = 7;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
            //SquareWorld mySquareWorld = (SquareWorld) getWorld();
            //mySquareWorld.score();

        }
    }

    /**
     * Move the Robot a given distance sideways
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());

    }

    /**
     * Put out a puff of smoke (only on every second call).
     */
    private void makeSmoke()
    {
        count--;
        if (count == 0) {
            getWorld().addObject ( new Smoke(), getX(), getY());
            count = 2;
        }
    }

    /**
     * Release the Robot from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        fixed = false;
    }
    
    /**
     * Check whether we should go to the next level, and if yes, start the next level.
     */
    private void checkNextLevel()
    {
         if (getX() == getWorld().getWidth()-1) {
            if (level == 1) {
                level = 2;
                getWorld().removeObject(this);
                Greenfoot.setWorld(new SquareWorld(this));
            }
            else {
                level = 1;
                getWorld().removeObject(this);
                Greenfoot.setWorld(new CircleWorld(this));
            }
        }
    }
}

danpost danpost

2013/5/17

#
The problem is in the use of the 'level' field to determine which world to go to. Every time a new Robot object is created, the 'level' field is reset; so, you will always go to the same world. Change your 'checkNextLevel' method to the following:
private void checkNextLevel()
{
    if (getX() == getWorld().getWidth()-1) {
        if (getWorld() instanceof SquareWorld) {
            Greenfoot.setWorld(new CircleWorld(this));
        } else {
            Greenfoot.setWorld(new SquareWorld(this));
        }
    }
}
geekykid2013 geekykid2013

2013/5/17

#
I now have another error from this code: cannot find symbol - constructor CircleWorld(Robot). Should I have declared CircleWorld in my constructor?
danpost danpost

2013/5/17

#
You probably do not have a constructor starting with:
public CircleWorld(Robot robot)
in your CircleWorld world class.
geekykid2013 geekykid2013

2013/5/17

#
I created a ContinueButton (class extend from Actor) that links directly to my SquareWorld. I get an error in this class: cannot find Symbol - constructor SquareWorld() ?
danpost danpost

2013/5/17

#
Then you are missing a constructor with no arguments in your SquareWorld class. (one that begins with 'public SquareWorld()'.
geekykid2013 geekykid2013

2013/5/17

#
Sorry to be a pain on this , but I already have a constructor SquareWorld(Robot robot) in the Square World class. When I removed the arguments (Robot robot) I then received an error in my Robot class - cannot find symbol...etc etc... Are you asking me to create another constructor?
danpost danpost

2013/5/17

#
Yes. You can have multiple constructor in a class. Usually, what you would do, is have one constructor (the one with less arguments) fill in the missing arguments in the other. Something like this:
public SquareWorld()
{
    this(new Robot()); // this calls the following constructor
}

public SquareWorld(Robot robot)
{
    // code constructing world
}
geekykid2013 geekykid2013

2013/5/17

#
OK. Thanks v. much DanPost that seems to have done the trick. you have been a great help today. Although my robot (object) needs to collect all the gold eggs available before moving to the next level. At the moment it collects only six and then jumps to the next level. How can I change that?
danpost danpost

2013/5/17

#
Change the condition to change worlds (line 3 of the 'checkNextLevel' method) to 'if (getWorld().getObjects(goldEgg.class).isEmpty()) {'.
geekykid2013 geekykid2013

2013/5/17

#
The number of gold eggs collected has increased but now I get an exception error which stops the game.
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 Robot.checkNextLevel(Robot.java:186)
	at Robot.act(Robot.java:47)
	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)
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 Robot.checkNextLevel(Robot.java:186)
	at Robot.act(Robot.java:47)
	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)
geekykid2013 geekykid2013

2013/5/17

#
I am now getting further exceptions errors. Do I need to change my previous code?
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 Robot.checkNextLevel(Robot.java:186)
	at Robot.act(Robot.java:47)
	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)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
danpost danpost

2013/5/17

#
Your 'checkOut' method could remove the actor from the world. If you continue execution of future methods that require the actor to be in the world, you will get that message. The 'checkNextLevel' method uses 'getX', which require the actor to be in the world. Therefore, you need to either exit out of the method before executing the call to 'checkNextLevel' or put a condition on calling it.
// either
// beginning of act method
    checkOut();
    if (getWorld() == null) return;
    checkNextLevel();
}
// or (after beginning of act method
    checkOut();
    if (getWorld() != null) checkNextLevel();
}
I prefer the first way, as if you add more code to the method, it will not mess up due to the actor already having been removed from the world.
geekykid2013 geekykid2013

2013/5/17

#
do you mean that the code sits inside the act method or i create anew act method
geekykid2013 geekykid2013

2013/5/17

#
I sat the code inside the act method. I still receive exception errors
There are more replies on the next page.
1
2