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

2019/1/5

class issue in greenfoot for score counter

aa124 aa124

2019/1/5

#
m trying to add a score counter in greenfoot. The following code is in my bullets class. Each time a bullet hits the rocket, the score is supposed to increase by one. I've added the necessary code to the counter class and it works normally, however when I try to add the code to my bullet class, greenfoot says that it cant find myWorld. public void act() { if(!toRemove) { setLocation(getX()+vx,getY()); Actor actor=getOneIntersectingObject(Enemy.class); if(actor!=null) { ((Enemy)actor).destroyed(); GameScreen gameScreen = (GameScreen)myWorld; Counter counter = gameScreen.getCounter(); counter.addScore(); } if(getX()>getWorld().getWidth()+200) { toRemove=true; } } else { getWorld().removeObject(this); } } The part that doesn't work is GameScreen gameScreen = (GameScreen)myWorld; if(actor!=null) { ((Enemy)actor).destroyed(); GameScreen gameScreen = (GameScreen)myWorld; Counter counter = gameScreen.getCounter(); counter.addScore(); } This is in my counter method 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, greenfoot.Color.RED, greenfoot.Color.BLACK)); } public void addScore() { score++; } } Pls help
Zamoht Zamoht

2019/1/5

#
myWorld is probably not a declared reference in your class. You could try to replace "myWorld" with "getWorld()".
aa124 aa124

2019/1/5

#
I tried that and the code compiles however the score is not adding each time the bullet hits the other actors. What should I do?
Zamoht Zamoht

2019/1/5

#
Can you show me the GameScreen code? Seems like gameScreen.getCounter(); doesn't return the right counter.
aa124 aa124

2019/1/6

#
public class GameScreen extends World { private int delay = 0; 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(600, 400, 1, false); prepare(); } public Counter getCounter() { return counter; } public void act() { if(delay>0) { delay--; } else { delay=20; } if(delay==1) { int py=Greenfoot.getRandomNumber(getHeight()); addObject(new Enemy(-(2+Greenfoot.getRandomNumber(3))),getWidth()+200,py); } } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { addObject(new Rocket(), 100, 200); Counter counter = new Counter(); addObject(counter,300,30); }
aa124 aa124

2019/1/6

#
this is the counter code 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, greenfoot.Color.RED, greenfoot.Color.BLACK)); } public void addScore() { score++; } }
Zamoht Zamoht

2019/1/6

#
Please use the embedded code feature when listing code. Okay so the problem seems to be that you are declaring a new Counter in the prepare method "Counter counter = new Counter();". This is then different from the one in the top "private int delay = 0; Counter counter = new Counter();". In your getCounter() method you return the one in the top, which is not in the world, because you declare a new and place that in the world instead. To solve this change "Counter counter = new Counter();" to "counter = new Counter();" or remove the line completely. Hope this works and makes sense.
danpost danpost

2019/1/6

#
Remove the middle line in your prepare method:
Counter counter = new Counter();
You have already declared a Counter field called counter with a Counter object assigned to it (line 4 above). The line to be removed declares a local Counter variable called counter, whose name over-shadows that of the field. For the rest of the method "counter" refers to the local variable and to refer to the field you would need to use "this.counter".
You need to login to post a reply.