I want to use the getKey() method so that I only get one action (ball is shot) when the space bar is clicked. The following code works:
but why does this code give me a null pointer exception?
(notice the order of the IF statement.)
String myKey = Greenfoot.getKey(); if ("space".equals(myKey)){ //add the ball Ball ball = new Ball(); getWorld().addObject(ball, getWorld().getWidth() / 2, getWorld().getHeight() - 50); ball.setRotation(getRotation()-90); }
String myKey = Greenfoot.getKey(); if (myKey.equals("space")){ //add the ball Ball ball = new Ball(); getWorld().addObject(ball, getWorld().getWidth() / 2, getWorld().getHeight() - 50); ball.setRotation(getRotation()-90); }