This site requires JavaScript, please enable it in your browser!
Greenfoot back
Gazzzah
Gazzzah wrote ...

2012/4/27

Color detection with getColorAt?

Gazzzah Gazzzah

2012/4/27

#
So, in a basic maze test, I'm trying to make the player class detect walls and stuff by using getColorAt on an image. This is what I've got:
//color the search for
private Color black = new Color(0, 0, 0, 255);
    private void checkWalls()
    {
        world wld = (world) getWorld();
        if (wld.bGround.getColorAt(x + 16, y) == black)
        {
            x--;
            System.out.println("top");
        }
        if (wld.bGround.getColorAt(x - 16, y) == black)
        {
            x++;
            System.out.println("bottom");
        }
        if (wld.bGround.getColorAt(x, y + 16) == black)
        {
            y--;
            System.out.println("right");
        }
        if (wld.bGround.getColorAt(x, y - 16) == black)
        {
            y++;
            System.out.println("left");
        }
    }
It's not working :/ The outputs aren't even working. Anyone know how I can do this?
ttamasu ttamasu

2012/4/27

#
this is a common "gots ya" when you use Java. For instance when you use the boolean expression: wld.bGround.getColorAt(x,y) == black you are saying are the memory location of the value you get when you wld.bGround.getColorAt(x,y) the same as the class instance of black. (Of coursethe answer is no. ) What you want is what is the contents at the memory location equal to that of variable black. wld.bGround.getColorAt(x,y).equals(black) -Takashi
Gazzzah Gazzzah

2012/4/27

#
That worked! Thank you.
You need to login to post a reply.