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

2013/10/4

Help with this Code!

tartags11 tartags11

2013/10/4

#
No idea why the Scoreboard doesnt come up.
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class FruitWorld extends World
{
    private Counter scoreCounter;
    
    /**
     * Create the fruit world. Our world has a size 
     * of 560x560 cells, where every cell is just 1 pixel.
     */
    public FruitWorld() 
    {
        super(700, 700, 1);
        scoreCounter = new Counter();
        addObject( new Counter(), 650, 40 );
        addObject( new Player1(), 150, 100 );
        addObject( new Player2(), 550, 600 );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
    }
    private void endOfGame()
    {
        int player1Eaten = ((Player2) getObjects(Player2.class).get(0)).getPlayer1Eaten();
        if (player1Eaten == 1) 
        {
            gameOver ("Game Over");
        }
    }
    public void gameOver(String message) 
    {
        int value = ((Counter) getObjects(Counter.class).get(0)).getValue();
        addObject (new ScoreBoard ("Game Over", value), 320, getHeight()/2);
        Greenfoot.stop();
    }
}
erdelf erdelf

2013/10/4

#
u are trying to use the first object of a list with all objects of the player2 class in it, but u wrote it that way, that there arent any player2s in the world at this time
tartags11 tartags11

2013/10/4

#
I don't think I understand what you are saying? The counter and the scoreboard are different objects. The counter works fine. The scoreboard does not show up at the end of the game. The score value in the scoreboard is that of the counters value, that is why the counter class is in the code above the addObject Scoreboard method.
danpost danpost

2013/10/4

#
It is OK to declare variable fields for a class outside methods, but you can only set those fields to already known values or objects (usually constants or 'new' objects). Set the value of those fields when needed within the methods or constructor of the class. Explaining the error: you are getting an empty list from 'getObjects' because no player has yet been placed in the world (in fact, the world has not even begun to be constructed when these fields are initialized). You cannot get the first item (from 'get(0)') from a list that has no items in it. Hence, the error. Lines 17 and 18 is a common error for beginners. In line 17, you create a Counter object and set a reference to it. In line 18, you create a different Counter object and place it into the world. Changing the object you have a reference to will not change the one in the world (and vise-versa). Use the reference to add the object into the world, as follows:
scoreCounter = new Counter();
addObject(scoreCounter, 650, 40);
BTW, if you think that these fields (lines 6 through 8) will be updated when the fields in other classes change, they will not. What you could do instead, is keep a reference to the Player1 and Player2 objects (set by the constructor after the objects are created -- as with the Counter object above) and use that reference when needed within the class.
// field declarations
Counter scoreCounter;
Player1 player1;
Player2 player2;
// constructor
public FruitWorld()
{
    super(700, 700, 1);
    scoreCounter = new Counter();
    addObject(scoreCounter, 650, 40);
    player1 = new Player1();
    addObject(player1, 150, 100);
    player2 = new Player2();
    addObject(player2, 550, 600);
    // add fruit (rest of constructor
}
The fields can be assigned in there declarations by coding it as follows:
// field declarations (and initial assignments)
Counter scoreCounter = new Counter();
Player1 player1 = new Player1();
Player2 player2 = new Player2();
// constructor
public FruitWorld()
{
    super(700, 700, 1);
    addObject(scoreCounter, 650, 40);
    addObject(player1, 150, 100);
    addObject(player2, 550, 600);
    // add fruit (rest of constructor)
}
Now, throughout the rest of the world class, you can use 'player1.getFruitEaten()' or 'player2.getPlayer1Eaten()' or 'scoreCounter.getValue()' to get the specific values required. NOTE: original post that this reply was for has been edited; so, the line numbers do not match up and some information no longer pertains to the code that now resides in that post.
tartags11 tartags11

2013/10/7

#
Still not working. The end of game scoreboard does not appear. Right now I am more worried about the scoreboard not appearing then whether the score on the scoreboard is correct. Please Help.
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class FruitWorld extends World
{
    Counter scoreCounter;
    Player1 player1;
    Player2 player2; 
    
    /**
     * Create the fruit world. Our world has a size 
     * of 700x700 cells, where every cell is just 1 pixel.
     */
    public FruitWorld() 
    {
        super(700, 700, 1);
        scoreCounter = new Counter();
        addObject(scoreCounter, 650, 40);
        player1 = new Player1();
        addObject(player1, 150, 100);
        player2 = new Player2();
        addObject(player2, 550, 600);
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
        addObject( new Fruit(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700) );
    }
    private void endOfGame()
    {
        int player1Eaten = ((Player2) getObjects(Player2.class).get(0)).getPlayer1Eaten();
        if (player1Eaten == 1) 
        {
            gameOver ("Game Over");
        }
    }
    public void gameOver(String message) 
    {
        int value = ((Counter) getObjects(Counter.class).get(0)).getValue();
        addObject (new ScoreBoard ("Game Over", value), 350, 350);
        Greenfoot.stop();
    }
}
danpost danpost

2013/10/7

#
Insert the following line at line 35 of the code you provided (in the 'endOfGame' method:
System.out.println("Final score:  "+player1Eaten);
Post back with results (if you do not get a terminal window, state so; if you do, post what was displayed in it).
tartags11 tartags11

2013/10/8

#
Adding in the System.out.printIn code did not change anything. Game continues to run even after Player 1 is eaten. No terminal window appears however. Code compiles fine.
danpost danpost

2013/10/8

#
The the problem resides elsewhere. What code do you have in the Player2 class around the location where you increment the player1Eaten field. Put a 'System.out.println' statement there and see what happens. The fact that no terminal window did not display tells you that the 'endOfGame' method is not being called.
davmac davmac

2013/10/8

#
In fact, it's possible that all you need is to call the method in order to fix your problem. You could add an act() method to the world to do this.
public void act()
{
    endOfGame();
}
tartags11 tartags11

2013/10/8

#
I added in the act() method into the world and it worked!!!! Thanks so much!!
You need to login to post a reply.