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

2015/1/16

Help Needed With Score Going Up To 15

Manda_x Manda_x

2015/1/16

#
I want my counter to go up to 15 (when my main actor collects 15 targets) how do I achieve this?
danpost danpost

2015/1/16

#
You will need to add the following method to your Counter class:
public int getPoints()
{
    return points;
}
Then, after calling 'addPoints', you can use and 'if' block with the condition '((Counter)getWorld().getObjects(Counter.class).get(0)).getPoints() == 15'. To help simplify things:
Counter counter = ((Counter)getWorld().getObjects(Counter.class).get(0);
counter.addPoints();
if (counter.getPoints() == 15)
{
    /** whatever you want do do */
}
Manda_x Manda_x

2015/1/16

#
i'll try that now, thank you
Manda_x Manda_x

2015/1/16

#
where do i put Counter counter.... in my code after i add "public int get points"
{
    int points=0;
    Counter counter = ((Counter)getWorld().getObjects(Counter.class).get(0));
    counter.addPoints();
    if(counter.getPoints() ==30)

    public void updateImage()
    {
        setImage(new GreenfootImage("Score:"+ points,35,Color.GREEN,Color.BLACK));
    }

    public void addPoint()
    {
        points++;
        updateImage();
    }

    public Counter()
    {
        updateImage();
    }

    public int getPoints()
    {
        return points;
    }
    
}
danpost danpost

2015/1/16

#
Manda_x wrote...
where do i put Counter counter.... in my code after i add "public int get points"
No. The six-line snippet is what you use in the class of the character that collects targets. The first line gets a reference to the Counter object so that the following lines can execute the 'getPoints' and'addPoints' methods on it.
Manda_x Manda_x

2015/1/19

#
nop, I don't understand.
davmac davmac

2015/1/19

#
Manda_x wrote...
nop, I don't understand.
lines 3-5 of what you posted in the Counter class, do not belong in that class. You need to put those lines at whatever point it is that you want to increase the count - that is, whenever your main actor collects a target. This will probably be in the main actor class.
You need to login to post a reply.