if greenfoot.isKeyDown-up then a+=1
here the number is increasing continuously. i want; when the up key is down then a should be 1 and again if the up key is down then the a should be 2 and so on....
What should i write
Plz write the code
private boolean upKeyDown; // for state of key private int a; public void act() { if (upKeyDown != Greenfoot.isKeyDown("up")) // key state changed { upKeyDown = !upKeyDown; if (upKeyDown) a++; // to down state } }
//global variable private boolean pressed = false; //in act method if(Greenfoot.isKeyDown("up") && !pressed){ a++;//does the same as a += 1; pressed = true; } else{ pressed = false; }
//global variable private boolean pressed = false; //in act method if(Greenfoot.isKeyDown("up")){ if(!pressed){ a++; pressed = true; } } else{ pressed = false; }