This site requires JavaScript, please enable it in your browser!
Greenfoot back
alinasomcutean
alinasomcutean wrote ...

2017/4/21

Die

alinasomcutean alinasomcutean

2017/4/21

#
Hi! I have a die which should stop when you press "space" key. But I observerd that if you keep press the "space" key the die doesn't stop. The die should be stop at the same number while you keep press the "space" key and also should be stop few secondes after. What should I do?
danpost danpost

2017/4/21

#
Sounds like you need both a boolean field to track the state of the "space" key as well as an int field to time the delay after the key is released. If the int field is not zero, do not do anything except decrement the timer and exit the act method. Otherwise, the following should be done. The die should only be rolling when the boolean field indicates the key is up (timer is zero). Anytime the boolean field does not match the current state of the key, then the key was either just now pressed or just now released (there was a change in the state of the key) (again, timer is zero). Negate the boolean value and act on its new state -- if released, start the timer (give it a value of something in the range of 150 to 180); if pressed,, the die should stop (no code need be added for this here as we dealt with that at the beginning)..
You need to login to post a reply.