我有兩個類:GameScreen.java和KeyButton.java。有三個按鈕是GameScreen類中引用的布爾值(是否按下它們),我想將它們與KeyButton類中的值進行比較。下面是相關代碼:布爾值在if語句中沒有改變
GameScreen.java:
private void checkButtons() {
// Log.d("GameScreen","Note and Bar Intersected");
KeyButton keybutton = new KeyButton();
if (B1Pressed == keybutton.getB1() && B2Pressed == keybutton.getB2() && B3Pressed == keybutton.getB3()) {
// Log.i("GameScreenTest", "Correct");
score++;
} else {
// Log.i("GameScreenTest - Result", "Wrong");
// Log.i("GameScreenTest - Button Pressed", Boolean.toString(B1Pressed));
// Log.i("GameScreenTest - Button Required", Boolean.toString(keybutton.getB1()));
}
}
KeyButton.java:
public class KeyButton {
public boolean B1 = false;
public boolean B2 = false;
public boolean B3 = false;
Rectangles Rectangles = new Rectangles();
{
//C major
if (ScalesScreen.scaleType.equals("C")) {
//C
if (GameScreen.intersected == Rectangles.rectangle1) {
B1 = false;
B2 = false;
B3 = false;
}
//D
if (GameScreen.intersected == Rectangles.rectangle2) {
B1 = true;
B2 = false;
B3 = true;
}
//E
if (GameScreen.intersected == Rectangles.rectangle3) {
B1 = true;
B2 = true;
B3 = false;
}
//F
if (GameScreen.intersected == Rectangles.rectangle4) {
B1 = true;
B2 = false;
B3 = false;
}
...
public boolean getB1() {
// TODO Auto-generated method stub
//Log.d("Key 1", Boolean.toString(B1));
return B1;
}
public boolean getB2() {
// TODO Auto-generated method stub
//Log.d("Key 2", Boolean.toString(B2));
return B2;
}
public boolean getB3() {
// TODO Auto-generated method stub
//Log.d("Key 3", Boolean.toString(B3));
return B3;
}
出於某種原因,B1,B2和B3值在GameScreen返回false代碼(來自keybutton.getB1()),只要沒有按下按鈕,分數就會增加。所以,出於某種原因,布爾值不會被改變爲它們應該在KeyButton類中。我能做些什麼來解決這個問題?
新增:
for (Rect rect : rectangles) {
if(Rect.intersects(rect, bar)) {
// Log.d("GameScreen", "intersected");
intersected = rect;
// Log.d("Intersected", intersected.toString());
checkButtons();
}
}
四個交叉檢查是否意外嵌套在if(ScalesScreen.scaleType.equals(「C」))塊中? –
如果按下所有按鈕或者按下按鈕中的任何一個,布爾值將發生變化 –
@Shashank Udupa它們應該根據相交的矩形(添加的代碼部分)是否與在不同的矩形中定義的矩形相同類(稱爲矩形,它不會改變)。 – user2712795