我正在尝试使其能够使用EventHandlers来移动角色,但是在这一部分中遇到了此空指针错误,并且我不知道如何解决。如果有人对如何解决有任何想法,将不胜感激。我收到错误
由以下原因引起:myFirstGame.honeyBee.start(honeyBee.java:69)上的java.lang.NullPointerException
这是这行代码:scene.addEventHandler(KeyEvent.KEY_PRESSED, (key) -> {
是因为scene.add吗?我不太确定从哪里开始
public class honeyBee extends Application {
GameObject gameObject;
GraphicsContext gc;
Stage stage;
Canvas canvas;
Image img1;
Factory Factory;
Scene scene;
private BeeCharacter bee;
ArrayList<GameObject>characterList = new ArrayList<GameObject>();
ArrayList<GameObject>Hive = new ArrayList<GameObject>();
ArrayList<GameObject>flowers = new ArrayList<GameObject>();
Random rnd = new Random(System.currentTimeMillis());
int count = 0;
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
// TODO Auto-generated method stub
gc.drawImage(img1, 0, 0, canvas.getWidth(), canvas.getHeight());
for(GameObject obj : characterList) {
obj.update();
}
for(GameObject obj : Hive) {
obj.update();
}
if(count++>40) {
flowers.add(new SpawnFlowers(gc, rnd.nextInt(600), rnd.nextInt(550)));
count = 2;
}
for(GameObject obj : flowers)
((SpawnFlowers)obj).grow();
}
};
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
public void start(Stage stage) throws Exception {
stage.setTitle("HoneyBee");
Pane root = new Pane();
//scene = new Scene(root, canvas.getWidth(), canvas.getHeight());
scene.addEventHandler(KeyEvent.KEY_PRESSED, (key) -> {
if(key.getCode()==KeyCode.UP) {
bee.setSpeedY(5);
bee.setDirectionY(-1);
} else if(key.getCode()==KeyCode.LEFT) {
bee.setSpeedX(5);
bee.setDirectionX(-1);
} else if(key.getCode()==KeyCode.DOWN) {
bee.setSpeedY(5);
bee.setDirectionY(1);
} else if(key.getCode()==KeyCode.RIGHT) {
bee.setSpeedX(5);
bee.setDirectionX(1);
}
});
scene.addEventHandler(KeyEvent.KEY_RELEASED, (key) -> {
if(key.getCode()==KeyCode.UP) {
bee.setSpeedY(0);
bee.setDirectionY(0);
} else if(key.getCode()==KeyCode.LEFT) {
bee.setSpeedX(0);
bee.setDirectionX(0);
} else if(key.getCode()==KeyCode.DOWN) {
bee.setSpeedY(0);
bee.setDirectionY(0);
} else if(key.getCode()==KeyCode.RIGHT) {
bee.setSpeedX(0);
bee.setDirectionX(0);
}
});
stage.setScene(new Scene(root, 780, 580));
stage.setResizable(false);
stage.show();
canvas = new Canvas(800,600);
gc = canvas.getGraphicsContext2D();
Factory = new Factory(gc);
img1 =new Image("res/1.png");
gc.drawImage(img1, 0, 0, canvas.getWidth(), canvas.getHeight());
root.getChildren().add(canvas);
bee = (BeeCharacter)Factory.createCharacter("bee", 400, 300, 0, 0);
Hive.add(Factory.createCharacter("Hive", 700, 300, 0, 0));
timer.start();
}
}
我的蜜蜂班
class BeeCharacter extends GameObject{
protected double speedX, speedY=0;
protected double dx, dy=0;
protected int score=0;
public BeeCharacter(GraphicsContext gc, double x, double y) {
super(gc, x, y);
img = new Image("res/bee.png");
update();
}
public void update() {
if(x+speedX*dx>gc.getCanvas().getWidth() || x+speedX*dx < 0)
{
setDirectionX(0);
} else {
x+=speedX*dx;
}
if(y+speedY*dy>gc.getCanvas().getHeight() || y+speedY*dy < 0)
{
setDirectionY(0);
} else {
y+=speedY*dy;
}
super.update();
}
public void setSpeedY(double speedY) {
this.speedY = speedY;
}
public void setSpeedX(double speedX) {
this.speedX = speedX;
}
public void setDirectionX(int dir) {
dx = dir;
}
public void setDirectionY(int dir) {
dy = dir;
}
public void setLocation(int x, int y)
{
this.x = x;
this.y = y;
}
}