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

2013/10/29

Adding score when destroying enemies

Foyman Foyman

2013/10/29

#
I want to create a scoreboard where if I destroy an enemy, my score will go up. I had it so when an enemy score spawned it went up, but I don't want that. I put the code for adding score into the enemy code thinking that would work. It complied, but the game would stop when I destroyed the enemy
Actor shot = getOneIntersectingObject(Shot.class);
        if (shot != null)
        {
            getWorld().removeObject(shot);
            getWorld().removeObject(this);
            scoreboard.addScore(25);
        }
You need a reference to the scoreboard. Instead of the enemy class, have it in you're world class, and then access your world scoreboard with this (Make sure scoreboard is PUBLIC!!!):
(myWorldClassName)(getWorld()).scoreboard.addScore(25);
You need to login to post a reply.