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

2017/7/1

Creating a score

Witcherblood Witcherblood

2017/7/1

#
Hi, I want to create a score for my game. The problem is that I have an actor crosshair with the code to kill the targets, but I need the number, how many targets are getting shot, in the MyWorld class to display it as I want. How can I get access to that number? Thanks in advance.
danpost danpost

2017/7/2

#
Use the following to get the World object:
MyWorld world = (MyWorld)getWorld();
Because 'world' is of type MyWorld, you can access any public fields or methods in the MyWorld class. For example, you can follow the line with:
world.score++;
if the 'int score' field was public; or, you could have a public method:
public void bumpScore()
{
    score++;
    updateScoreDisplay(); // or code to update the display of the score
}
in the MyWorld class and use:
world.bumpScore();
after getting the MyWorld reference in 'world' above.
Witcherblood Witcherblood

2017/7/2

#
Ok, thank you. It works well. But how could I display it now, because it is int and not a String?
danpost danpost

2017/7/2

#
Witcherblood wrote...
how could I display it now, because it is int and not a String?
You should probably check out my Value Display Tutorial scenario. If you cannot view that, it can also be found here
Witcherblood Witcherblood

2017/7/2

#
Thank you danpost! It works perfect, as I wanted it. You were a great help to me.
You need to login to post a reply.