【发布时间】:2015-01-21 19:17:06
【问题描述】:
我目前正在开发一款游戏,并且现在(在游戏中)是一名关卡编辑器/阅读器。我这样做的方法是将级别存储在 txt 文件中,然后将 txt 文件输入缓冲区读取器,然后将文本文件放入像这样 [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 401级
添加 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 401 级 添加 TYPE_WALL 100 300 400 100 添加 TYPE_WALL 200 300 200 50 添加 TYPE_WALL 400 53 800 10 添加 TYPE_PLAYER 100 200
二级
3级
4级
5级
6级
空
空
空
空
空
空
空
空
空
空
正如我之前所说,问题是当我尝试创建一个等于 getLevel('path') 的二维数组然后打印它时,它会打印“null”!!
这是一些Level类的代码:
包 com.sontvedt.engine;
导入 java.io.IOException; 导入 java.util.ArrayList;
导入 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");
}
}
}
}
一直在寻找答案,但我一无所获......请帮助!
【问题讨论】:
-
永远不要返回全局变量!将行本地化并返回。
-
当您说 getLevel 打印 null 时,您的意思是 system.out.println(getLevel(pathvar)) 打印 null 吗?或者你遍历数组并且每个值都是空的
-
已修复!但没有帮助
-
是的,这不是问题,它只是困扰着我:p 我还在寻找,但文本文件的示例可能有用
-
当我尝试在另一个类中设置一个名为 strLevel 的二维数组等于 TextHandler.getLevel() 然后尝试打印 (strLevel[1][1]) 我在底部得到结果只是说了几次 null
标签: java printing null multidimensional-array