Java – test for color equality
I was finishing the breakout task from the Stanford speech (still green) on iTunes u and got into a roar I'm trying to set a point value for bricks of different colors so that I can calculate a score, but my if doesn't seem to work I don't think getcolor () returns the value I think; I created a status tag to show what it returned, but I still couldn't figure out how to test it It's probably a simple one that I miss or don't know yet
This is a small part of what I am studying:
if (collider != null && collider != paddle) { remove(scoreLabel); vy = -vy; Color brickColor = collider.getColor(); add(new GLabel("" + collider.getColor(),10,12)); double temp = brickVal(brickColor) * scoreMultiplier; score += Math.abs(temp); addscoreboard(); remove(collider); } } private double brickVal(Color c) { if (c.equals(Color.RED)) { return 10.0; } else if (c == Color.ORANGE) { return brickVal = 8.0; } else if (c == Color.YELLOW) { return brickVal = 6.0; } else if (c == Color.GREEN) { return brickVal = 4.0; } else if (Color.CYAN.equals(c)) { return brickVal = 2.0; } else if (c == Color.MAGENTA) { return brickVal = 1.0; } else { return 1.0; } }
If you need complete code, please let me know
Solution
For image c = = color In the case of X, please use color X.equals(c). You are testing whether objects are the same instances, not that they are considered equal to each other
You can also use color Red uses C. equals (color. X), but when C is null, many people prefer to use other methods to prevent NullPointerException