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

2014/4/5

Scoreboards and counters...and a big thank you to anyone who reads through all this

1
2
Roz Roz

2014/4/5

#
There are two ways that my scenario ends: 1) Patrick collects 100 shamrocks or 2) Patrick is poisoned by a mushroom. I've managed to get the counter and scoreboard to cooperate when Patrick collects a hundred shamrocks. At this point in the game the scenario stops and the scoreboard comes up showing the correct number from the counter (the counter keeps track of shamrocks collected). My problem now is getting the scoreboard to come up when Patrick is poisoned by a mushroom. I've tried the same code, altered the same code, rearranged the same code (!) that I've used in class Patrick, but cannot get it to work. Any help would be greatly appreciated. Here is a copy of the terminal error that appears as well as classes Mushroom, Patrick and PatrickWorld: THIS IS THE TERMINAL ERROR I GET WHEN PATRICK IS EATEN BY A MUSHROOM at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Mushroom.tryToEatPatrick(Mushroom.java:76) at Mushroom.act(Mushroom.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Mushroom.tryToEatPatrick(Mushroom.java:76) at Mushroom.act(Mushroom.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Mushroom.tryToEatPatrick(Mushroom.java:76) at Mushroom.act(Mushroom.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Mushroom.tryToEatPatrick(Mushroom.java:76) at Mushroom.act(Mushroom.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) MUSHROOM CLASS import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Poisonous mushrooms float around and will end game if they touch Patrick. * * @author () * @version (1.0) */ public class Mushroom extends Animal { private int shamrocksEaten; private Counter counter; /** * Act - do whatever the Mushroom wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(2); randomTurn(); turnAtEdge(); tryToEatPatrick(); } /** * With a 20% probability, turn a bit right or left. */ public void randomTurn() { if (Greenfoot.getRandomNumber(100) < 20 ) { turn(Greenfoot.getRandomNumber(40)-30 ); } } /** * If we reach the edge of the world, turn a little bit. */ public void turnAtEdge() { if(atWorldEdge()) { turn(7); } } /** * Check whether we can see Patrick. If we can, poison/eat him. */ void tryToEatPatrick() { if (canSee(Patrick.class)) { eat(Patrick.class); Greenfoot.playSound("SadTrombone.wav"); Greenfoot.stop(); PatrickWorld patrickworld = (PatrickWorld) getWorld(); //patrickworld.removeObject(this); patrickworld.gameOver(counter.getValue()); } } PATRICK CLASS import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Patrick is on a mission to collect as many Shamrocks as he can. For every ten Shamrocks he collects, he will * receive a Keg of beer. Patrick can be controlled by using the up, down, left and right keys. He is able to * collect/eat Shamrocks and food items that appear from time to time. * * @author () * @version (2.0) */ public class Patrick extends Animal { private int shamrocksEaten; private Counter counter; /** * Create Patrick with a reference to the counter in the game. */ public Patrick(Counter pointCounter) { counter = pointCounter; } /** * Act - do whatever Patrick wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeys(); lookForShamrock(); lookForBurger(); lookForFries(); lookForPizza(); lookForMuffin(); } public Patrick() { shamrocksEaten = 0; } /** * This method is called when the game is over to display the final score */ public void gameOver() { getWorld().addObject(new ScoreBoard(counter), getWorld().getWidth()/2, getWorld().getHeight()/2); } /** * Check whether we can see a Shamrock. If we can, collect/eat it. */ public void lookForShamrock() { if ( canSee(Shamrock.class) ) { eat(Shamrock.class); counter.add(1); Greenfoot.playSound("Bamboo.wav"); createNewShamrock(); createNewKeg(); } } /** * Create a new Keg and another Mushroom for every 10 shamrocks collected. */ private void createNewKeg() { if (counter.getValue() == 9 ) { getWorld().addObject(new Keg(), 80, 455); Greenfoot.playSound("Popcorn.wav"); getWorld().addObject(new Mushroom(), Greenfoot.getRandomNumber(480), Greenfoot.getRandomNumber(600)); } if (counter.getValue() ==19 ) { getWorld().addObject(new Keg(), 110, 455); Greenfoot.playSound("Popcorn.wav"); getWorld().addObject(new Burger(), Greenfoot.getRandomNumber(480), Greenfoot.getRandomNumber(600)); getWorld().addObject(new Mushroom(), Greenfoot.getRandomNumber(480), Greenfoot.getRandomNumber(600)); } if (counter.getValue() == 100) { getWorld().addObject(new Keg(), 350, 455); Greenfoot.playSound("ThatsAllFolks.wav"); Greenfoot.stop(); PatrickWorld patrickworld = (PatrickWorld) getWorld(); // patrickworld.removeObject(this); patrickworld.gameOver(counter.getValue()); } } } PATRICKWORLD import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) * @author () * @version (2.0) */ public class PatrickWorld extends World { private int counter; /** * Constructor for objects of class PatrickWorld. * */ public PatrickWorld() { super(600, 480, 1); prepare(); createMushrooms(5); createShamrocks(10); } /** * 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, 30, 450); Patrick patrick = new Patrick(counter); addObject(patrick, 307, 179); } /** * Create 15 Shamrocks and place them at random coordinates in the world. */ private void createShamrocks(int number) { for(int s = 0; s < number; s++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); addObject(new Shamrock(), x, y); } } /** * Create 5 Mushrooms and place them at random coordinates in the world. */ private void createMushrooms(int number) { for(int m = 0; m < number; m++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); addObject(new Mushroom(), x, y); } } /** * This method is called when the game is over to display the final score. */ public void gameOver(int score) { addObject (new ScoreBoard(score), getWidth() / 2, getHeight() /2 ); } }
erdelf erdelf

