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

2012/1/9

Greenfoot.getKey().equals("enter")/Greenfoot.getKey().equals("space")

1
2
davmac davmac

2012/1/12

#
Call getKey() once, and store what it returns in a variable.
Duta Duta

2012/1/14

#
You can get isKeyDown() to only do something once, but you need to have a boolean field and it gets a little bit more complicated - if you want this I can provide some code though
Duta Duta

2012/1/14

#
Actually I'll just provide the code anyway: You have to have a boolean field (I've called it readEnter in this, but you can obviously rename it) - place this in with your fields:
private boolean readEnter = true;
Then you have something like the following in your act() method:
if(!readEnter && !Greenfoot.isKeyDown("enter"))
        readEnter = true;
And then you just do the following if you want to use isKeyDown():
if(Greenfoot.isKeyDown("enter") && readEnter)
{
    //Whatever you want to do.
    readEnter = false;
}
Poli Poli

2013/1/21

#
String key = Greenfoot.getKey(); If(key!=null && key.equals("space")) Thanks it helped a lot
Poli Poli

2013/1/21

#
Builderboy2005 wrote...
Greenfoot.getKey() returns null if no key has been pressed. To fix this, try storing the key into a string beforehand so you can test it:
String key = Greenfoot.getKey();
If(key!=null && key.equals("space"))
thanks
You need to login to post a reply.
1
2