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

2013/5/22

Game error

1
2
3
4
geekykid2013 geekykid2013

2013/5/22

#
Hi, whenever I compile my game I keep getting the following error. what must I change in my code to correct it. The error is in line 191
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 boolean fixed = true; // ????

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

    public Robot()
    {
       

    }

    /**
     * Act. Move the Robot if we're not fixed to paddle.
     */
    public void act()
    {
        if (!fixed)
        {
            move();
            makeSmoke();
            checkEggs();
            checkNextLevel();
            checkOut();
            if (getWorld() == null) return;
            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 (getWorld().getObjects(goldEgg.class).isEmpty()) {
                Greenfoot.setWorld(new CircleWorld(this));
                }
                else {
                Greenfoot.setWorld(new SquareWorld(this));
                 }
           }
        }
    }
danpost danpost

2013/5/22

#
You say you are getting an error; but, you do not let us know what the error message says!
geekykid2013 geekykid2013

2013/5/22

#
Good to hear from you DanPost. Hope you had a good day today. error message says: cannot find symbol - constructor CircleWorld(Robot)
danpost danpost

2013/5/22

#
You need to add another constructor to your CircleWorld class that excepts the Robot object in an argument.
public CircleWorld(Robot inRobot)
{
    super(/* int, int, int [, boolean ] */);
    robot = inRobot;
    // whatever code you have in the other constructor
}
// your other constructor should call the one above like this
public CircleWorld()
{
    this(new Robot());
}
geekykid2013 geekykid2013

2013/5/22

#
That seems to work. Now I get another error in my prepare method inside SquareWorld class, saying that - private void prepare() is an illegal start to an expression. Should I change it to public or omit the void
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * 
 * The square world. The square world has Gold Eggs that need to be collected
 * 
 * @author  
 * @version 1.0
 */
public class SquareWorld extends World
{

    // private static final int GAP = 12;

    private Counter counter;
    private HealthBar healthBar;
    private Paddle paddle;
    private Robot robot;
    
    
    

