Hi Guys,
i got my game finished but the score from World 1 doesn't transfere to World2. It just goes back to 0 again.
How do i write the code in my Subworld class to transfere the score? Thanks!
Here the Codes:
Subworld:
world2:
world1 is empty
my actor:
and the counter:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class subworld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class subworld extends World
{
Counter counter = new Counter("Score: ");
public subworld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(900, 600, 1);
setPaintOrder(ScoreBoard.class,ScoreBoard2.class, hase.class, pilz.class, dog.class, Counter.class);
prepare();
}
private int counterspawn;
private int colorTimer = -1;
private void prepare()
{
hase hase = new hase();
addObject(hase, 275, 250);
dog dog = new dog();
addObject(dog, 603,365);
dog.setLocation(823,515);
hase.setLocation(252,222);
addObject(counter, 100, 560);
}
public void act()
{
runcounterspawn();
if(colorTimer>0)
{
colorTimer--;
if (colorTimer==0)
{
setBackground("grass.png");
colorTimer--;
}
}
}
public void counteat()
{
counter.add(10);
}
public void changeColor()
{
colorTimer=150;
setBackground("mushroombunt.png");
}
private void runcounterspawn()
{
counterspawn = (counterspawn+1)%300;
if (counterspawn == 0) spawnPilz();
}
private void spawnPilz()
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new pilz(), x, y);
}
public void gameOver()
{
addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
Greenfoot.playSound("zonk.mp3");
Greenfoot.stop();
}
public void changeLevel()
{
Greenfoot.setWorld(new world2());
}
public void nextlevel()
{
Greenfoot.playSound("clap.mp3");
changeLevel();
}
public void win()
{
addObject(new ScoreBoard2(counter.getValue()), getWidth()/2, getHeight()/2);
Greenfoot.playSound("clap.mp3");
Greenfoot.stop();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class world3 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class world2 extends subworld
{
/**
* Constructor for objects of class world2.
*
*/
public world2()
{
prepare();
}
private void prepare()
{
addObject(counter, 100, 560);
rock rock = new rock();
addObject(rock, 793, 93);
rock rock2 = new rock();
addObject(rock2, 802, 514);
rock rock3 = new rock();
addObject(rock3, 101, 511);
rock rock4 = new rock();
addObject(rock4, 104, 97);
rock4.setLocation(101, 90);
rock rock5 = new rock();
addObject(rock5, 449, 94);
rock rock6 = new rock();
addObject(rock6, 449, 515);
rock rock7 = new rock();
addObject(rock7, 450, 326);
rock7.setLocation(450, 322);
rock rock8 = new rock();
addObject(rock8, 801, 324);
rock8.setLocation(797, 318);
rock rock9 = new rock();
addObject(rock9, 104, 325);
rock9.setLocation(100, 321);
dog dog = new dog();
addObject(dog, 603,365);
dog.setLocation(823,515);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class hase here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class hase extends Actor
{
public int pilzEaten = 0;
public void act()
{
{if(Greenfoot.isKeyDown("up"))
{move(3);
if (!getIntersectingObjects(rock.class).isEmpty()) move(-4);}}
{if(Greenfoot.isKeyDown("down"))
{move(-3);
if (!getIntersectingObjects(rock.class).isEmpty()) move(+4);}}
{if(Greenfoot.isKeyDown("left"))
{turn(-3);}}
{if(Greenfoot.isKeyDown("right"))
{turn(3);}}
eat();
}
public void eat()
{Actor pilz;
pilz = getOneObjectAtOffset(0,0,pilz.class);
if(pilz !=null)
{
getWorld().removeObject(pilz);
Greenfoot.playSound("burp.mp3");
((subworld) getWorld()).changeColor();
((subworld) getWorld()).counteat();
pilzEaten++;
if (pilzEaten==3)
{
if (getWorld() instanceof world2)
{
((world2) getWorld()).win();
}
else if(getWorld() instanceof world1)
{
((world1) getWorld()).nextlevel();
}
}
}
}
}
import greenfoot.*;
import java.awt.Font;
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
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) * 16;
setImage(new GreenfootImage(stringLength, 24));
GreenfootImage image = getImage();
Font font = image.getFont();
image.setFont(font.deriveFont(24.0F));
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;
}
private void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 18);
}
}
