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) :
1
| System.out.println(stringWorld[0][0]); |
the program print :
o now if i do this :
1 2 3 4 5
| 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| 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;
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(); }
public void setWorld() { System.out.println(stringWorld[0][0]);
if (stringWorld[0][0] == "o") { System.out.println("yes"); } else { System.out.println("no"); }
}
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();
} 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
public class Load { private String[][] testWorld = new String[Board.height][Board.width];
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()); } }
public String[][] getWorld() {
return testWorld;
} 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 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
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 { private Drawing draw = new Drawing(); private Loop loop = new Loop();
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;
public void init() {
loop.init(); initHero(); hero = new Player(heroX, heroY, heroW, heroH); onGround = false; canJump = false; world = new World(width, height);
world.setWorld();
}
public void myBoardLoop() { delta = loop.getDelta(); update(delta); }
private void update(int delta) { painting(); heroUpdate(); loop.updateFPS(); }
private void painting() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); draw.coloring("white"); draw.fillRect(hero.getX(), hero.getY(), hero.getWidth(), hero.getHeight());
}
private void initHero() { heroX = 3 * 32; heroY = Game.HEIGHT - (2 * 32); heroW = 32; heroH = 32; heroSpeed = 0.5; }
private void heroUpdate() { control(delta); hero.setX(heroX); hero.setY(heroY); hero.setWidth(heroW); hero.setHeight(heroH);
}
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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