    /**
     * Constructor for objects of class SquareWorld.
     * 
     */
    public SquareWorld()
    {    

        super(700, 700, 1, true ); // Create a new world with 700x700 cells with a cell size of 1x1 pixels.
        setPaintOrder ( Robot.class, Smoke.class );
        counter = new Counter(); // set the counter to the world
        addObject (counter, 100, 678); // add the counter object to the world
        healthBar = new HealthBar("Robot Health", "", 100, 100);
        addObject(healthBar, 600, 678);
        robot = new Robot(counter, healthBar);
        addObject(robot, getWidth() / 2, getHeight() - 90);
        paddle = new Paddle(robot); // set the Paddle to the world
        addObject ( paddle, getWidth() / 2, getHeight() - 55); //add the paddle object to the world
    
        

       //prepare();
    //}

    //public void RobotIsOut()
    //{
    //    paddle.newRobot();
    //}

   // public void score()
    //{
      //  counter.addScore();
    //}
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {

        //Counter counter = new Counter();
        //addObject(counter, 100, 678);

        goldEgg goldegg = new goldEgg();
        addObject(goldegg, 319, 404);
        goldegg.setLocation(309, 397);
        goldEgg goldegg2 = new goldEgg();
        addObject(goldegg2, 316, 190);
        goldegg2.setLocation(308, 182);
        goldEgg goldegg3 = new goldEgg();
        addObject(goldegg3, 465, 262);
        goldegg3.setLocation(458, 259);
        goldEgg goldegg4 = new goldEgg();
        addObject(goldegg4, 556, 95);
        goldegg4.setLocation(552, 95);
        goldEgg goldegg5 = new goldEgg();
        addObject(goldegg5, 145, 97);
        goldegg5.setLocation(143, 91);
        goldEgg goldegg6 = new goldEgg();
        addObject(goldegg6, 151, 266);
        goldegg6.setLocation(142, 258);
        goldEgg goldegg7 = new goldEgg();
        addObject(goldegg7, 557, 417);
        goldegg7.setLocation(555, 411);
        goldEgg goldegg8 = new goldEgg();
        addObject(goldegg8, 237, 335);
        goldegg8.setLocation(232, 332);
        goldEgg goldegg9 = new goldEgg();
        addObject(goldegg9, 353, 291);
        goldegg9.setLocation(346, 286);
        goldEgg goldegg10 = new goldEgg();
        addObject(goldegg10, 146, 493);
        goldegg10.setLocation(144, 486);
        goldEgg goldegg11 = new goldEgg();
        addObject(goldegg11, 389, 494);
        goldegg11.setLocation(386, 483);
        goldEgg goldegg12 = new goldEgg();
        addObject(goldegg12, 564, 268);
        goldegg12.setLocation(553, 259);
        goldEgg goldegg13 = new goldEgg();
        addObject(goldegg13, 394, 100);
        goldegg13.setLocation(388, 91);
        redEgg redegg = new redEgg();
        addObject(redegg, 234, 100);
        redegg.setLocation(227, 92);
        blueEgg blueegg = new blueEgg();
        addObject(blueegg, 314, 100);
        blueegg.setLocation(307, 93);
        redEgg redegg2 = new redEgg();
        addObject(redegg2, 472, 101);
        redegg2.setLocation(462, 88);
        yellowEgg yellowegg = new yellowEgg();
        addObject(yellowegg, 393, 187);
        yellowegg.setLocation(389, 181);
        yellowEgg yellowegg2 = new yellowEgg();
        addObject(yellowegg2, 464, 334);
        yellowegg2.setLocation(453, 332);
        redEgg redegg3 = new redEgg();
        addObject(redegg3, 239, 267);
        redegg3.setLocation(233, 260);
        blueEgg blueegg2 = new blueEgg();
        addObject(blueegg2, 152, 341);
        blueegg2.setLocation(143, 332);
        blueEgg blueegg3 = new blueEgg();
        addObject(blueegg3, 464, 195);
        blueegg3.setLocation(458, 187);
        yellowEgg yellowegg3 = new yellowEgg();
        addObject(yellowegg3, 560, 190);
        yellowegg3.setLocation(553, 185);
        redEgg redegg4 = new redEgg();
        addObject(redegg4, 149, 418);
        redegg4.setLocation(144, 412);
        blueEgg blueegg4 = new blueEgg();
        addObject(blueegg4, 149, 195);
        blueegg4.setLocation(141, 182);
        yellowEgg yellowegg4 = new yellowEgg();
        addObject(yellowegg4, 236, 191);
        yellowegg4.setLocation(230, 184);
        redEgg redegg5 = new redEgg();
        addObject(redegg5, 456, 403);
        redegg5.setLocation(454, 394);
        yellowEgg yellowegg5 = new yellowEgg();
        addObject(yellowegg5, 241, 401);
        yellowegg5.setLocation(231, 392);
        redEgg redegg6 = new redEgg();
        addObject(redegg6, 307, 496);
        redegg6.setLocation(304, 486);
        blueEgg blueegg5 = new blueEgg();
        addObject(blueegg5, 388, 402);
        blueegg5.setLocation(381, 395);
        blueEgg blueegg6 = new blueEgg();
        addObject(blueegg6, 559, 340);
        blueegg6.setLocation(554, 335);
        yellowEgg yellowegg6 = new yellowEgg();
        addObject(yellowegg6, 558, 493);
        yellowegg6.setLocation(553, 490);
        redEgg redegg7 = new redEgg();
        addObject(redegg7, 233, 491);
        redegg7.setLocation(227, 485);
        blueEgg blueegg7 = new blueEgg();
        addObject(blueegg7, 472, 492);
        blueegg7.setLocation(461, 481);
        removeObject(redegg4);
        redEgg redegg8 = new redEgg();
        addObject(redegg8, 148, 416);
        redegg8.setLocation(142, 408);
    }
    
    
}
saying that it is an illegal start of the prepare method - private void prepare()
danpost danpost

2013/5/22

#
You commented out the closing curly bracket for the constructor (uncomment line 45).
geekykid2013 geekykid2013

2013/5/22

#
When I have to create a constructor in the SquareWorld() do I have to write it as follows -
public SquareWorld(Robot inRobot)  
{  
    super(/* int, int, int, true */);  
    robot = inRobot;  
    // whatever code you have in the other constructor  
}  
// your other constructor should call the one above like this  
public SquareWorld()  
{  
    this(new Robot());  
} 
geekykid2013 geekykid2013

2013/5/22

#
My code is now saying as before cannot find symbol - Constructor SquareWorld(Robot). I guess I have to create another constructor inside my circle world right? but I have on there already. Do I write brand new code underneath the current constructor
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 boolean fixed = true; // ????

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

