I am currently working on a game and right now (in the game) a level-editor/reader. The way I am doing this is by storing the levels in a txt file and then feeding the txt file into a bufferreader and then putting the text file into a 2d array like this [lines][words]. By this I mean that I want the number in the first square bracket to represent what line you want (from the file), and then choosing a word from that line with the other square bracket.
I get to print the file and store it in a 2d array in one class, but when i try to transfer the information to another class like so: String strLevel = TextHandler.getLevel("res/levels.txt")
And when i then try to print strLevel[1][1] it just print out null(s) in the console...
Here is the code for TextHandler.java:
public class 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;
}
}
This is the text file:
level 0
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 200 40level 1
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 100 200level 2
//Empty levellevel 3
//Empty levellevel 4
//Empty levellevel 5
//Empty levellevel 6
//Empty level
This is the outcome:
level 0
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 200 40level 1 add TYPE_WALL 100 300 400 100 add TYPE_WALL 200 300 200 50 add TYPE_WALL 400 53 800 10 add TYPE_PLAYER 100 200
level 2
level 3
level 4
level 5
level 6
null
null
null
null
null
null
null
null
null
null
As i said earlier, the problem is when i try to make a 2d array that is equals to getLevel('path') and then print it, it prints "null"!!
Here is some Level class code:
package com.sontvedt.engine;
import java.io.IOException; import java.util.ArrayList;
import com.sontvedt.entity.*;
public class Level {
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");
}
}
}
}
Have been searching for the answer for quite some time but i found nothing... Pleace Help!