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

2011/3/7

Keyboard inputs don't work?

Maxident Maxident

2011/3/7

#
i've made most of the logic for my 'block based game' upon my Background class, which is a subclass of world. I tryed this code out: if(Greenfoot.mouseClicked(this)) { CurrentShape.moveRight(); } which worked fine, but obviously using the mouse to move right isn't ideal so i tried loads using the keyboard, particularly the one which is actually given in the programmer manual (http://www.greenfoot.org/doc/manual.html#keyboard) if(Greenfoot.getKey().equals("space") { some code here } but it just keeps giving me a NullPointerException pointing to the getKey line of code. help?
smyneni smyneni

2011/3/8

#
Is that a typo error here? a parenthesis at the end of the if statement is missing.............. or you can try with the - isKeyDown("space") method
davmac davmac

2011/3/8

#
Greenfoot.getKey() can return null (check the documentation), and if so then you're trying to call the "equals" method on a null reference, which you can't do. You could add a check for this case specifically, though a neat trick is just to reverse the expression: if ("space".equals(Greenfoot.getkey())) { .... This works because equals can take a null reference as an argument, even though it can't be called via a null reference.
m.legere1323 m.legere1323

2011/3/8

#
I see a missing parenthesis.... try adding ")" after "if(Greenfoot.getKey().equals("space") "
Maxident Maxident

2011/3/8

#
thanks for the tip davmac, very helpful :D, and i left the extra bracket out in my post but it was in my code but thanks anyway
m.legere1323 m.legere1323

2011/3/8

#
aww =(
You need to login to post a reply.