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

2017/6/7

Score Counter

Venbha Venbha

2017/6/7

#
How to create a score counter?? in my counter.class i wrote import greenfoot.*; import java.awt.Color; public class Counter extends Actor { int score = 0; public void act() { setImage(new GreenfootImage("Score : " + score, 24, Color.BLUE, Color.GREEN)); } public void addScore() { score++; } } but its not working. ???????????????????????????
Super_Hippo Super_Hippo

2017/6/7

#
Do you ever call the 'addScore' method?
Yehuda Yehuda

2017/6/7

#
You have to make sure you're calling the addScore method and all on the same instance of Counter. Meaning that when you add the Counter into the world you first create it.
private Counter scoreCounter = new Counter();
// then add it
addObject(scoreCounter, x, y);
Now for the value to change you have to call the addScore method on that instance.
scoreCounter.addScore();
This is what has to be done for the Counter to work. If you want to add the score from a different class (besides from the World) then you have to get that instance from the world with a getter method.
Venbha Venbha

2017/6/8

#
Thank you I will try today. thanks
Venbha Venbha

2017/7/4

#
Super_Hippo wrote...
Do you ever call the 'addScore' method?
i created the subclass not imported
Super_Hippo Super_Hippo

2017/7/9

#
This doesn't really matter. You made a addScore method (you posted it above). If you just want to add one score point every act cycle, you can call the addScore method from the Counter's act method. If not, you need to access the counter object and call the addScore method on that object whenever you want to add points.
You need to login to post a reply.