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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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 ; } } } |