I have: x++ in an if statement, and it's is constantly going up when that if statement is acting. how do i make it go up once when i hit a button (the if statement is Greenfoot.isKeyDown);
You need to track the state of the button and incorporate it into your 'if' condition and add another 'if' -- one 'if' for button press and the other for button release.
You need to track the state of the button and incorporate it into your 'if' condition and add another 'if' -- one 'if' for button press and the other for button release.
I believe this makes the assumption that the answer to my second question would be "yes, increment the variable twice if the key is pressed twice".
@davmac, in this case, I am convinced that there was sufficient evidence given to conclude that nothing was misleading. What was given suggests something like this:
I think you are probably right, but I wanted to clarify, because the question says:
How to make an int go up once
With your suggested solution, the value might be incremented more than once (it will be incremented each time the key is pressed). That is why I asked my question - I wanted to determine whether the asker really meant "once" or "once per keypress".
Add an instance boolean field to the class for tracking the state of the button -- call it 'buttonDown' (or you can be more specific and give the button name in the field name; like 'spaceDown' or 'xDown'). The two conditions for incrementing on a button down action would be (1) the 'buttonDown' field is currently 'false' and (2) the 'isKeyDown' method returns 'true'. The two conditions for the button up action would be (1) the 'buttonDown' field is currently 'true' and (2) the 'isKeyDown' method returns 'false'. In both 'if' blocks, you will change the value of the 'buttonDown' field and in the first 'if' block, you will also increment the value of 'x'.