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

2017/1/19

how to count balls everytime a new ball is added

beyondFrustrated beyondFrustrated

2017/1/19

#
public class Spielfeld extends World { int xf = 20; int yf = 10; Counter counter = new Counter(); BallCounter ballcounter = new BallCounter(); private int balls = 0; public Spielfeld() { super(640, 480, 1); addObject (new Start(), 316,145); addObject (new Instructions(),322, 185); } public void act() { neuerBall(); addBricks(); gameOver(); } public void neuerBall () { if (Greenfoot.isKeyDown("space") && getObjects(Ball.class).size() == 0) { Ball ball = new Ball () ; addObject(ball, 320, 240) ; } } public void addBricks () { if (Greenfoot.mouseClicked(null)) { for(int i=0; i<1; i++) { if (getObjects(Brick.class).size() == 0) for(int p=0; p<48; p++) { Brick brick = new Brick (); addObject(brick, xf, yf); xf= xf+40; if (xf >= 640) { xf=20; yf= yf+45; } } yf=10; if (getObjects(Paddel.class).size()== 0) { Paddel paddel = new Paddel (); addObject(paddel, 320, 460); } } addObject( counter, 118, 461); addObject( ballcounter, 582, 458); } } public void gameOver() { if (balls > 3) { addObject (new GameOver(), 316,145); } } public void score() { counter.addScore(); } public void balls() { ballcounter.countBalls(); } } ---------------------------------- public class BallCounter extends Actor { private int balls; public BallCounter() { setImage (new GreenfootImage(200, 30)); updateBalls(); } public void act() { countBalls(); updateBalls(); } public void countBalls() { if (Greenfoot.isKeyDown("space")) { balls++; } updateBalls(); } public void updateBalls() { GreenfootImage img = getImage(); img.clear(); img.setColor(Color.WHITE); img.drawString("Balls: " + balls, 4, 20); } }
beyondFrustrated beyondFrustrated

2017/1/19

#
it always adds multiple balls instead of just one. so how would i write ´if a new ball is added, add one ball to the ballcounter?`
danpost danpost

2017/1/19

#
Remove 'countBalls();' from the act method of the BallCounter class and add the following line in the Spielfeld class in the 'neuerBall' method after creating the ball:
ballcounter.countBalls();
beyondFrustrated beyondFrustrated

2017/1/19

#
now it doesnt add a ball at all
danpost danpost

2017/1/19

#
beyondFrustrated wrote...
now it doesnt add a ball at all
Please show the methods you have changed.
You need to login to post a reply.