I was wondering what the code would be if I wanted a big asteroid to spawn 2 small ones when shot, and a medium spawn 3 small asteroids when shot (all layered on each other)
private size; //3=big, 2=medium, 1=small
public Asteroid(int type)
{
size = type;
GreenfootImage img = new GreenfootImage("Asteroid.png");
img.scale(size*img.getWidth(), size*img.getHeight()); //just an idea here
setImage(img);
setRotation(Greenfoot.getRandomNumber(360));
}public void damage()
{
if (--size>0) //prevent spawning of Asteroid when this was already small
{
for (int i=0; i<4-size; i++) //4-size means a big one spawn 2 while a medium one spawn 3.
{
getWorld().addObject(new Asteroid(size), getX(), getY());
}
}
getWorld().removeObject(this);
}public void act()
{
move(2);
Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
if (asteroid != null)
{
asteroid.damage();
getWorld().removeObject(this);
}
}