将文本文件加载到扫描仪中的LibGDX问题

时间:2019-05-09 13:36:54

标签: java libgdx

JAVA程序

我最近开始使用LibGDX制作“ Pacman”的简单克隆。但是,我在将文本文件加载到扫描仪时遇到问题。当我在NetBeans中运行我的游戏时,一切都很好。但是问题出在我尝试将其作为.jar文件运行时。

我曾尝试使用BufferedReader,FileReader,但没有一个解决了问题...

File subor = Gdx.files.internal("HraciaPlocha/gamearea.txt").file();

Scanner sc = new Scanner(subor);

this.width = sc.nextInt() + 1; // Custom width
this.height = sc.nextInt() + 1; //Custom height

sc.nextLine();

    this.arena = new Block[width][height];

    for (int rows = 0; rows < this.arena.length - 1; rows++) {

        for (int columns = 0; columns < this.arena[rows].length - 1; columns++) {
            switch (sc.nextInt()) {
                case 1:
                    this.arena[rows][columns] = new Bariera(lavaXSur + 32 * columns, hornaYSur + 32 * rows);
                    break;
                case 2:
                    this.arena[rows][columns] = new KlasickaBodka(lavaXSur + 32 * columns, hornaYSur + 32 * riadky);
                    this.pridajBodku();
                    break;
                case 3:
                    this.arena[rows]columns] = new Zmurzuvac(lavaXSur + 32 * columns, hornaYSur + 32 * rows);
                    break;
                default:
                    this.arena[rows][columns] = null;
                    break;
            }

        }

    }

    sc.close();

    this.spustena = true;
}

我希望从.jar文件运行时游戏能够运行,但它会给我这个错误:

mßj 09, 2019 3:31:43 PM com.rufo123.pacman.Hra create
SEVERE: null
java.io.FileNotFoundException: HraciaPlocha\gamearea.txt (System cannot find specified path)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.util.Scanner.<init>(Unknown Source)
        at com.rufo123.pacman.GameArea.nacitajArenu(GameArea.java:54)
        at com.rufo123.pacman.Hra.create(Hra.java:59)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplic
ation.java:149)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplicati
on.java:126)

Exception in thread "LWJGL Application" java.lang.NullPointerException
        at com.rufo123.pacman.GameArea.renderArena(GameArea.java:93)
        at com.rufo123.pacman.Hra.render(Hra.java:97)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplic
ation.java:225)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplicati
on.java:126)

如果您能帮助我解决这个问题,我将非常高兴。

PS。如果不清楚,请提出要求。 :)

1 个答案:

答案 0 :(得分:0)

我尝试过与您相同的操作,并且出现相同的错误。

我不确定为什么会出现错误,但是我解决了这样的问题:

FileHandle f = Gdx.files.internal("test/test.txt");

Scanner scanner = new Scanner(f.read());

while(scanner.hasNext()){
    System.out.println(scanner.nextLine());
}