I need help on trying to get my score counter to work I have looked at many other forums for this and still nothing. I am trying to make it when I pick up a Goldasteroid the score will add up 5 and if I destroy an Alienship the score will add 10 points here is what I have:
ScoreBoard:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
/**
* The ScoreBoard is used to display results on the screen. It can display some
* text and several numbers.
*
* @author M Kolling
* @version 1.0
*/
public class ScoreBoard extends Actor
{
public static final float FONT_SIZE = 48.0f;
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
/**
* Create a score board with dummy result for testing.
*/
public ScoreBoard()
{
this(100);
}
/**
* Create a score board for the final result.
*/
public ScoreBoard(int score)
{
makeImage("Game Over", "Score: ", score);
}
/**
* Make the score board image.
*/
private void makeImage(String title, String prefix, int score)
{
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(new Color(255,255,255, 128));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(0, 0, 0, 128));
image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
Font font = image.getFont();
font = font.deriveFont(FONT_SIZE);
image.setFont(font);
image.setColor(Color.WHITE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200);
setImage(image);
}
}
Counter:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Graphics;
/**
* Counter that displays a text and number.
*
* @author Michael Kolling
* @version 1.0.1
*/
public class Counter extends Actor
{
private static final Color textColor = new Color(255, 180, 150);
private int value = 0;
private int target = 0;
private String text;
private int stringLength;
public Counter()
{
this("");
}
public Counter(String prefix)
{
text = prefix;
stringLength = (text.length() + 2) * 10;
setImage(new GreenfootImage(stringLength, 16));
GreenfootImage image = getImage();
image.setColor(textColor);
updateImage();
}
public void act() {
if(value < target) {
value++;
updateImage();
}
else if(value > target) {
value--;
updateImage();
}
}
public void add(int score)
{
target += score;
}
public int getValue()
{
return value;
}
/**
* Make the image
*/
private void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 12);
}
}
Redship:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Redship here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Redship extends SmoothMover
{
public int score;
private static final int gunReloadTime = 15;
private int reloadDelayCount;
public Redship()
{
reloadDelayCount = 5;
//addForce(new Vector(13, 0.3));
}
/**
* Act - do whatever the Redship wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
//move(3);
checkKeyPress();
reloadDelayCount++;
//score();
lookForGoldastroid();
}//end act
/**
* Checks for a pressed key, if this is true it will exeacute an action.
*/
public void checkKeyPress()
{
if(Greenfoot.isKeyDown("d"))//right
{
turn(4);
}//end if
if(Greenfoot.isKeyDown("a"))//left
{
turn(-4);
}//end if
if(Greenfoot.isKeyDown("s"))//slow down
{
move(-2);
}//end if
if(Greenfoot.isKeyDown("w"))//speed up
{
move(1);
}//end if
if(Greenfoot.isKeyDown("space"))
{
fire();
}//end if
}//end checkKeyPress
/**
* Fires a laser if ready to fire.
*/
public void fire()
{
if(reloadDelayCount >= gunReloadTime)
{
Laser laser = new Laser (getMovement().copy(), getRotation());
getWorld().addObject (laser, getX(), getY());
laser.move ();
reloadDelayCount = 0;
}//end if
}//end fire
/**
* Check to see if we are touching a Goldastroid.
* If so then pick it up and add 50 points to score.
*/
public void lookForGoldastroid()
{
if (canSee(Goldastroid.class))
{
pickUp(Goldastroid.class);
score++;
getWorld().addObject(new Goldastroid(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
}//end if
}//end lookForGoldastroid
}//end class
Laser:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Laser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Laser extends SmoothMover
{
private static final int damage = 1;
private int life = 90;
/**
* Laser will destroy Alienship if it hits them.
*/
public void act()
{
if(life <= 0)
{
getWorld().removeObject(this);
}
else
{
life--;
move(10);
checkAlienshipHit();
}//end if/else
}//end hitAlienship
public Laser(Vector speed, int rotation)
{
setRotation(rotation);
addForce(new Vector(rotation, 15));
}//end Laser
public void removeAtEdge()
{
if ( atWorldEdge() )
{
getWorld().removeObject(this);
}
}//end removeAtEdge
/**
* Checks if laser has hit Alienship
*/
public void checkAlienshipHit()
{
Alienship alienship = (Alienship) getOneIntersectingObject(Alienship.class);
if(alienship != null)
{
getWorld().removeObject(this);// remove laser
alienship.hit(damage);
}//end if
}//end checkAlienshipHit
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}//end atWorldEdge
}//end class
Aienship:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Alienship here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Alienship extends Aliens
{
private int health;
private int stability;
private int spawnTimer = 10;
/**
* Act - do whatever the Alienship wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
turnAtEdge();
randomTurn();
//move(2);
lookForRedship();
runSpawnTimer();
}//end act
public void healthLevel()
{
health = 1;
stability = health;
this.health = health;
}//end health
private void runSpawnTimer()
{
spawnTimer = (spawnTimer+1)%1000;
if(spawnTimer== 0) spawnAlienship();
}//end runSpawnTimer
private void spawnAlienship()
{
getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
}
public void turnAtEdge()
{
if(atWorldEdge())
{
turn(Greenfoot.getRandomNumber(10));
}//end if
}//end turnAtEdge
public void randomTurn()
{
if(Greenfoot.getRandomNumber(100) > 90)
{
turn(Greenfoot.getRandomNumber(90)-45);
}//end if
}//end randomTurn
public void lookForRedship()
{
if(canSee(Redship.class))
{
destroy(Redship.class);
Greenfoot.playSound("Blast.wav");
Greenfoot.stop();
}
}
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
//end if/else
}//end atWorldEdge
public int getStability()
{
return stability;
}
public void hit(int damage)
{
stability = stability - damage;
if(stability <= 0)
destroy ();
}//end hit
public void destroy()
{
if(health <= 0)
{
((SpaceWorld)getWorld()).countScore();
getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
getWorld().removeObject(this);
}
}//end destory
}
Goldasteroid:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Goldastroid here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Goldasteroid extends Space
{
/**
* Act - do whatever the Goldasteroid wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
turn(3);
}//end act
}

