Hi,
I am new to Greenfoot game development. I have an application with two actors and one of them has the code below. The problem is that the following statement is creating multiple actors instead of one! For each execution of this statement, it produces double the number of actors on the world instead of one:
getWorld().addObject (new Invador(),Greenfoot.getRandomNumber (560), Greenfoot.getRandomNumber (60));
public class Invador extends Actor
{
int counter=1;
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
public void eat(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
if(actor != null)
{
getWorld().removeObject(actor);
Greenfoot.playSound("bomb1.wav");
}
}
public void act()
{
counter++;
if(canSee(Boy.class))
{
eat(Boy.class);
getWorld().addObject (new Boy(),Greenfoot.getRandomNumber (560), 320);
}
if(counter % 300==0)
{
getWorld().addObject (new Invador(),Greenfoot.getRandomNumber (560), Greenfoot.getRandomNumber (60));
counter=1;
}
setRotation(90);
move(1);
}
}
Any help is appreciated. Thanks.