In my game I've created a Counter in a world and I placed the counter also in the Game Over world. My problem is I can't get the score from the world in the gameOver world, so the counter starts every time at zero! How can I save the score in the main world and show it in the GameOver world? :)
The hud isn't important it's only the picture behind the Counter!
public class Counter extends Actor { public int score = 0; private int time = 0; public int getScore(){ return score; } public void act() { addScore(); // erhöht den Counter } public Counter() { score = 0; setImage(new GreenfootImage(400,120)); //Größe für Bild des Counters update(); } public void addScore() { time++; // Zeit +1 bis Counter um 1 hoch geht if (time == 62){ // wenn Zeit bei 62 ist geht Counter um 1 hoch score++; update(); // updatet das Bild des Counters time = 0; // stellt Zeit bis zum näachsten erhöhen des Counters auf null } } public void update() // erneuert das Bild nach jedem dazuzählen { GreenfootImage img = getImage(); img.clear(); // löscht Bild img.setColor(Color.BLACK); // setzt Farbe der Schrift auf Schwarz img.drawString("Score: " + score, 12, 60); // addiert den Score } }
public GameOver() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1600, 790, 1); Hud hud = new Hud(); addObject ( hud, 86, 747); Counter counter = new Counter(); addObject ( counter, 210, 752); prepare(); }
public level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1600, 800, 1); Hud hud = new Hud(); addObject(hud, 93, 753); hud.setLocation(86, 747); Counter counter = new Counter(); addObject ( counter, 210, 752); tank tank = new tank(); addObject(tank, 810, 494); prepare(); }