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

2013/12/3

Is there any way to detect a key RELEASE?

Duckyroannubbles Duckyroannubbles

2013/12/3

#
I've got functioning menu layers for my game so that when you have a button that brings you into another menu, that works. My problem is, I want to have it so that when the player presses the escape key, it will be a quick way to go one layer of menus up, so they don't have to actually reach over and click a button to do so. If I just used Greenfoot.isKeyDown(), though, I'd end up with people accidentally going back to the very highest layer whenever they tapped escape. Should I just resign myself to a cooldown timer or is there a detect release method?
danpost danpost

2013/12/3

#
All you need is an instance boolean value to track the state of the key:
// instance boolean
private boolean escapeIsDown;
// checks to perform
if (!escapeIsDown && Greenfoot.isKeyDown("escape"))
{
    // do whatever when escape key is down
    escapeIsDown = true;
}
if (escapeIsDown && !Greenfoot.isKeyDown("escape"))
{
    // do whatever when escape key is released
    escapeIsDown = false;
}
An alternate way is to use the 'getKey' method (instead of 'isKeyDown':
String key = Greenfoot.getKey();
if ("escape".equals(key))
{
    // do whatever when escape key is released
}
Duckyroannubbles Duckyroannubbles

2013/12/3

#
Thanks!
You need to login to post a reply.