2016-02-18 74 views
-2

我有兩個類: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(); 

    } 
} 
+0

四個交叉檢查是否意外嵌套在if(ScalesScreen.scaleType.equals(「C」))塊中? –

+0

如果按下所有按鈕或者按下按鈕中的任何一個,布爾值將發生變化 –

+0

@Shashank Udupa它們應該根據相交的矩形(添加的代碼部分)是否與在不同的矩形中定義的矩形相同類(稱爲矩形,它不會改變)。 – user2712795

回答

1

您正在創建一個新的KeyButton對象每當CheckButton方法被調用。每次標誌將被重置的情況就是這種情況。

KeyButton keybutton = new KeyButton(); 

取而代之的是使用Keybutton的全局實例或使KeyButton作爲單個實例。所以旗幟會持續下去。

public class KeyButton 
{ 
    public boolean B1 = false; 
    public boolean B2 = false; 
    public boolean B3 = false; 

    // Singleton 
    private static KeyButton sInstance; 
    private KeyButton() {} 

    public static synchronized KeyButton getInstance() 
    { 
     if (sInstance == null) 
     { 
      sInstance = new KeyButton(); 
     } 
     return sInstance; 
    } 

    .. 
} 
+0

我將此代碼添加到我的代碼並將我的代碼更改爲'KeyButton keybutton = KeyButton.getInstance();'但所有布爾值仍然返回false。 – user2712795