I am trying to make a method that checks if two keys are pressed. If they are, it adds an object to the screen. It all works correctly except even if I only press the keys once, multiple of the same object is added to the same place. I only want one object to be added. Here is my code:
public class spawn1 extends spawns
{
/**
* Act - do whatever the spawn1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
spawn();
}
public void spawn()
{
int numWalkers1 = 0;
int numSpiders1 = 0;
int numCreepers1 = 0;
if (Greenfoot.isKeyDown("1") && Greenfoot.isKeyDown("w") && numWalkers1 < 1)
{
getWorld().addObject(new walker(), 110, 160);
numWalkers1 = 1;
}
if (Greenfoot.isKeyDown("1") && Greenfoot.isKeyDown("s") && numSpiders1 < 1)
{
getWorld().addObject(new spider(), 110, 160);
numSpiders1 += 1;
}
if (Greenfoot.isKeyDown("1") && Greenfoot.isKeyDown("c") && numCreepers1 < 1)
{
getWorld().addObject(new creeper(), 110, 160);
numCreepers1 += 1;
}
}
}

