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

2019/3/23

Scoreboard not working

dammnn dammnn

2019/3/23

#
I want to increase the Scoreboard if my Actor shoots an enemy. It doesn't work, here are my codes: First, the world: public class Level1 extends World { Scoreboard scoreboard = new Scoreboard(); public Level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 600, 1); populateWorld(); scoreboard = new Scoreboard(); } public void populateWorld() { addObject(new Mario(), 900, 100); for(int i = 0; i < 10; i++) { addObject(new Pilz(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(600)); } for(int i = 0; i < 4; i++) { addObject(new Shyguy(), Greenfoot.getRandomNumber(500), Greenfoot.getRandomNumber(600)); } addObject(new Scoreboard(), 50, 20); } public Scoreboard getScoreboard() { return scoreboard; } } the Actor Scoreboard: public class Scoreboard extends Actor { private int score = 0; public void updateImage() { setImage(new GreenfootImage("Score: "+score, 25, Color.WHITE, new Color(0,0,0,0))); } public Scoreboard() { updateImage(); } public void addScore() { score++; updateImage(); } } and the Fireball Actor, which kills the enemy: public class Feuerball extends Actor { public int Scoreboard = 0; public void act() { move(12); if (isTouching(Shyguy.class) || isTouching(Shyguy2.class)) { ((Level1)getWorld()).getScoreboard().addScore(); removeTouching(Shyguy.class); removeTouching(Shyguy2.class); getWorld().removeObject(this); // Greenfoot.playSound(_soundFile_) } else if (isAtEdge()) getWorld().removeObject(this); } } can anybody find the prob?
danpost danpost

2019/3/23

#
The Scoreboard object you add into the world is not the same one you are scoring on.
dammnn dammnn

2019/3/23

#
But how can I fix that? I almost tried everything...
danpost danpost

2019/3/23

#
dammnn wrote...
But how can I fix that? I almost tried everything...
// change
addObject(new Scoreboard(), 50, 20);

// to
addObject(scoreboard, 50, 20);
dammnn dammnn

2019/3/23

#
It works! Thanks a lot
You need to login to post a reply.