测试颜色是否相等
我正在 iTunes U 上完成斯坦福大学讲座的突破作业(仍然很绿),但遇到了麻烦。我试图为不同颜色的砖块设置一个分值,以便我可以计算分数,但我的 if 似乎不起作用。我有一种感觉 getColor() 没有返回我认为的值;我创建了一个状态标签来显示它返回的内容,但我仍然不知道如何测试它。这很可能是我遗漏的或者只是还不知道的简单的东西。
这是我正在处理的部分片段:
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;
}
}
如果您需要完整的代码,请告诉我。
I'm working on the Breakout assignment from the Stanford lectures on iTunes U (still pretty green) and ran into a snarl. I'm trying to set a point value for the different colored bricks so I can calculate a score but my if's don't seem to work. I have a feeling that getColor() isn't returning the value that I think it is; I created a status label to show my what it's returning but I still can't figure out how to test for that. More than likely it's something simple I'm missing or just don't know of yet.
Here's a snippet of the bit I'm working on:
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 the full code let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于类似于
c == Color.X
的情况,请使用Color.X.equals(c)
。您正在测试对象是否是同一实例,而不是测试它们是否被认为彼此相等。您还可以像使用
Color.RED
一样使用c.equals(Color.X)
,但是许多人更喜欢使用其他方法来防止NullPointerException< /code> 适用于
c
为null
的情况。Use
Color.X.equals(c)
for your if cases that are likec == Color.X
. You're testing if the objects are the same instance, instead of if they're considered to be equal to each other.You could also use
c.equals(Color.X)
like you did forColor.RED
, however many people prefer the other way to safeguard against aNullPointerException
for cases wherec
isnull
.