我目前正在开发一款游戏,现在(在游戏中)是一名关卡编辑/读者。我这样做的方法是将级别存储在txt文件中,然后将txt文件输入缓冲区读取器,然后将文本文件放入2d数组,如[lines] [words]。我的意思是我希望第一个方括号中的数字表示你想要的行(来自文件),然后用另一个方括号从该行中选择一个单词。
我打印文件并将其存储在一个类中的二维数组中,但当我尝试将信息传输到另一个类时,如下所示:String strLevel = TextHandler.getLevel("res/levels.txt")
然后,当我尝试打印strLevel [1] [1]时,它只是在控制台中打印出null(s)...
以下是TextHandler.java的代码:
公共类TextHandler {
final static int LEVELS = Level.LEVEL_AMOUNT;
final static int MAX_LINES = 100;
final static int MAX_WORDS = 100;
public static String[][] getLevel(String path) throws IOException {
int x = 0;
int y = 0;
String lines[][] = new String[MAX_LINES][MAX_WORDS];
BufferedReader in = new BufferedReader(new FileReader(path));
String line;
while ((line = in.readLine()) != null) //file reading
{
String[] values = line.split(" ");
for (String str : values){
lines[x][y] = str;
System.out.print(lines[x][y] + " "); //This print funciton works!
y += 1;
}
x += 1;
System.out.println("");
}
return lines;
}
}
这是文本文件:
等级0
添加TYPE_WALL 100 300 400 100
添加TYPE_WALL 200 300 200 50
添加TYPE_WALL 400 53 800 10
添加TYPE_PLAYER 200 40等级1 添加TYPE_WALL 100 300 400 100
添加TYPE_WALL 200 300 200 50
添加TYPE_WALL 400 53 800 10
添加TYPE_PLAYER 100 2002级 //空水平
等级3 //空水平
等级4 //空水平
等级5 //空水平
等级6 //空水平
这是结果:
等级0
添加TYPE_WALL 100 300 400 100
添加TYPE_WALL 200 300 200 50
添加TYPE_WALL 400 53 800 10
添加TYPE_PLAYER 200 40等级1 添加TYPE_WALL 100 300 400 100 添加TYPE_WALL 200 300 200 50 添加TYPE_WALL 400 53 800 10 添加TYPE_PLAYER 100 200
等级2
等级3
等级4
等级5
等级6 空
空
空
空
空
空
空
空
空
null
正如我之前所说,问题是当我尝试制作一个等于getLevel的二维数组('路径')然后打印它时,它会打印" null"! !
这是一些Level类代码:
package com.sontvedt.engine;
import java.io.IOException; import java.util.ArrayList;
import com.sontvedt.entity。*;
公共班级{
Utilities util = new Utilities();
TextHandler txt = new TextHandler();
static final int LEVEL_AMOUNT = 10;
String strLevel[][];
Player player;
Wall wall = new Wall(42,42,42,42);
ArrayList<Entity> objects = new ArrayList<Entity>();
Enemy[] enemies = new Enemy[20]; //This it later going to be added in the entities arrayList. The reason its a normal array is so that it would work with collision detection
//Pre-init of the levelAmount should be done here
public Level(){
player = new Player(42, 42);
try {
strLevel = TextHandler.getLevel("res/levels.txt");
} catch (IOException e) {
e.printStackTrace();
}
//Init entities ArrayList
createLevels(0);
}
//Collision check
public void update(){
}
/* LEVEL CREATION
*
* The first thing that is going to be in the "level editor" text file
* is which level the items are going to be added to. Like so:
* - level 0
*
* The entity adding is later going to be added in a text file as so:
* - Group entity xPos yPos Width Height
* - enemies enemy 60 60, 200, 100
*
* The player postition is the first two numbers in txt file,
* Player is also the last item that should be added to entities
*
* This may have to be in the end of the superloop
*
* for(int i = 0; i < enemies.length; i++)
* entities[level].add(enemies[i]);
*
*/
public void createLevels(int level){
objects.add(player);
objects.add(wall);
//Should print add from text file/TextHandler.getLevel()
System.out.println(strLevel[1][1]);
for(int i = 0; i >= TextHandler.MAX_LINES; i++){
for(int j = 0; j >= TextHandler.MAX_WORDS; j++){
if(strLevel[j][j] == "level"){
System.out.println("I found the word 'level', I'm so cool, look at meee... :P");
}
}
}
}
很长一段时间以来一直在寻找答案,但我一无所获......请尽快帮助!
答案 0 :(得分:1)
到达新行时,您不会重置y值:
while ((line = in.readLine()) != null) //file reading
{
String[] values = line.split(" ");
y=0; // gotta start back at 0 when we get to a new line
其他内容
//Should print add from text file/TextHandler.getLevel()
System.out.println(strLevel[1][1]);
评论不正确,应打印TYPE_WALL。
if(strLevel[j][j] == "level"){
使用.equals
而非==
比较字符串,以进行空安全比较:
if("level".equals(strLevel[j][j]))
if(strLevel[j][j] == "level"){
我非常确定你的意思是strLevel[i][j]
,也因为你在其他地方使用x
和y
,你应该尽量保持一致。也许将i
重命名为x
,将j
重命名为y
。