    public Robot()
    {
       

    }

    /**
     * Act. Move the Robot if we're not fixed to paddle.
     */
    public void act()
    {
        if (!fixed)
        {
            move();
            makeSmoke();
            checkEggs();
            checkNextLevel();
            checkOut();
            if (getWorld() == null) return;
            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 (getWorld().getObjects(goldEgg.class).isEmpty()) {
                Greenfoot.setWorld(new CircleWorld(this));
                }
                else {
                Greenfoot.setWorld(new SquareWorld(this));
                 }
           }
        }
    }
e there already?
danpost danpost

2013/5/22

#
Before we fix the world-changing, let us make sure that we have access to everything that we need passed from world to world (the HealthBar object and the Counter object). Add the following methods to the Robot class:
public HealthBar getHealthBar()
{
    return healthBar;
}

public Counter getCounter()
{
    return counter;
}
Now, when we pass the Robot object, we can access its healthBar and counter using those methods. My next post will either ask some questions or further assist you (I have to review what there is so far)
danpost danpost

2013/5/22

#
Ok, both the CircleWorld class and the SquareWorld class need a constructor that excepts the Robot object in an argument. The one that is the initial world created in your scenario will also need a constructor with no arguments. Which world is the first to be instantiated in your scenario (SquareWorld or CircleWorld)?
geekykid2013 geekykid2013

2013/5/22

#
I have implemented the code above as suggested, however I keep getting the same message as before - cannot find symbol - constructor SquareWorld(Robot). I seem to get confused with theses constructor at times. I know that they are special methods created in greenfoot when an object is created, however I am not always sure where and how to write the code in Greenfoot, particularly in this case. Sometimes I think that I have write it inside an existing constructor, sometimes outside. I'm never really sure.
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 boolean fixed = true; // ????

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

    public HealthBar getHealthBar()
    {
       
        return healthBar;
    }
    
    public Counter getCounter()
    {
        return counter;
    }

    /**
     * Act. Move the Robot if we're not fixed to paddle.
     */
    public void act()
    {
        if (!fixed)
        {
            move();
            makeSmoke();
            checkEggs();
            checkNextLevel();
            checkOut();
            if (getWorld() == null) return;
            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 (getWorld().getObjects(goldEgg.class).isEmpty()) {
                Greenfoot.setWorld(new CircleWorld(this));
                }
                else {
                Greenfoot.setWorld(new SquareWorld(this));
                 }
           }
        }
    }


s sure how to write the code for it, particularly in this case.
geekykid2013 geekykid2013

2013/5/22

#
Okay. The SquareWorld() needs to be initiated first, then the CircleWorld(), then finally StarWorld()
danpost danpost

2013/5/22

#
Begin your SquareWorld class like this:
import greenfoot.*;
import java.awt.Color;

public class SquareWorld extends World
{
    private Counter counter;
    private HealthBar healthBar;
    private Paddle paddle;
    private Robot robot;

    public SquareWorld()
    {
        this(null);
    }

    public SquareWorld(Robot inRobot)
    {
        super(700, 700, 1, true );
        setPaintOrder ( Robot.class, Smoke.class );
        if (inRobot == null) counter = new Counter();
        else counter = inRobot.getCounter();
        addObject (counter, 100, 678);
        if (inRobot == null) healthBar = new HealthBar("Robot Health", "", 100, 100);
        else healthBar = inRobot.getHealthBar();
        addObject(healthBar, 600, 678);
        if (inRobot == null) robot = new Robot(counter, healthBar);
        else robot = inRobot;
        addObject(robot, getWidth() / 2, getHeight() - 90);
        paddle = new Paddle(robot); // set the Paddle to the world
        addObject ( paddle, getWidth() / 2, getHeight() - 55); //add the paddle object to the world
    
        

       //prepare();
    }
    // etc.
geekykid2013 geekykid2013

2013/5/22

#
Ok. i have implemented the code above as suggested. so far so good - no syntax errors
danpost danpost

2013/5/22

#
In the other worlds, do not use 'new HealthBar' or 'new Counter'; but get the objects from the Robot object as in the SquareWorld 'else' parts. What I am trying to say is that you do not use the 'if (inRobot == null)' conditionals, but to just execute the else parts of them. You also do not need the basic constructors with no arguments in those classes.
There are more replies on the next page.
1
2
3
4