0

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

            }
Gilmor
  • 53
  • 7
  • Please, look at this http://stackoverflow.com/questions/5669501/how-to-get-rgb-values-of-bitmap-in-android – JpCrow Dec 17 '14 at 15:38
  • Perhaps your conditions need to test for a narrow range instead of an exact integer. There may be some rounding due to the `getPixel()` method, image compression, etc. – JASON G PETERSON Dec 17 '14 at 15:40
  • @JpCrow I had a look there but it didnt help my problem.. – Gilmor Dec 17 '14 at 16:02
  • @JASONGPETERSON yey you there right!!! when I checked if the the color is lower or higher by 10 it worked.Thanks alot :)) – Gilmor Dec 17 '14 at 16:09

3 Answers3

0

Did you try to Log.d the red, green and blue values to see whether it's really the situation as observed?

Patrick Chan
  • 1,019
  • 10
  • 14
0

Maybe not fixing your problem but :

int pixel = image.getPixel(xx, yy);

suggest you're using "xx" as "x coordinate" (width) but you're itering on the height of your image with :

int h = image.getHeight();
for (int xx = 0; xx < h; xx++)
Nino DELCEY
  • 652
  • 1
  • 9
  • 25
  • I didnt notice that, but that doesnt matter becuase in the end it will check all the pixels so it will give the same result. – Gilmor Dec 17 '14 at 16:00
0

Perhaps your conditions need to test for a narrow range instead of an exact integer. There may be some rounding due to the getPixel() method, image compression, etc. (And, in fact, this turned out to work.)

JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19