Hi,
I'm trying to do the following:
1. If clickwithMouse on ball, remove ball and decrease score by 1
2. If click on World, add a new ball and increase score by 1
I have managed to do these, except ... the score is being increased by 2. For example, I start with one ball in the World (score = 1). I click on World, second ball is added (score = 2), I click on World, third ball added (score = 4), I click on World, fourth ball (score =8) etc
Interestingly, to remove the last ball (the one that gave me a score of 8), I need to click on it 4 times! I've been looking at this for hours and no luck, can't figure out where I am going wrong. I appreciate any assistance you may give. I include my codes below.
Cheers,
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class MyWorld extends World
{
private Counter counter;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
addNewBall();
counter = new Counter();
addObject(counter, 151,6);
}
public void addNewBall()
{
int X = Greenfoot.getRandomNumber(600);
int Y = Greenfoot.getRandomNumber(300);
int direction = Greenfoot.getRandomNumber(360);
addObject(new Ball(direction),X,Y);
}
public void addScore()
{
counter.upScore();
}
public void subtractScore()
{
counter.downScore();
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Counter extends Actor
{
private int score;
public Counter() //the constructor class
{
score = 1; //initalise score to zero
setImage (new GreenfootImage(300,40));
update();
}
public void upScore()
{
score++;
update();
}
public void downScore()
{
score--;
update();
}
public void update()
{
GreenfootImage img = getImage();
img.clear();
img.setColor(Color.WHITE);
img.drawString("Score: " + score, 5, 30);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ball extends Actor
{
private int speed = 3;
/**
* A ball constructor - create a new ball
*/
public Ball(int direction)
{
turn(direction);
}
/**
* Act - do whatever the Ball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
move(speed);
if (atWorldEdge())
{
turn(153);
}
changeBalls();
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public boolean atWorldEdge()
{
if(getX() < 10 || getX() > getWorld().getWidth() - 10)
return true;
if(getY() < 10 || getY() > getWorld().getHeight() - 10)
return true;
else
return false;
}
public void changeBalls()
{
World world = getWorld();
MyWorld myWorld = (MyWorld) getWorld();
if (Greenfoot.mouseClicked(this))
{
world.removeObject(this);
myWorld.subtractScore();
}
//
if (Greenfoot.mouseClicked(world))
{
MouseInfo mouse = Greenfoot.getMouseInfo();
world.addObject(new Ball(10), mouse.getX(), mouse.getY());
myWorld.addScore();
}
}
}
