Hello there. I continue to code my game and, after 2 hours of debugging i still get a problem:
I am loading the game map from an .png image. When i run the game on first map it’s ok, the code read the color, but when i change the map, the code doesn’t recognize the same color.
Here is my code (based on Catacomb Snatch) ((the problem is at System.out.println(“spawn?”), in that if)) :
public void loadLevel(Level l) {
currentLevel = l;
BufferedImage bufferedImage = l.levelImage;
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
Level.levelWidth = w;
Level.levelHeight = h;
Level.tiles = new Tile[h*w];
Level.unpassableTiles = new Bag();
Level.passableTiles = new Bag();
Level.enemy = new Bag();
Level.spawners = new Bag();
Level.others = new Bag();
int[] rgbs = new int[w * h];
Arrays.fill(rgbs, 0xffA8A800);
bufferedImage.getRGB(0, 0, w, h, rgbs, 0, w);
l.player = new Player(256, 256, 0);
int counter = 0;
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
int col = rgbs[x + y * w] & 0xffffff;
Tile tile = null;
if(col == 0xff00ff) {
System.out.println("spawn?");
tile = new FloorTile(x*FloorTile.getWidth(), y*FloorTile.getHeight());
l.player.setX((float) (x*FloorTile.getWidth() + FloorTile.getWidth()/2 - l.player.getWidth()/2));
l.player.setY(y*FloorTile.getHeight());
l.player.setLastX(l.player.getX());
l.player.setLastY(l.player.getY());
} else if(col == 0xffffff) {
tile = new FloorTile(x*FloorTile.getWidth(), y*FloorTile.getHeight());
} else if(col == 0xff0000) {
tile = new WallTile(x*WallTile.getWidth(), y*WallTile.getHeight());
} else if(col == 0x000000) {
tile = new HoleTile(x*HoleTile.getWidth(), y*HoleTile.getHeight());
} else if(col == 0x999999) {
tile = new FloorTile(x*FloorTile.getWidth(), y*FloorTile.getHeight());
Level.spawners.add(new Spawner(x*32, y*32));
}
Level.tiles[counter] = tile;
counter++;
if(!tile.isPassable())
Level.unpassableTiles.add(tile);
else if(tile.isPassable())
Level.passableTiles.add(tile);
}
}
}
EDIT: Hmm, i did some tests and i realised that the color is recognized only in first level(map).