2014/4/5

#
not quite sure, but change the order of method callings in the tryToEat method place the stop at the end
Roz Roz

2014/4/5

#
Thank you for taking a look. I tried changing the order, but it didn't work.
erdelf erdelf

2014/4/5

#
would u mind posting the eat method `? and btw poste your code in code tags or press code under the reply box
Roz Roz

2014/4/5

#
Sure...thanks for the code tip....is this what you meant?
void tryToEatPatrick()
    {
        if (canSee(Patrick.class))
        {
            eat(Patrick.class);
            Greenfoot.playSound("SadTrombone.wav");
           Greenfoot.stop();
            
            PatrickWorld patrickworld = (PatrickWorld) getWorld();
           // patrickworld.removeObject(this);
            patrickworld.gameOver(counter.getValue());
                      
        }
           
          
        }
erdelf erdelf

2014/4/5

#
no, i mean the eat method you call it here in line 5
Roz Roz

2014/4/5

#
I can't seem to find it. It is inherited from class Animal which is from Greenfoot.
Roz Roz

2014/4/5

#
I found in online: Class Animal java.lang.Object extended by greenfoot.Actor extended by Animal void eat(java.lang.Class clss) Try to eat an object of class 'clss'.
erdelf erdelf

2014/4/5

#
ok, you know what `? just surround the entire method code with
if(getWorld !=null)
Roz Roz

2014/4/6

#
It wouldn't compile unless I added parentheses after getWorld, and unfortunately it still didn't work. void tryToEatPatrick() { if(getWorld() !=null) { if (canSee(Patrick.class)) { eat(Patrick.class); Greenfoot.playSound("SadTrombone.wav"); PatrickWorld patrickworld = (PatrickWorld) getWorld(); // patrickworld.removeObject(this); patrickworld.gameOver(counter.getValue()); Greenfoot.stop(); } } }
erdelf erdelf

2014/4/6

#
oh yeah i forgot the parantheses did your error message change `?
Roz Roz

2014/4/6

#
Nope, same terminal error - null pointer. Would it help if I showed you the code from the game I borrowed from?
erdelf erdelf

2014/4/6

#
one question, which code is in line 76 of the mushroom class`?
danpost danpost

2014/4/6

#
The problem is that when you try to eat Patrick in the Mushroom class, you try to get a value from a counter IN the Mushroom class which is never set to anything (it is still 'null' -- hence the exception). The Counter object you need to get the value from is in your Patrick class. Add a public 'get' method to return the Counter object (or its value) in the Patrick class and have your Mushroom class call it to apply to the GameOver argument.
danpost danpost

2014/4/6

#
This is another instance where the Animal class methods are of little use. Twice already a 'getIntersecting' method has been called for a Patrick object (once with 'canSee' and again with 'eat') and still you do not have a reference to the object that was eaten (which you will need, in this case). This is more like what your 'tryToEatPatrick' method should look like (the last line is tentative).
void tryToEatPatrick()
{
    Patrick patrick = (Patrick)getOneIntersectingObject(Patrick.class);
    if (patrick != null)
    {
        PatrickWorld patrickworld = (PatrickWorld)getWorld();
        patrickworld.removeObject(patrick);
        Greenfoot.playSound("SadTrombone.wav");
        Greenfoot.stop();
        patrickworld..removeObject(this);
        patrickworld.gameOver(patrick.getCounter().getValue());
    }
}
There are more replies on the next page.
1
2