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
TopInPut TopInPut

2012/1/9

#
Why the "java lang nullpointerexception" come, if every Player of my 2 Players have one getKey() method? And why ("space".equals(Greenfoot.getKey()))/("enter".equals(Greenfoot.getKey())) are not still working?
TopInPut TopInPut

2012/1/9

#
(All codes are in an if-method!)
Builderboy2005 Builderboy2005

2012/1/9

#
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"))
davmac davmac

2012/1/10

#
And, if you call it more than once, only the first time will return the key that was pressed. If you have multiple players each checking for keystrokes, it's usually easier to use isKeyDown(...) instead of getKey(). Or otherwise, have the world call getKey() and then control the appropriate player depending on which key was pressed.
TopInPut TopInPut

2012/1/10

#
But the isKeyDown() can be hold down and the methods go on. I just want 1 method per Press.;)
TopInPut TopInPut

2012/1/10

#
@Builderboy : Nice solution. ;)
davmac davmac

2012/1/10

#
But the isKeyDown() can be hold down and the methods go on. I just want 1 method per Press.;)
This:
Or otherwise, have the world call getKey() and then control the appropriate player depending on which key was pressed.
TopInPut TopInPut

2012/1/10

#
Oh i hate it. -.- The nice solution doesn't works. :S Is there another way like getKey() Or isKeyDown() so changeable, that it just do the method one time per press? ^^
Builderboy2005 Builderboy2005

2012/1/10

#
Try what davmac suggested, and have 1 call to getKey() in your main world where it stores the information for all classes to reference. There is also a way to get isKeyDown() to work the way you want but it requires more variables and a bit of trickiness.
TopInPut TopInPut

2012/1/10

#
It's possible to use getButtom() ?
Builderboy2005 Builderboy2005

2012/1/10

#
The only getButton() method that Greenfoot provides is for the mouse, is that what you mean?
TopInPut TopInPut

2012/1/11

#
No.xD Is there another way for getKey() ?
TopInPut TopInPut

2012/1/11

#
Or something like getKey :D
Builderboy2005 Builderboy2005

2012/1/11

#
What do you mean by 'is there another way for getKey()'?
TopInPut TopInPut

2012/1/12

#
I mean 2 times getKey() doesn't work. IsKeyDown() makes too much. So is there another possiblelity? ^^
There are more replies on the next page.
1
2