hello people.
i want to carry my points to the next world.
this is the counter code i have:
And this is what i have in my first World "Haven"
And i add the same code to my next World "Haven2"
thanks in advance!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class Counter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int points;
private int target;
/**
* Create a new counter, initialised to 0.
*/
public Counter()
{
background = getImage(); // get image from class
points = 0;
target = 0;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if(getWorld() instanceof Haven)
{
setLocation(getX(),getY()-1);
} else if(getWorld() instanceof Haven2)
{
setLocation(getX(),getY()-2);
} else if(getWorld() instanceof Haven3)
{
setLocation(getX(),getY()-2);
}
if (points < target) {
points++;
updateImage();
}
else if (points > target) {
points--;
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 points;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue)
{
target = newValue;
points = 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: " + points, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
public class Haven extends World
{
public Haven()
{
prepare();
}
private void prepare()
{
Counter counter = new Counter();
addObject (counter, 201, 14);
}
}
