in my game ive been trying to continue my counters score to the next level but its not working what have i done wrong, or what i am missing.
heres my Playerx1 world
heres my level_2 world
heres my Player_p1 actor
and heres my counter world
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Playerx1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Playerx1 extends World
{
public Counter counter; //save the counter in a field to get access from outside
/**
* Constructor for objects of class CrabWorld.
*
*/
public Playerx1()
{
super(1000, 660, 1);
prepare();
}
public void act()
{
if (getObjects(Player_p1.class).isEmpty()){
Greenfoot.setWorld(new GameOverScreen());
}
if (getObjects(Peasant.class).isEmpty()){
Greenfoot.setWorld(new level_2(counter));
}
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
//when creating
counter = new Counter();
addObject(counter, 54, 624);
Player_p1 player_p1 = new Player_p1(counter);
addObject(player_p1, 240, 304);import greenfoot.*;
/**
* Write a description of class level_2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class level_2 extends Playerx1
{
private Counter counter;
public level_2(Counter pointCounter)
{
counter = pointCounter;
}
/**
* Constructor for objects of class level_2.
*
*/
public level_2()
{
prepare();
}
public void act()
{
if (getObjects(Player_p1.class).isEmpty()){
Greenfoot.setWorld(new GameOverScreen());
}
if (getObjects(Peasant.class).isEmpty()){
Greenfoot.setWorld(new level_3(counter));
}
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Cops cops = new Cops();
addObject(cops, 62, 328);
cops.setLocation(291, 410);
cops.setLocation(307, 436);
cops.setLocation(298, 443);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player_p1 extends Actor
{
private Counter counter;
public Player_p1(Counter pointCounter)
{
counter = pointCounter;
}
public void act()
{
moveAndTurn();
eat();
}
public void moveAndTurn()
{
if (Greenfoot.isKeyDown("a"))
{
turn(-7);
}
if (Greenfoot.isKeyDown("d"))
{
turn(7);
}
if (Greenfoot.isKeyDown("w"))
{
move(7);
}
if (Greenfoot.isKeyDown("s"))
{
move(-7);
}
}
public void eat()
{
Actor peasant;
peasant = getOneObjectAtOffset(0, 0, Peasant.class);
if (peasant != null)
{
World world;
world = getWorld();
world.removeObject(peasant);
counter.add(1);
Greenfoot.playSound("eating.wav");
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* A simple counter with graphical representation as an actor on screen.
*
* @author mik
* @version 1.0
*/
public class Counter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
private int score = 0; //declare 'score' as private
public int getScore() //to be able to access this field from outside this class
{
return score;
}
/**
* Create a new counter, initialised to 0.
*/
public Counter()
{
background = getImage(); // get image from class
value = 0;
target = 0;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
/**
* Add a new score to the current counter value.
*/
public void add(int score)
{
target += score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return value;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("Score : " + value, 22, Color.GREEN, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
