how to make scoreboard get sscore from counter ?
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; }
/** * 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); }
/** * 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(); }
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. }
/** * Constructor for objects of class CrabWorld. * */ public CrabWorld() { super(560, 560, 1); prepare(); }
CrabWorld w = (CrabWorld) getWorld(); Counter c = w.counter; //Then to get the score variable, you just use "c.getValue()"