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

2013/2/7

Object count

1
2
Gingervitis Gingervitis

2013/2/7

#
How can I make a new bonus world if all the lettuces in the main menu have been eaten? Here is my code but it isn't working.
public void seeEatLettuce()  
      
    {  
        if (canSee(Lettuce.class))  
        {  
            eat(Lettuce.class);  
            Greenfoot.playSound("slurp.wav");  
            
            lettucesEaten = lettucesEaten + 1;   
            if (lettucesEaten == 5)                   
            {  
                createNewLettuce();
                Greenfoot.setWorld(new Moon()); 
            }  
        }  
    }
danpost danpost

2013/2/7

#
Please be more explicit as to what you are trying to acccomplish. Line 12 above has no effect as to the proceeding of the scenario as the new lettuce in placed in a world that is getting flagged for garbage collection. The code tells me that the new world is going to be a Moon world, but does not inform me as to what type is being replace by it. If it is also a Moon world, maybe you need to create a constructor with a parameter so that the creation process can be modified for your bonus world.
// line 13 would be written as
Greenfoot.setWorld(new Moon(true));
// and the alternate world constructor would be
public Moon(boolean bonusWorld)
{
    // if initial setup is ok
    this(); // calling your base world constructor
    //  OR (if different setup required)
    super(600, 400, 1); // whatever dimensions you desire

    // additional code to modify setup here
}
Gingervitis Gingervitis

2013/2/7

#
I'm not 100% sure if that is the problem. I don't think the
lettucesEaten = lettucesEaten + 1;   
            if (lettucesEaten == 5)                   
            {  
                Greenfoot.setWorld(new Moon(true)); 
            } 
statement is being executed. I have created several other worlds before and they start perfectly fine. I have just never started a new world this particular way before.
danpost danpost

2013/2/7

#
Modify the code to the following and run it eating 5 lettuces to check the progress:
lettucesEaten=lettucesEaten+1;
System.out.println("Lettuces eaten: "+lettucesEaten);
if(lettucesEaten==5)
{
    System.out.println("Changing worlds");
    Greenfoot.setWorld(new Moon(true));
}
Post back as to what output you get.
Gingervitis Gingervitis

2013/2/7

#
Creating a new world isn't exactly what I was going to do. I originally wanted to have a specific class eat another class and after a certain amount eaten, it would create a new class. I just thought the same process would be used for both scenarios.
Gingervitis Gingervitis

2013/2/7

#
public Moon(boolean bonusWorld)  
    {  
         
        super(600, 500, 1); 

         
    }

error at line 1 "invalid method declaration; return type required" I think I put in the wrong code.
danpost danpost

2013/2/7

