Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图将角色添加到我的2D游戏中,但它不起作用?_Java_Javafx - Fatal编程技术网

Java 试图将角色添加到我的2D游戏中,但它不起作用?

Java 试图将角色添加到我的2D游戏中,但它不起作用?,java,javafx,Java,Javafx,我正在做一个蜜蜂游戏,到目前为止一切都很顺利,直到我尝试添加我的蜜蜂角色,在那里我遇到了一个问题。问题是它没有把我的角色加入舞台,所以我看不到我的蜜蜂角色,我不知道为什么?我不确定是什么问题,代码对我来说似乎很好,我问过我的一些朋友,他们也不知道为什么它会坏 我得到这个错误: Caused by: java.lang.NullPointerException at myFirstGame.GameObject.update(GameObject.java:20) at myFir

我正在做一个蜜蜂游戏,到目前为止一切都很顺利,直到我尝试添加我的蜜蜂角色,在那里我遇到了一个问题。问题是它没有把我的角色加入舞台,所以我看不到我的蜜蜂角色,我不知道为什么?我不确定是什么问题,代码对我来说似乎很好,我问过我的一些朋友,他们也不知道为什么它会坏

我得到这个错误:

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)

我把
工厂=新工厂(gc)
gc=canvas.getGraphicsContext2D()下并且它修复了它,谢谢回答它并删除它的人

您正在将一个未初始化的
GraphicsContext
传递给
GameObject
,因此当您使用它时,当然会得到一个NPE。
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;
    }

}
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);
    }


}