This site requires JavaScript, please enable it in your browser!
Greenfoot back
sanjo94
sanjo94 wrote ...

2014/1/20

How to carry points to next world.

sanjo94 sanjo94

2014/1/20

#
hello people. i want to carry my points to the next world. this is the counter code i have:
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);
    }
}
And this is what i have in my first World "Haven"
public class Haven extends World
{
public Haven()
    {    
       prepare();
     }
private void prepare()
    {
        Counter counter  = new Counter();    
        addObject (counter, 201, 14);
    }
}
And i add the same code to my next World "Haven2" thanks in advance!
danpost danpost

2014/1/20

#
When creating the new Haven2 world, do this:
Haven2 h2 = new Haven2(); // create new world with new counter object
// these next two lines get the counters of the current and the new worlds
Counter counter = (Counter)worldReference.getObjects(Counter.class).get(0);
Counter h2counter = (Counter)h2.getObjects(Counter.class).get(0);
h2counter.setValue(counter.getValue()); // passing the score
Greenfoot.setWorld(h2); // starting the new world
'worldReference' should be replaced with either 'this' (or nothing as 'this' would be default) or 'getWorld()' depending on what class the code is placed.
sanjo94 sanjo94

2014/1/20

#
IT WORKS !! :) Thank you very much. it really helped me out.
You need to login to post a reply.