I'm making a game and in the game I load the map using an image. I check each pixel RGB and depends on that I add objects. This is my code:
private void loadLevel() {
Bitmap image = Assets.loadLevel(LEVEL);
int w = image.getWidth();
int h = image.getHeight();
for (int xx = 0; xx < h; xx++) {
for (int yy = 0; yy < w; yy++) {
int pixel = image.getPixel(xx, yy);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
if (red == 100 && green == 60 && blue == 0) { // / block
Log.d("PlayState","Creating blockss!!!");
addObject(new Block(xx * 32, yy * 32, 0, ObjectId.Block));
}
if (red == 100 && green == 50 && blue == 0) { // /block 1
addObject(new Block(xx * 32, yy * 32, 1, ObjectId.Block));
}
if (red == 100 && green == 40 && blue == 0) { // /block 2
addObject(new Block(xx * 32, yy * 32, 2, ObjectId.Block));
}
if (red == 100 && green == 30 && blue == 0) { // /block 3
addObject(new Block(xx * 32, yy * 32, 3, ObjectId.Block));
}
if (red == 255 && green == 255 && blue == 255) { // /block 4
addObject(new Block(xx * 32, yy * 32, 4, ObjectId.Block));
}
if (red == 255 && green == 255 && blue == 245) { // /block 5
addObject(new Block(xx * 32, yy * 32, 7, ObjectId.Block));
}
if (red == 0 && green == 255 && blue == 33) { // /block up
addObject(new Block(xx * 32, yy * 32, 5, ObjectId.Block));
}
if (red == 0 && green == 245 && blue == 33) { // /block up1
addObject(new Block(xx * 32, yy * 32, 6, ObjectId.Block));
}
if (red == 0 && green == 0 && blue == 255) { // / player
Log.d("PlayState","Creating Playerrr!!!");
addObject(new Player(xx * 32, yy * 32, ObjectId.Player));
}
}
}
}
The problem is that for some reason it only adds the Player object,it might be becuase it is the only object that both red & green are 0. When I used exactly the same code to program to windows it worked great.
Btw, I have also tried
int red = Color.red(pixel);
int green = Color.green(pixel);
int blue = Color.blue(pixel);
same result...
EDIT:
If someone else has this problem thanks to @JASONGPETERSON I found the solution. Instead of looking for the exact value check if it a bit lower or higher, for example
if ((red > 90 && red < 110) && (green > 50 && green < 70) && blue == 0) { // / block
Log.d("PlayState","Creating blockss!!!");
addObject(new Block(xx * 32, yy * 32, 0, ObjectId.Block));
}