1

我正在处理 3.x 中编写游戏,我正在尝试使用 SelectInput(); 一种加载不同游戏保存文件的方法。但是,我不断收到无法修复的 NullPointer 异常。我一直尝试在 loadGame 函数之前运行 SelectInput 函数,但程序还是崩溃了。目前,在下面的代码中,程序在读取为 'columns=splitTokens(rows[1]);' 的行处出错

任何帮助使此代码再次运行将不胜感激。

编码:

PImage tile1;
PImage tile2;
PImage tile3;
PImage tile4;
PImage tile5;
PImage tile6;
PImage tile7;
PImage tile8;

String input;

String rows[];
String columns[];
int array2D[][];

boolean gameLoaded = false;

void setup() {
  size(600, 600);
  background(0);
  frameRate(30);

  tile1 = loadImage("grass0.png");
  tile2 = loadImage("grass1.png");
  tile3 = loadImage("grass2.png");
  tile4 = loadImage("tile1.png");
  tile5 = loadImage("tile2.png");
  tile6 = loadImage("tree.png");
  tile7 = loadImage("tree2.png");
  tile8 = loadImage("tile2.png");

  selectInput("Select a file to process:", "fileSelected");

  if (gameLoaded == false) {
    //selectInput("Select a file to process:", "fileSelected");
    loadGame();
    gameLoaded = true;
  }
}


void draw() {
  if (gameLoaded == true) {
    for (int a = 0; a < rows.length; a++) {
      for (int b = 0; b < columns.length; b++) {
        if (array2D[a][b] == 1) {
          image(tile1, a * 100, b * 100);
        }
        if (array2D[a][b] == 2) {
          image(tile2, a * 100, b * 100);
        }
        if (array2D[a][b] == 3) {
          image(tile3, a * 100, b * 100);
        }
        if (array2D[a][b] == 4) {
          image(tile4, a * 100, b * 100);
        }
        if (array2D[a][b] == 5) {
          image(tile5, a * 100, b * 100);
        }
        if (array2D[a][b] == 6) {
          image(tile6, a * 100, b * 100);
        }
        if (array2D[a][b] == 7) {
          image(tile7, a * 100, b * 100);
        }
        if (array2D[a][b] == 8) {
          image(tile8, a * 100, b * 100);
        }
      }
    }
  }
}


void loadGame() {
  //rows=loadStrings("file.txt");
  rows = loadStrings(input);

  columns = splitTokens(rows[1]);

  array2D = new int[rows.length][columns.length];

  println("There are " + rows.length + " rows");
  println("There are " + columns.length + " columns");


  for (int a = 0; a < rows.length; a++) {
    columns = splitTokens(rows[a]);

    for (int b = 0; b < columns.length; b++) {
      array2D[a][b] = Integer.parseInt(columns[b]);
    }
  }
}

void fileSelelected(File selection) {
  if (selection == null) {
    println("Nothing was selected, so nothing happens");
  } else {
    input = selection.getAbsolutePath();
    //rows=loadStrings(input);
  }
}

4

1 回答 1

0

它很容易阅读,但文档建议文件选择器在单独的线程上运行。这意味着您的程序不会等到选择文件并且 selectInput 函数完成。相反,它会继续处理您的代码,直到选择一个文件,然后中断正常处理以处理 fileSelected 函数。

在您的代码中,这意味着loadGame()它将始终在您有机会选择文件之前运行。解决方案是创建一个变量来检查文件是否已被选中。

在代码的顶部创建变量:

boolean isFileSelected = false;

加载游戏前,检查文件是否被选中:

 if (gameLoaded == false && isFileSelected == true) {
    loadGame();
    gameLoaded = true;
  }

选择文件后,更改变量:

void fileSelelected(File selection) {
  if (selection == null) {
    println("Nothing was selected, so nothing happens");
  } else {
    input = selection.getAbsolutePath();
    isFileSelected = true;
  }
}

如果还不完全清楚,下面的脚本可以帮助理解这个概念。一旦程序启动,它将继续打印start,直到您选择了一个文件。

String test = "start";

void setup() {
  selectInput("Select a file to process:", "fileSelected");
}

void draw(){
   println(test);
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    test = "test";
  }
}
于 2019-03-19T08:12:17.877 回答