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

2016/3/10

Scoreboard will not work!

browny87 browny87

2016/3/10

#
hi guys I followed a tutorial to make a scoreboard and it worked fine, I remade it in a different game and now it will not work, I know i'm being daft but I cannot for the life of me find the error in the code. Here is the code; Code from the counter; public class Counter extends Actor { int score = 0; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK)); } public void addScore() { score++; } } code from the object; public class Car extends Actor { /** * Act - do whatever the Car wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); eat(); } public void moveAndTurn() { if(Greenfoot.isKeyDown("right")) setLocation(getX()+1, getY()); if(Greenfoot.isKeyDown("left")) setLocation(getX()-1, getY()); if(Greenfoot.isKeyDown("down")) setLocation(getX(), getY()+1); if(Greenfoot.isKeyDown("up")) setLocation(getX(), getY()-1); } public void eat() { Actor FuelCanister; FuelCanister = getOneObjectAtOffset(0,0,FuelCanister.class); if (FuelCanister !=null) { World world; world = getWorld(); world.removeObject(FuelCanister); GameScreen gamescreen = (GameScreen)world; Counter counter = gamescreen.getCounter(); counter.addScore(); } } } GameScreen is the name of the world with the counter on, FuelCanister is the pickup and the Counter is Counter Any ideas?
browny87 browny87

2016/3/10

#
the code to set the counter up in the world ive used is; public class GameScreen extends World { Counter counter = new Counter(); /** * Constructor for objects of class GameScreen. * */ public GameScreen() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 446, 1); prepare(); } public Counter getCounter() { return counter; }
danpost danpost

2016/3/10

#
NVM
browny87 browny87

2016/3/10

#
yeah solved it, it gets called incorrectly in the prepare code, thanks!
danpost danpost

2016/3/10

#
I was just about to edit my post and ask you to show the 'prepare' method of your GameScreen class. It seems I was on the right path. Glad you fixed it.
You need to login to post a reply.