I tried to make it so if there was a collision between Projectile and Target and the Image (for Target) was a certain image then the score would increase by 1 and it was change to a different image. Everything works except the score, which doesn't increase.
Here is the code for the World
Here is the code for the Target
Here is the code for the Scoreboard
If someone could help that'd be great.
Thanks!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class ProjectileWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ProjectileWorld extends World
{
private long startMillis = 0;
private int numFrames = 0;
private final int groundHeight;
public static final double MAX_FORCE = 8.5;
public static final double MIN_FORCE = 3.5;
public static final double GRAVITY = 0.1;
public static final int LOWEST_ANGLE = 30;
private Player[] players;
private int curPlayer;
private int turnCountdown;
private Scoreboard sb;
/**
* Constructor for objects of class ProjectileWorld.
*
*/
public ProjectileWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1, false);
groundHeight = 550;
getBackground().setColor(new java.awt.Color(0.8f, 0.95f, 1.0f, 1.0f));
getBackground().fill();
getBackground().setColor(new java.awt.Color(0.4f, 0.7f, 0.4f, 1.0f));
getBackground().fillRect(0, groundHeight, getWidth(), getHeight());
sb = new Scoreboard();
addObject(sb,70,30);
addObject(new Target(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight() - 250) + 200);
players = new Player[] {new HumanPlayer()};
for (Player p : players)
{
addObject(p, Greenfoot.getRandomNumber(getWidth() - 100), groundHeight);
}
curPlayer = 0;
turnCountdown = 10;
}
public void updateScore()
{
sb.changeScore();
}
public void started()
{
numFrames = 0;
startMillis = System.currentTimeMillis();
}
private double t;
public void act()
{
numFrames += 1;
t = getTimePerFrame();
if (turnCountdown > 0)
{
turnCountdown -= 1;
if (turnCountdown == 0)
{
players[curPlayer].startTurn();
}
}
}
public double getTimePerFrame()
{
return (double)(System.currentTimeMillis() - startMillis) / (1000.0 * (double)(numFrames));
}
public boolean hitsGround(double startX, double startY, double endX, double endY)
{
return endY > groundHeight;
}
public boolean hitsTarget(double startX, double startY, double endX, double endY)
{
return endY > groundHeight;
}
public void landed()
{
curPlayer = (curPlayer + 1) % players.length;
turnCountdown = 10;
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class target here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Target extends Actor
{
private GreenfootImage target = new GreenfootImage("teddybear.png");
private GreenfootImage dead = new GreenfootImage("skull.png");
/**
* Act - do whatever the target wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkCollision();
}
public void checkCollision()
{
Actor collided;
collided = getOneIntersectingObject(Projectile.class);
if (collided != null)
{
if (getImage() == target)
{
ProjectileWorld world = (ProjectileWorld)getWorld();
world.updateScore();
}
setImage(dead);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Scoreboard here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Scoreboard extends Actor
{
private int score = 0;
public Scoreboard()
{
GreenfootImage img = new GreenfootImage(100,30);
img.drawString("Score: " + score, 5, 25);
setImage(img);
}
public void changeScore()
{
score = score + 1;
GreenfootImage img = getImage();
img.clear();
img.drawString("Score: " + score, 5, 25);
}
}

