I want to duplicate all of the objects that have enough "energy". The code as it is written, objects duplicates multiple times because I cannot release the "d" key quick enough.
So I tried adding a boolean to keep track if the key was pressed. I also tried adding delay timer. Finally I tried using the delay method.
All three prevented the method from acting on all the objects. Only one of the objects duplicate.
public void duplicate()
{
if(Greenfoot.isKeyDown("d"))
{
World W = getWorld();
if(energy > 30)
{
int speedMutation = Greenfoot.getRandomNumber(3) - 1;
Creature dupCre = new Creature(speed + speedMutation);
W.addObject(dupCre, getX(), getY());
} else {
W.removeObject(this);
System.out.println(" Another one bites the dust: Mutation Failed");
}
}
}
