I am trying to make a code that add one to a variable if you click the space bar. The code I wrote made the variable keep increasing if i hold the space bar but I don't want it to do that


1 2 3 4 5 6 7 8 9 | private boolean spaceDown = false ; // last "space" key state private int variable = 0 ; // the int field being increased public void act() { if (spaceDown != Greenfoot.isKeyDown( "space" )) { // is there a change in state of the "space" key spaceDown = ! spaceDown; if (spaceDown) variable++; // if change is from up to down, increase value of variable } } |