我正在制作一款产生3点尖星(3PS)和4点尖星(4PS)的游戏。您必须使3PS落在右侧,而4PS落在左侧。我正在使用if语句来检查它们是否掉入了正确的路线,但无法正常工作。我为3PS创建了star类,并为3PS类创建了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

最佳答案

四星是星的子类,因此如果

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) {
  ...
  }
}


但是,如果您检查实际课程,那不是很OO。
您应该具有两个子类别3PS和4PS的Star类。然后在star类中,定义抽象方法boolean shouldCount(position),在3PS中,如果位置在屏幕的右侧和屏幕外,则应实现shouldCount()以返回true,在4PS中则相反。然后,您只需调用shouldCount并提高得分。

07-24 20:21