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

2011/12/23

how do I make things respawn?

1
2
programmer22 programmer22

2011/12/23

#
ok so where i put if (getObjects(Worm.class).size() == 0) it turns the area blue and the last thing under gets put in the blue with it and it says not a statement any ideas on what to do :import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CrabWorld here. * * @author (your name) * @version (a version number or a date) */ public class CrabWorld extends World { /** * Constructor for objects of class CrabWorld. * */ public CrabWorld() { super(560, 560, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Crab crab = new Crab(); addObject(crab, 257, 300); Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Lobster lobster = new Lobster(); addObject(lobster, 112, 474); Lobster lobster2 = new Lobster(); addObject(lobster2, 94, 112); Counter counter = new Counter(); addObject(counter, 46, 53); counter.setLocation(23, 22); Worm worm18 = new Worm(); addObject(worm18, 336, 509); Worm worm19 = new Worm(); addObject(worm19, 447, 143); Worm worm20 = new Worm(); addObject(worm20, 490, 472); } private void addWorms() { if (getObjects(Worm.class).size() == 0) Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Worm worm18 = new Worm(); addObject(worm18, 336, 509); Worm worm19 = new Worm(); addObject(worm19, 447, 143); Worm worm20 = new Worm(); addObject(worm20, 490, 472); } } it says that worm worm = newworm is not a statement any ideas on how to fix?
programmer22 programmer22

2011/12/23

#
ok so where i put if (getObjects(Worm.class).size() == 0) it turns the area blue and the last thing under gets put in the blue with it and it says not a statement any ideas on what to do :import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CrabWorld here. * * @author (your name) * @version (a version number or a date) */ public class CrabWorld extends World { /** * Constructor for objects of class CrabWorld. * */ public CrabWorld() { super(560, 560, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Crab crab = new Crab(); addObject(crab, 257, 300); Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Lobster lobster = new Lobster(); addObject(lobster, 112, 474); Lobster lobster2 = new Lobster(); addObject(lobster2, 94, 112); Counter counter = new Counter(); addObject(counter, 46, 53); counter.setLocation(23, 22); Worm worm18 = new Worm(); addObject(worm18, 336, 509); Worm worm19 = new Worm(); addObject(worm19, 447, 143); Worm worm20 = new Worm(); addObject(worm20, 490, 472); } private void addWorms() { if (getObjects(Worm.class).size() == 0) Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Worm worm18 = new Worm(); addObject(worm18, 336, 509); Worm worm19 = new Worm(); addObject(worm19, 447, 143); Worm worm20 = new Worm(); addObject(worm20, 490, 472); } } it says that worm worm = newworm is not a statement any ideas on how to fix?
programmer22 programmer22

2011/12/23

#
ok so i added curly brackets right after if (getObjects(Worm.class).size() == 0) so no syntax errors but doesnt add worms when it finishes eating them all private void addWorms() { if (getObjects(Worm.class).size() == 0) { } Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Worm worm18 = new Worm(); addObject(worm18, 336, 509); Worm worm19 = new Worm(); addObject(worm19, 447, 143); Worm worm20 = new Worm(); addObject(worm20, 490, 472); }
Duta Duta

2011/12/23

#
you need to place that mass of code into the curly brackets, like so:
private void addWorms()
{
    if (getObjects(Worm.class).size() == 0)
    {
        Worm worm = new Worm();
        addObject(worm, 392, 50);
        Worm worm2 = new Worm();
        addObject(worm2, 462, 264);
        Worm worm3 = new Worm();
        addObject(worm3, 425, 501);
        Worm worm4 = new Worm();
        addObject(worm4, 413, 227);
        Worm worm5 = new Worm();
        addObject(worm5, 427, 460);
        Worm worm6 = new Worm();
        addObject(worm6, 179, 488);
        Worm worm7 = new Worm();
        addObject(worm7, 246, 495);
        Worm worm8 = new Worm();
        addObject(worm8, 54, 443);
        Worm worm9 = new Worm();
        addObject(worm9, 88, 269);
        Worm worm10 = new Worm();
        addObject(worm10, 105, 193);
        Worm worm11 = new Worm();
        addObject(worm11, 103, 52);
        Worm worm12 = new Worm();
        addObject(worm12, 223, 109);
        Worm worm13 = new Worm();
        addObject(worm13, 280, 71);
        Worm worm14 = new Worm();
        addObject(worm14, 339, 195);
        Worm worm15 = new Worm();
        addObject(worm15, 525, 105);
        Worm worm16 = new Worm();
        addObject(worm16, 480, 400);
        Worm worm17 = new Worm();
        addObject(worm17, 130, 390);
        Worm worm18 = new Worm();
        addObject(worm18, 336, 509);
        Worm worm19 = new Worm();
        addObject(worm19, 447, 143);
        Worm worm20 = new Worm();
        addObject(worm20, 490, 472);
    }
}
Duta Duta

2011/12/23

#
Also, is a call to this method placed in the act() method? If not this won't happen when the worm count reaches 0. If you haven't done this, then find your act() method, and within its curly brackets, put a call to addWorms(), like this:
pubic void act()
{
    // Whatever else is in your act method
    addWorms();
}
Duta Duta

2011/12/23

#
Oh and another thing, with your addWorms() method, I think a better option would be to have something like this replace it:
private void addWorms()
{
    if (getObjects(Worm.class).size() == 0) //When there is no worms
    {
        for(int i = 0; i < 20; i++) //Do the following 20 times
        {
            addObject(new Worm(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); //Add an object of the Worm class to a random location in the world
        }
    }
}
You need to login to post a reply.
1
2