#
I hope you put that code in your Moon world class; and I hope you kept the original constructor that started
public Moon()
{
    super(...);
Gingervitis Gingervitis

2013/2/7

#
I have been putting the code in my Turtle class because that is what is eating the lettuce.
danpost danpost

2013/2/7

#
Yeah, the lettuceEating code goes where you have it. It is the 'public Moon(boolean bonusWorld)' code that needs to be in your Moon world class.
Gingervitis Gingervitis

2013/2/7

#
Either way it had no effect. I put in that code in the world class too.
danpost danpost

2013/2/7

#
You should modify the Tutle class code to produce that output as shown above and see what comes out of it.
Gingervitis Gingervitis

2013/2/7

#
I put everything in the turtle class that you posted above. It still has no effect.
danpost danpost

2013/2/7

#
Post your whole Tutle class code for me to see.
Gingervitis Gingervitis

2013/2/7

#
import greenfoot.*; 

public class Turtle extends Animal
{
    private Counter counter;
    private int timeDelayCount;
    private int lettucesEaten;  
 
    
    public Turtle()
    {
       lettucesEaten = 0;
    }

    public void CrabWorld()
    {

    }

    public Turtle(Counter pointCounter)
    {
        counter = pointCounter;
        timeDelayCount = 0;
        lettucesEaten = 0;
    }
    int x =Greenfoot.getRandomNumber(4) + 4;
    // if you want to have the turtle move at a random speed,
    //switch "4" in move to the int variable "x"
    public void act()
    {
        move(3);
        tryToEat();
        tryToEatStarfish();
        tryToEatCrab();
        ifAtEdge();
        tryToEatUD();
        tryToEatSud();
        checkKeys();
        mouseClick();
        checkNewTime();
        tryToEatWorm();
        tryToEatSuperWorm();
        ifCanSeeArrow();
        tryToEatWorm3();
        eatLettuce();
    }
    
    public Moon(boolean bonusWorld)  
{  
    // if initial setup is ok  
    this(); // calling your base world constructor  
    //  OR (if different setup required)  
    super(600, 00, 1); // whatever dimensions you desire  
  
    // additional code to modify setup here  
} 

    public void ifEaten()
    {

        {
            Greenfoot.setWorld(new LaunchWorld());
        }
    }

    public void mouseClick()
    {
        if (Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();           

            createTurtle(); 
        }

    }

    /**
     * Check if keys are being pressed. Press left turn left. 
     * Press right turn right.
     */
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }

    }
    
    public void eatLettuce()  
      
    {  
        if (canSee(Lettuce.class))  
        {  
            eat(Lettuce.class);  
            Greenfoot.playSound("slurp.wav");  
            
            lettucesEaten = lettucesEaten + 1;   
            if (lettucesEaten == 5)                   
            {  
                createNewLettuce();
                Greenfoot.setWorld(new Moon(true)); 
            }  
        }  
    }

    public void newCrab2()
    {
        timeDelayCount = 300;

    }

    public void checkNewTime()
    {
        if (timeDelayCount > 0)
        {
            timeDelayCount--;  
            if (timeDelayCount ==0)
            {
                Greenfoot.setWorld(new CrabWorld());
                createNewCrab2();
                createNewLettuce();

            }
        }
    }

    /**
     * If turtle reaches edge of world, turn (x) amount
     */
    public void ifAtEdge()
    {
        if ( atWorldEdge() )
        {
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                if(counter != null) counter.add(150);
                Greenfoot.playSound("slurp.wav");
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
                createNewStarfish();

            }

        }
    }

    public void tryToEatSud()    
    {
        if(canSee(SuperUltimateDestroyer.class))
        {
            eat(SuperUltimateDestroyer.class);
            Greenfoot.playSound("slurp.wav");
            Greenfoot.playSound("gamefinish.wav");

            newCrab2();
            getWorld();

        }
    }

    public void tryToEatUD()
    {
        if(canSee(UltimateDestroyer.class))
        {
            eat(UltimateDestroyer.class);
            counter.add(500);
            Greenfoot.playSound("slurp.wav");
            createNewSuperUltimateDestroyer();

        }
    }

    public void tryToEatWorm()
    {
        if (canSee(Worm.class))
        {
            eat(Worm.class);
            slurp();
            createNewLobster2();
            createNewLobster2();
            createNewLobster2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewSuperWorm();
        }

        if (canSee(Worm2.class))
        {
            eat(Worm2.class); 
            slurp();
        }
    }

    public void tryToEatSuperWorm()
    {
        if (canSee(SuperWorm.class))
        {
            eat(SuperWorm.class); 
            createNewArrow();

        }

        if (canSee(SuperWorm2.class))
        {
            eat(SuperWorm2.class); 
            slurp();
            createNewRocket();

        }
    }

    public void tryToEatWorm3()
    {
        if (canSee(Worm3.class))
        {
            eat(Worm3.class);
            slurp();
            createNewLobster2();
            createNewLobster2();
            createNewLobster2();
            createNewLobster2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewWorm2();
            createNewSuperWorm2();
        }
    }

    /**
     * If turtle can see lettuce or Destroyer and sometimes a snake, 
     * it will eat them 
     */
    public void tryToEat()
    {
        if (canSee(BlasterBullet.class))
        {
            eat(BlasterBullet.class);

        }

        if (canSee(BlasterBullet2.class))
        {
            eat(BlasterBullet2.class);

        }

        if(canSee(Lettuce.class))
        {
            eat(Lettuce.class);
            if(counter != null) counter.add(5);  
            Greenfoot.playSound("slurp.wav");
        }

        if(canSee(Bug.class))
        {
            eat(Bug.class);
            if(counter != null) counter.add(20);
            Greenfoot.playSound("slurp.wav");
            createNewBug();
            createNewBug();
            createNewSnake();
        }

        if (canSee(Snake.class))
        {
            eat(Snake.class);
            if(counter != null)counter.add(75);
            createNewDestroyer();
        }

        if (canSee(Counter.class))
        {

            if(canSee(Starfish.class))
            {
                eat(Starfish.class);
                if(counter != null) counter.add(5);
                Greenfoot.playSound("slurp.wav");
                createNewStarfish();
                createNewCrab();
                createNewCrab();
            }

            if (canSee(Crab.class))
            {
                eat(Crab.class);
                if(counter != null) counter.add(75);
                createNewCrab();
                createNewStarfish();
                createNewStarfish();
            }

        }
    }

    public void tryToEatStarfish()
    {
        if(canSee(Starfish.class))
        {
            eat(Starfish.class);
            createNewStarfish();
            createNewCrab();

        }
    }

    public void tryToEatCrab()
    {
        if (canSee(Crab.class))
        {
            eat(Crab.class);
            createNewCrab();
            createNewStarfish();

        }
    }

    public void newLaunchWorld()
    {
        Greenfoot.setWorld(new LaunchWorld());
    }

    public void ifCanSeeArrow()
    {
        if (canSee(Arrow.class))
        {
            newLaunchWorld();
        }
    }

    /**
     * We won the game.
     */
    public void gameOver()
    {
        Greenfoot.playSound("gamefinish.wav");
        Greenfoot.stop();
    }

    public void slurp()
    {
        Greenfoot.playSound("slurp.wav");
    }

    /**
     *  Create a new bug and place it randomly
     */
    private void createNewBug()
    {
        Bug newBug;
        newBug = new Bug();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newBug, x, y);
    }

    /**
     *  Create a new lettuce and place it randomly
     */
    private void createNewLettuce()
    {
        Lettuce newLettuce;
        newLettuce = new Lettuce();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newLettuce, x, y);
    }

    /**
     *  Create a new snake and place it randomly
     */
    private void createNewSnake()
    {
        Snake newSnake;
        newSnake = new Snake();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSnake, x, y);
    }

    /**
     *  Create a new destroyer that eats everything but you and place it randomly
     */
    private void createNewDestroyer()
    {
        Destroyer newDestroyer;
        newDestroyer = new Destroyer();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newDestroyer, x, y);
    }

    /**
     *  Create a new destroyer2 that eats everything but you and place it randomly
     */
    private void createNewDestroyer2()
    {
        Destroyer2 newDestroyer2;
        newDestroyer2 = new Destroyer2();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newDestroyer2, x, y);
    }

    /**
     *  Create a new UltimateDestroyer and place it randomly
     */
    private void createNewUltimateDestroyer()
    {
        UltimateDestroyer newUltimateDestroyer;
        newUltimateDestroyer = new UltimateDestroyer();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newUltimateDestroyer, x, y);
    }

    /**
     *  Create a new starfish and place it randomly
     */
    private void createNewStarfish()
    {
        Starfish newStarfish;
        newStarfish = new Starfish();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newStarfish, x, y);
    } 

    /**
     *  Create a new crab and place it randomly
     */
    private void createNewCrab()
    {
        Crab newCrab;
        newCrab = new Crab();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newCrab, x, y);
    }

    /**
     *  Create a new crab2 and place it randomly
     */
    private void createNewCrab2()
    {
        Crab2 newCrab2;
        newCrab2 = new Crab2();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newCrab2, x, y);
    }

    /**
     *  Create a new SuperUltimateDestroyer and place it randomly
     */
    private void createNewSuperUltimateDestroyer()
    {
        SuperUltimateDestroyer newSuperUltimateDestroyer;
        newSuperUltimateDestroyer = new SuperUltimateDestroyer();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSuperUltimateDestroyer, x, y);
    }

    /**
     *  Create a new Turtle and place it randomly
     */
    public void createTurtle()
    {
        Turtle newTurtle;
        newTurtle = new Turtle();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newTurtle, x, y);
    }

    /**
     *  Create a new lobster2 and place it randomly
     */
    private void createNewLobster2()
    {
        Lobster2 newLobster2;
        newLobster2 = new Lobster2();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newLobster2, x, y);
    }

    /**
     *  Create a new Worm and place it randomly
     */
    private void createNewWorm()
    {
        Worm newWorm;
        newWorm = new Worm();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newWorm, x, y);
    }

    /**
     *  Create a new Worm2 and place it randomly
     */
    private void createNewWorm2()
    {
        Worm2 newWorm2;
        newWorm2 = new Worm2();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newWorm2, x, y);
    }

    /**
     *  Create a new Worm2 and place it randomly
     */
    private void createNewWorm3()
    {
        Worm3 newWorm3;
        newWorm3 = new Worm3();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newWorm3, x, y);
    }

    /**
     *  Create a new SuperWorm and place it randomly
     */
    private void createNewSuperWorm()
    {
        SuperWorm newSuperWorm;
        newSuperWorm = new SuperWorm();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSuperWorm, x, y);
    }

    /**
     *  Create a new SuperWorm and place it randomly
     */
    private void createNewSuperWorm2()
    {
        SuperWorm2 newSuperWorm2;
        newSuperWorm2 = new SuperWorm2();

        World world;
        world = getWorld();

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

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSuperWorm2, x, y);
    }

    /**
     *  Create a new arrow and place it randomly
     */
    private void createNewArrow()
    {
        Arrow newArrow;
        newArrow = new Arrow();

        World world;
        world = getWorld();

        world.addObject(newArrow, 561, 256);
    }

    /**
     *  Create a new Turtle and place it randomly
     */
    public void createNewRocket()
    {
        Rocket newRocket;
        newRocket = new Rocket();

        World world;
        world = getWorld();

        world.addObject(newRocket, 350, 460);
    }
    
    
    
     
}
Gingervitis Gingervitis

2013/2/7

#
lines 94-109 pertain to this situation
There are more replies on the next page.
1
2