Image color not recognized 2nd time.

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).

Ok, the problem is solved. I don’t know why, but the maps weren’t read by eclipse, so i reinitialized them. Sorry for this topic. This can be closed/deleted. Thanks!

Whenever you change files, make sure to press F5 in Eclipse to refresh. I even had to select in the package explorer the specific folder where was my conflicting file and the press F5 for Eclipse to notice the changes.

Good luck and keep it up! :slight_smile:

One advice here. Go to menu Window -> preferences and then search for “refresh” in the filter.

Then enable:

  • Refresh during startup
  • Automatic refresh

These help a lot!

Okay. Thanks, i am tired to refresh the files after i edit one level image and it doesn’t update. :stuck_out_tongue:

You can turn on automatic refresh in Eclipse. Window -> Preferences -> General -> Click on Workspace. Make sure the first 4 boxes are checked.