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

2012/1/12

Scoreboard

programmer22 programmer22

2012/1/12

#
how to make scoreboard get sscore from counter ?
davmac davmac

2012/1/12

#
Show us your code.
Duta Duta

2012/1/12

#
YourWorldName w = (YourWorldName) getWorld(); //Replace YourWorldName with the name of your world class. You can change its name from "w" to whatever you want, just make sure to also change it in the next line
Counter c = w.getCounter(); // For more on this, see later on.

//Alright, now you have 2 options:
//One: in your Counter class, make your score variable public, then to get the score you do:
c.score;
//Two: in your Counter class, create the following method:
public int getScore()
{
    return score;
}
//Then to get the score, you do:
c.getScore();
//The difference between the two is that when you use the latter one, "score" can be private (if you want that for whatever reason)

//Also, in your world class (this might be done already, but in case):
//Create this field:
private Counter theCounter; //This doesn't _have_ to be private
//Then in your worlds constructor, replace
addObject(new Counter(), xLocation, yLocation);
//with
theCounter = new Counter();
addObject(theCounter, xLocation, yLocation);
//And finally add the following method to your world class:
public Counter getCounter()
{
    return theCounter;
}
programmer22 programmer22

2012/1/12

#
hey duta if i showed u my code would that help ? and if so which part and if not could you please go into explanation about the code i allready have that part about my options with get score but mine says get value
programmer22 programmer22

2012/1/12

#
also would u mind telling me where some of that stuff goes like the first line you gave me and worlds constructor???? also the c score where that go?
programmer22 programmer22

2012/1/12

#
nvm >.> ill just post my world code and you can tell me what to do from their .... 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); ScoreBoard scoreboard = new ScoreBoard(); addObject(scoreboard, 273, 302); scoreboard.setLocation(275, 279); Greenfoot.stop(); removeObject(scoreboard); } public void act() { addWorms(); } 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 } } } }
Duta Duta

2012/1/13

#
Before I post, I thought I'd just quickly point this out: Look at your prepare() method.
/**
     * 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);
        ScoreBoard scoreboard = new ScoreBoard();
        addObject(scoreboard, 273, 302);
        scoreboard.setLocation(275, 279);
        Greenfoot.stop();  
        removeObject(scoreboard);
  } 
A better version of this is the following:
/**
 * 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(); This line shouldn't be here, I'll talk about this later
    addObject(crab, 257, 300);
    
    //Creates 20 worms at random locations in the world
    for(int i = 0; i < 20; i++)
    {
        addObject(new Worm(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
    
    //Creates 2 lobsters at random locations in the world
    for(int i = 0; i < 2; i++)
    {
        addObject(new Lobster(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
    
    //Counter counter = new Counter(); Same with this as with the Crab crab = new Crab(); line.
    addObject(counter, 23, 22);
    
    //ScoreBoard scoreboard = new ScoreBoard(); Same with this as with the Crab crab = new Crab(); line.
    addObject(scoreboard, 275, 279);
    removeObject(scoreboard);
    
    Greenfoot.stop();  
}
With the Crab crab = new Crab(); line and the other 2, what you are doing by declaring them within the method is declaring them as local variables. This means that when the method ends, they will be deleted - to get around this just move those lines to a different location and they'll be declared as fields instead - fields aren't deleted until the object they are in gets deleted - and as this is in your world class, the world never gets deleted so its fine. The following code is from the top of your class:
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
{
    //Declare your fields here:
    Crab crab = new Crab();
    Counter counter = new Counter();
    ScoreBoard scoreboard = new ScoreBoard();

    /**
     * Constructor for objects of class CrabWorld.
     * 
     */
    public CrabWorld()
    {    
        super(560, 560, 1); 

        prepare();
    }

    //Other methods omitted.
}
Okay, now for some of your other questions. - Where is the worlds constructor? This is the world's constructor:
    /**
     * Constructor for objects of class CrabWorld.
     * 
     */
    public CrabWorld()
    {    
        super(560, 560, 1); 

        prepare();
    }
- Where does c.score go? When I said you have 2 options, I mean you can do the first thing I said (in your Counter class, make your score variable public), OR do the second thing I said (with the getScore() method - you already have this with your getValue() method you say, so just go with this). As you've got the second thing I said, this means that to access the score of the counter you just do:
CrabWorld w = (CrabWorld) getWorld();
Counter c = w.counter;
//Then to get the score variable, you just use "c.getValue()"
programmer22 programmer22

2012/1/16

#
just saying but i did that crab crab thing u said and it didnt work so i left my world class the same so the last thing you said i would have to do in order for it to have the right score correct and if so heres my code because when i put in c.getValue() it didnt work said something about not knowing what c is and im still lost on Counter c = w.getCounter(); where would i put that ? like whats confusing me is after you put in a method like: public int getScore() 09.{ return score; } //Then to get the score, you do: c.getScore(); would i put c score in with the rest or what? anyway heres my counter code import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Font; /** * Counter that displays a number. * * @author Michael Kolling * @version 1.0.1 */ public class Counter extends Actor { private int value = 0; private int target = 0; private String text; private int stringLength; public Counter() { this(""); } public Counter(String prefix) { text = prefix; stringLength = (text.length() + 2) * 24; setImage(new GreenfootImage(stringLength, 32)); GreenfootImage image = getImage(); Font font = image.getFont(); image.setFont(font.deriveFont(24.0F)); // use larger font updateImage(); } public void act() { if(value < target) { value++; updateImage(); } else if(value > target) { value--; updateImage(); } } public void add(int score) { target += score; } public void subtract(int score) { target -= score; } public int getValue() { return value; } /** * Make the image */ private void updateImage() { GreenfootImage image = getImage(); image.clear(); image.drawString(text + value, 1, 18); } }
You need to login to post a reply.