Hello,
i am trying to create a tiled map based on a file content, and till now things are working “fine” till i got this strange problem,
the thing is that i have a multi-Dimensional array, which i store in it all the content of the file (just to make it easier when i will draw the world)
now the problem is here :
if i do this (please follow me here) :
System.out.println(stringWorld[0][0]);
the program print : o
now if i do this :
if (stringWorld[0][0] == "o") {
System.out.println("yes");
} else {
System.out.println("no");
}
the program print : no
here is the classes that you might need :
this is the “world creator” class (the one that contain the error)
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class World {
private int width, height;
private Block block;
private String[][] stringWorld;
private File file;
private Load load;
//constructor
public World(int width, int height) {
this.width = width;
this.height = height;
load = new Load();
file = new File("res/beta.txt");
load.loadLevel(file);
stringWorld = load.getWorld();
}
//checking the grid case
public void setWorld() {
System.out.println(stringWorld[0][0]);
if (stringWorld[0][0] == "o") {
System.out.println("yes");
} else {
System.out.println("no");
}
}
// doesn't matter now
public void drawInWorld(int x, int y, int type) {
Block block = new Block(0, 0, 32, type);
block.setX(x * block.getSize());
block.setY(y * block.getSize());
block.draw();
}
//in case i want to check
public void printTheWorld() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
System.out.print(stringWorld[y][x]);
}
System.out.println("");
}
}
}
this is the " file loader" class
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Load {
// the array that will hold information from the file
private String[][] testWorld = new String[Board.height][Board.width];
// reading from the file and writing into the array
public void loadLevel(File path) {
try {
Scanner loadScanner = new Scanner(path);
while (loadScanner.hasNext()) {
for (int y = 0; y < Board.height; y++) {
for (int x = 0; x < Board.width; x++) {
testWorld[y][x] = loadScanner.next();
}
}
}
} catch (FileNotFoundException e) {
System.err.println("Error: " + e.getMessage());
}
}
// getting the array
public String[][] getWorld() {
return testWorld;
}
//in case i want to check
public void printing() {
for (int y = 0; y < Board.height; y++) {
for (int x = 0; x < Board.width; x++) {
System.out.print(testWorld[y][x]);
}
System.out.println("");
}
}
}
this is the board class (normally you’re not gonna need it )
/**
* This class will handle the game logic
*
*/
import java.util.Arrays;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class Board {
// varibable of other classes
private Drawing draw = new Drawing(); // drawing class
private Loop loop = new Loop(); // the loop class
private double heroX, heroY, heroW, heroH, heroSpeed;
private Player hero;
private Block block;
private World world;
private GameTools tool = new GameTools();
private boolean onGround, canJump;
int delta;
static int width = 26, height = 22;
static String[][] myWorld;
// initializing
public void init() {
loop.init();
initHero();
hero = new Player(heroX, heroY, heroW, heroH);
onGround = false;
canJump = false;
world = new World(width, height);
world.setWorld();
}
// the main game logic "caller"
public void myBoardLoop() {
delta = loop.getDelta();
update(delta);
}
// the update method
private void update(int delta) {
painting();
heroUpdate();
loop.updateFPS();
}
private void painting() {
// clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
//draw the player
draw.coloring("white");
draw.fillRect(hero.getX(), hero.getY(), hero.getWidth(),
hero.getHeight());
}
// initializing the player
private void initHero() {
heroX = 3 * 32;
heroY = Game.HEIGHT - (2 * 32);
heroW = 32;
heroH = 32;
heroSpeed = 0.5;
}
// updateing the player
private void heroUpdate() {
control(delta);
hero.setX(heroX);
hero.setY(heroY);
hero.setWidth(heroW);
hero.setHeight(heroH);
// heroCollision();
}
// controlling the player
private void control(int delta) {
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
heroX -= heroSpeed * delta;
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
heroX += heroSpeed * delta;
}
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
heroY += heroSpeed * delta;
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
heroY -= heroSpeed * delta;
}
}
private void heroCollision() {
if (!onGround) {
heroY -= 0.9 * delta;
}
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
int tmpBlockX = x;
int tmpBlockY = y;
int blockGridX = tmpBlockX * 32;
int blockGridY = tmpBlockY * 32;
String block = myWorld[y][x];
if (tool.aabb(hero.getX(), hero.getY(), hero.getWidth(),
hero.getHeight(), blockGridX, blockGridY, 32, 32)) {
if (block == "o") {
System.out.println("touch");
}
if (block == "g") {
onGround = true;
canJump = true;
} else {
onGround = false;
}
}
}
}
}
}
and finaly this is the file :
o A * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * o * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
g g g g g g g g g g g g g g g g g g g g g g g g g g
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
thank you very much