I am trying to create a delay for the scoreboard so that the counter has enough time to finish counting before the scoreboard comes up. I put a delay before the scoreboard the only problem is it delays the entire game. Any suggestions?
This is what I have written in the Space class. I am using the counter that was already defined I just added a CountScore function
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.util.List;
/**
* Space. Something for rockets to fly in.
*
*
*
*/
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 3;
public Space()
{
super(600, 400, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
createStars(300);
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 380);
Explosion.initializeImages();
ProtonWave.initializeImages();
}
/**
* Add a score to the score counter.
*/
public void countScore(int count)
{
scoreCounter.add(count);
}
/**
* Add a given number of asteroids to our world. Asteroids are only added into
* the left half of the world.
*/
private void addAsteroids(int count)
{
for (int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
/**
* Adds a specified number of stars in space.
*/
private void createStars(int number)
{
GreenfootImage background = getBackground();
for(int i = 0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
int color = 150 - Greenfoot.getRandomNumber(100);
background.setColor(new Color(color,color,color));
background.fillOval( x, y, 2, 2);
}
}
/**
* This method is called when the game is over to display the final score.
*/
public void gameOver()
{
{
Greenfoot.delay(85);
addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2);
}
}
}
This is what I have written in the Asteroid class
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
/**
* A rock in space
*
*
*/
public class Asteroid extends SmoothMover
{
private Counter counter;
/** Size of this asteroid */
private int size;
/** When the stability reaches 0 the asteroid will explode */
private int stability;
public Asteroid(Counter scoreCounter)
{
counter = scoreCounter;
}
public Asteroid()
{
this(50);
}
public Asteroid(int size)
{
super(new Vector(Greenfoot.getRandomNumber(360), 2));
setSize(size);
}
public Asteroid(int size, Vector speed)
{
super(speed);
setSize(size);
}
public void act()
{
move();
}
/**
* Set the size of this asteroid. Note that stability is directly
* related to size. Smaller asteroids are less stable.
*/
public void setSize(int size)
{
stability = size;
this.size = size;
GreenfootImage image = getImage();
image.scale(size, size);
}
/**
* Return the current stability of this asteroid. (If it goes down to
* zero, it breaks up.)
*/
public int getStability()
{
return stability;
}
/**
* Hit this asteroid dealing the given amount of damage and adding the
* correct amount of points.
*/
public void hit(int damage) {
stability = stability - damage;
if(stability <= 0)
{
breakUp ();
}
}
/**
* Break up this asteroid. If we are still big enough, this will create two
* smaller asteroids and add 10 points to the score. If we are small already,
* just disappear and add 25 points to the score.
*/
private void breakUp()
{
Space space = (Space) getWorld();
Greenfoot.playSound("Explosion.wav");
if(size <= 16)
{
getWorld().removeObject(this);
space.countScore(25); // adds 25 points for removing an asteroid.
}
else
{
int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
double l = getMovement().getLength();
Vector speed1 = new Vector(r + 60, l * 1.2);
Vector speed2 = new Vector(r - 60, l * 1.2);
Asteroid a1 = new Asteroid(size/2, speed1);
Asteroid a2 = new Asteroid(size/2, speed2);
getWorld().addObject(a1, getX(), getY());
getWorld().addObject(a2, getX(), getY());
a1.move();
a2.move();
getWorld().removeObject(this);
space.countScore(10); //adds 10 points for splitting an asteroid.
}
}
}
This is a problem with that Counter class. One solution is just to stop it animating altogether, which removes the problem: use counter.setValue(counter.getValue() + amount) rather than counter.add(amount). Or you can get the scoreboard to make sure the counter shows the final result: the slightly odd code counter.setValue(counter.getValue()) will actually do this.
Don't use Greenfoot.delay(...) as, like you noticed, it delays everything. You need to introduce a counter/timer which you decrement (or increment) every act, until it times out, and then you put the scoreboard in the world.