Reputation: 75
I'm making a game that produces a 3 pointed star (3PS) and 4 pointed star (4PS). You must make 3PS fall to the right, and 4PS to the left. Im using if statements to check if they have fallen down the correct route but its not working properly. I've created a star class for 3PS and a class 4PS which extends 3PS class. I have a variable that increments every time a star falls below the screen, the 3PS seems to be ok but the 4PS increments the variable for the 4PS and the 3PS. How can I fix this?
Here is my code
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;
}
}
Here is the 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
Upvotes: 0
Views: 63
Reputation: 8562
four star is subclass of star so it will also pass that if
if (star[a] instanceof Star) {
You could for example have code:
boolnea isOnLeft = starW <= (.5 * dimension[0]);
boolean isOut = starH <= 0;
if (star[a] instanceof fourStar) {
if (isOnLeft && isOut) {
...
}
} else { //3ps
if (!isOnLeft && isOut) {
...
}
}
Nevertheless it is not very OO if you check for the actuall class.
You should have Star class with two sublcasses 3PS and 4PS. Then in star class you define abstract method boolean shouldCount(position)
, in 3PS you implement the shouldCount() to return true if the position is on the right and out of the screen, in 4PS other way around. Then you simply call shouldCount
and increase the score.
Upvotes: 2