我正在制作一款能产生三角星(3PS)和四角星(4PS)的游戏。您必须使3PS向右下降,向左移动4PS。我使用if语句来检查它们是否已经落入正确的路线但是它无法正常工作。我为3PS创建了一个星级,并且为4PS类创建了一个类4PS。我有一个变量,每当一颗星落在屏幕下方时增量,3PS似乎没问题,但4PS增加了4PS和3PS的变量。我该如何解决这个问题?
这是我的代码
if (0 <= starW && starW <= (.5 * dimension[0]) && starH <= 0) {
if (star[a] instanceof fourStar) {
if (count4Check == false) {
count4C++;
count4Check = true;
starAdded = 0;
Log.i("count4C", String.valueOf(count4C));
}count4Check = false;
}
}
if (0 <= starW && starW <= (.5 * dimension[0]) && starH <= 0) {
if (star[a] instanceof Star) {
if (count3Check == false) {
count3W++;
count3Check = true;
starAdded = 0;
Log.i("count3W", String.valueOf(count3W));
}count3Check = false;
}
}
if ((.5 * dimension[0]) <= starW && starW <= dimension[0] && starH <= 0) {
if (star[a] instanceof Star) {
if (count3Check == false) {
count3C++;
count3Check = true;
starAdded = 0;
Log.i("count3C", String.valueOf(count3C));
}count3Check = false;
}
}
if ((.5 * dimension[0]) <= starW && starW <= dimension[0] && starH <= 0) {
if (star[a] instanceof fourStar) {
if (count4Check == false) {
count4W++;
count4Check = true;
starAdded = 0;
Log.i("count4W", String.valueOf(count4W));
}count4Check = false;
}
}
这是logcat
03-21 20:06:36.620 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:06:39.623 24716-25725/com.example.james.sata I/count3C﹕ 9
03-21 20:06:39.713 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:06:42.276 24716-25725/com.example.james.sata I/count4C﹕ 2
03-21 20:06:42.276 24716-25725/com.example.james.sata I/count3W﹕ 5
03-21 20:06:42.356 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:06:45.149 24716-25725/com.example.james.sata I/count3W﹕ 6
03-21 20:06:45.239 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:06:48.592 24716-25725/com.example.james.sata I/count4C﹕ 3
03-21 20:06:48.592 24716-25725/com.example.james.sata I/count3W﹕ 7
03-21 20:06:48.692 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:06:51.726 24716-25725/com.example.james.sata I/count4C﹕ 4
03-21 20:06:51.726 24716-25725/com.example.james.sata I/count3W﹕ 8
03-21 20:06:51.776 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:06:54.779 24716-25725/com.example.james.sata I/count3C﹕ 10
03-21 20:06:54.859 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 3 star added
03-21 20:07:10.446 24716-25725/com.example.james.sata I/count3C﹕ 11
03-21 20:07:10.526 24716-25725/com.example.james.sata I/MyGLRenderer﹕ 4 star added
03-21 20:07:25.772 24716-25725/com.example.james.sata I/count3C﹕ 12
03-21 20:07:25.772 24716-25725/com.example.james.sata I/count4W﹕ 3
答案 0 :(得分:2)
四星是星的子类,所以如果
也会传递它if (star[a] instanceof Star) {
例如,您可以拥有代码:
boolnea isOnLeft = starW <= (.5 * dimension[0]);
boolean isOut = starH <= 0;
if (star[a] instanceof fourStar) {
if (isOnLeft && isOut) {
...
}
} else { //3ps
if (!isOnLeft && isOut) {
...
}
}
然而,如果你检查实际课程,它不是很好。
你应该有Star class,有两个subpcasses 3PS和4PS。然后在星级类中定义抽象方法boolean shouldCount(position)
,在3PS中你实现了shouldCount(),如果位置在右侧和屏幕之外,则返回true,以4PS反过来。然后,您只需致电shouldCount
并提高分数。