I am trying to create levels of the asteroid game in the greenfoot book and am stuck. When the scenario initializes, two asteroids are created in the space class. Here is the code for that:
public class Space extends World
{
private int startAsteroids = 2;
addAsteroids(startAsteroids);
}
The addAsteroids() method code is this:
/**
* 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);
}
}
Then there is a method in the Asteroid class which splits the asteroid into smaller pieces, or removes it from the world when it reaches a certain size called breakUp(). The breakUp() method is called from the hit() method. here is that code:
/**
* Hit this asteroid dealing the given amount of damage.
*/
public void hit(int damage) {
stability = stability - damage;
if(stability <= 0){
breakUp ();}
}
I was thinking of adding a new method called levelUp() after this breakUp() line of code. There is a method called numberOfObjects() in the world class which returns the number of actors in the world, but there are no examples of how to use it in the book, so I wasn't sure how to implement it. Here is what I have so far:
/**
* Adds one more asteroid each time a level is completed.
*/
private void levelUp()
{
int asteroids = numberOfObjects();
if (asteroids == 0)
{
Space space = (Space) getWorld();
space.addAsteroids();
}
}
public int numberOfObjects();
{
return asteroids;
}
I was just trying to get the game to add asteroids to begin with by calling the addAsteroids() method from the space class and then I was going to try to make a loop counter to add one more to that each time. I got an error message saying "method addAsteroids in class Space cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length.
Any ideas? Thanks for any help!

