How I can make a score board? I want to keep the score and increase it when the character "eats" prime number and reset at 0 when you start a new game.
I wrote this, but I am not sure how to continue:
int score = 0;
public void act()
{
// Add your action code here.
setImage(new GreenfootImage("Score: " + score, 24, Color.BLUE, Color.WHITE));
}
public void addScore()
{
score++;
}
That is about all you will need for the ScoreCounter class. Now, you just need to have the world create one and add it to itself. If the world keeps a reference to the ScoreCounter object and has a 'getter' method that returns that object, the player can then adjust its score when needed.
I added the score in the world:
Score score = new Score();
addObject(score, 1080, 680);
But now how can I make it change when something is happening and to reset to 0 when a new game is started?
Also I made this function in world:
public Score getScor()
{
return score;
}
And in the Character class I wrote:
if(isTouching(Number_2.class))
{
getWorld().removeObjects(getWorld().getObjects(Number_2.class));
//sum++;
World myWorld = getWorld();
MyWorld world = (MyWorld)myWorld;
Score score = world.getScore();
score.addScore();
}
And I have an error at world.getScore(); It says that cannot find symbol method getScore()
By reading your problem it is saying cannot find symbol method getScore(), because you misspelt the first line, putting getScor(). This is because when you are calling getScore(), it will not return the score because the method is called getScor(). To fix this just put an e at the end of getScore().
Also just as a something so you can make your code clearer, which means that people here can reference lines in the code to help you, when typing a message and putting code, when you are about to put code in, see the options below the box for typing in, select code, and then type, or paste it in there. This way people can refer to specific lines to help you and make it simple.