我正在制作一个蜜蜂游戏,到目前为止一切都进行得很顺利,直到我试图在遇到问题的地方添加我的蜜蜂角色为止。问题是它没有将我的角色添加到舞台上,所以我看不到我的蜜蜂角色,我也不知道为什么?我不确定代码的问题对我来说是否合适,我问了一些朋友,他们也为什么不打断?
我收到此错误:
Caused by: java.lang.NullPointerException
at myFirstGame.GameObject.update(GameObject.java:20)
at myFirstGame.BeeCharacter.update(honeyBee.java:88)
at myFirstGame.BeeCharacter.<init>(honeyBee.java:79)
at myFirstGame.Factory.createCharacter(honeyBee.java:109)
at myFirstGame.honeyBee.start(honeyBee.java:65)
我的蜜蜂类
package myFirstGame;
import java.util.ArrayList;
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class honeyBee extends Application {
GameObject gameObject;
GraphicsContext gc;
Stage stage;
Canvas canvas;
Image img;
Factory Factory;
ArrayList<GameObject>characterList = 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(img, 0, 0, canvas.getWidth(), canvas.getHeight());
for(GameObject obj : characterList) {
obj.update();
}
}
};
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();
Factory = new Factory(gc);
stage.setScene(new Scene(root, 780, 580));
stage.setResizable(false);
stage.show();
canvas = new Canvas(800,600);
gc = canvas.getGraphicsContext2D();
img =new Image("res/1.png");
gc.drawImage(img, 0, 0, canvas.getWidth(), canvas.getHeight());
root.getChildren().add(canvas);
//gameObject.timer.start();
characterList.add(Factory.createCharacter("bee", 30, 30));
timer.start();
}
}
class BeeCharacter extends GameObject{
double dx=1;
public BeeCharacter(GraphicsContext gc, double x, double y) {
super(gc, x, y);
img = new Image("res/bee.png");
update();
}
public void update() {
x+=dx;
if(x>800 || x<0) {
dx=-dx;
y+=20;
}
super.update();
}
}
interface FactoryIF {
GameObject createCharacter(String start, double x, double y);
}
class Factory implements FactoryIF {
GraphicsContext gc;
public Factory(GraphicsContext gc) {
super();
this.gc = gc;
}
@Override
public GameObject createCharacter(String start, double x, double y) {
// TODO Auto-generated method stub
if(start.equals("bee"))
return new BeeCharacter(gc, x, y);
else
return null;
}
}
我的GameObject类
package myFirstGame;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
class GameObject {
protected Image img;
protected double x, y;
protected GraphicsContext gc;
public GameObject(GraphicsContext gc, double x, double y){
this.gc=gc;
this.x=x;
this.y=y;
}
public void update(){
if(img!=null)
gc.drawImage(img, x, y, 30, 30);
}
}
答案 0 :(得分:0)
我将Factory = new Factory(gc);
放在gc = canvas.getGraphicsContext2D();
下,并修正了它,谢谢回答的人,然后将其删除!