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

2017/4/4

Help return boolean if any key is/isn't down?

alittle_bit alittle_bit

2017/4/4

#
Hello, I was writing an if statement containing the following:
1
!(Greenfoot.isKeyDown(Greenfoot.getKey()) == true)
The goal is to retrieve a boolean result for an if statement to use. The line is supposed to supply a boolean value dependant on if any key is being held down. A 'null' value will not work in place of 'getKey()', if there was a way to easily replace this, that would be appreciated as the line does not work. Thank you for the help!
danpost danpost

2017/4/4

#
You can only use 'isKeyDown' method on any one specific key. The 'getKey' method will return a string representation of any keyboard input (like as when editing -- if a key is quickly pressed and released, one instance is sent to the keyboard buffer or if a key is pressed and held, after a moderate delay, one instance will be sent followed by multiple instances more quickly if continued to be held down. There may be exceptions to this behavior depending on the type of system you are using. At any rate, there is no specific method to detect any non-specified key being held down.
SR20 SR20

2017/5/11

#
1
Create another class and set the image to the gameover screen. Then however your game ends you position this gameover image on the screen. 2ez
Yehuda Yehuda

2017/5/12

#
What about doing 'getKey != null' to check that any key is pressed? (I've never used getKey before, so I don't really know what I'm suggesting.) Only seems to work once after the world is initialized/(re)set.
danpost danpost

2017/5/12

#
Yehuda wrote...
What about doing 'getKey != null' to check that any key is pressed?
The 'getKey' method only returns the value of keys that are released (or internally triggered as being released). Like I said, it is best suited for textual input (text editing type situations) or for when everything is on hold and a key must be pressed before any further actions can take place. It can feasibly be used only once per act step, meaning that it can be used either by the active World object or by an instance of an Actor class that only one object of the class is ever present in the active world -- but not by both at the same time.
Yehuda wrote...
Only seems to work once after the world is initialized/(re)set.
Again, it works once per act step; and only during run-time.
Yehuda Yehuda

2017/5/12

#
danpost wrote...
Again, it works once per act step; and only during run-time.
I tested it in the act method of the active World while the execution was running - and it only worked once after pressing "Reset".
danpost danpost

2017/5/12

#
Yehuda wrote...
danpost wrote...
Again, it works once per act step; and only during run-time.
I tested it in the act method of the active World while the execution was running - and it only worked once after pressing "Reset".
Show the class code you used.
Yehuda Yehuda

2017/5/12

#
Last time I tested it I just changed an already existing 'if' statement which switched the world - when I returned back to the original world it didn't work. Just now I made it pause the execution and it worked. But pressing the "Tab" button didn't do anything. I didn't test every button but I purposely tested the "Tab" button because I know that it doesn't work by isKeyDown even though it's in the API (Class Greenfoot). I didn't post any code since I only saw your post after posting mine.
danpost wrote...
Like I said, it is best suited for textual input (text editing type situations) or for when everything is on hold and a key must be pressed before any further actions can take place.
Well this topic is trying to see if any key was pressed.
alittle_bit wrote...
The line is supposed to supply a boolean value dependant on if any key is being held down.
You need to login to post a reply.