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

2013/4/18

Set a counter for the points

JesusJesus JesusJesus

2013/4/18

#
Hey, I made this Little Scenario : (Click for open the game) Now i would like to set a Counter to Show the score. It should add 1 Point for killing a Spider and 3 Points for destroying an enemyship. Is it possible, that anyone can make me such a Counter on this game (You can open it on Greenfoot) or that anyone can tell me how to create this Counter? Because i have nearly no idea how to do this. Thank you.
danpost danpost

2013/4/18

#
You can import a Counter class by going to the menubar and selecting Edit>Import class... . Select the Counter class and click 'OK'. Now, in the world class constructor, you need to create and add the counter object into the world. Next, you need to tell the counter to add one (or three) to it when a Spider or an enemyship is destroyed. Add a method to the class that destroys Spiders and enemyships to bump the counter (replace 'WorldName' with the name of your world):
private void bumpCounter(int amount)
{
    WorldName world;
    world = (WorldName)getWorld();
    Counter counter;
    counter = (Counter)world.getObjects(Counter.class).get(0);
    counter.add(amount);
}
Finally, when a Spider object is detected and killed, call 'bumpCounter(1);' and when an enemyship is detected and destroyed, call 'bumpCounter(3);'. NOTE: if the Spider and enemyship objects are destroyed by a bullet or projectile, bump the counter before removing the bullet or projectile from the world.
JesusJesus JesusJesus

2013/4/18

#
Thank you very much :D
You need to login to post a reply.