Search code examples
java2dcollision-detectionslick2d

testing for the color red


I am attempting to write a collision map for an old school RPG. I created an Image using the color red (255, 0, 0) where collisions should occur and am trying to test for when my sprites positions (x, y) will turn up as red on the collision map. Here is my code that won't seem to work:

public boolean isBlocked(int testX, int testY) {

    System.out.println(collisionMap.getColor(testX, testY)); //debug
    System.out.println(Color.red); //debug

    if ((collisionMap.getColor(testX, testY)) == Color.red) {
            System.out.println("COLLISION OCCURRING!"); //debug
            return true;
    }
    else {
        System.out.println("NO COLLISION OCCURRING!"); //debug
        return false;
    }
}

When a collision should be occurring, I get the following output in my console:

Color (1.0,0.0,0.0,1.0)

Color (1.0,0.0,0.0,1.0)

NO COLLISION OCCURRING!

For some reason the If statement isn't picking up the two values to be equal, even though they appear to be.


Solution

  • I believe this is because Color.red is an object, not a value. Try using

    collisionMap.getColor(textX, testY).equals(Color.red)