2011-09-22 42 views
0

好吧,我在這裏有三個條件。這看起來很簡單,但我無法按照自己的想法做到這一點,在我嘗試解釋它時忍受着我。有條件的陳述(請參閱說明)

If(conditionA){ 
Play Sound 1} 

If(conditionB) { 
changedrawable ///(irrelevant)  

}

If(conditionA && conditionB){ 
Play sound 2 
} 
    ////Unfortunately, since condition A is true, it plays sound 1 too, and I only want it to play sound 2 

FWIW這些條件只是座標上的觸摸屏,和我有他們設置爲布爾的

Boolean conditionA; 
conditionA = y > 0 && y < (0.2 * tY) && x > 0 && x < (0.1667 * tX) || y2 > 0 
      && y2 < (0.2 * tY) && x2 > 0 && x2 < (0.1667 * tX); 

ConditionA和ConditionB指的是不同的點上觸摸屏。主要問題:當條件A和條件B都爲真時,我怎樣才能得到它,只播放聲音2(而不是兩個聲音)?試圖解決這個問題的最後一天,我一直拉着我的頭髮。請讓我知道是否有解決方案或需要進一步的細節。謝謝!

回答

0

它不應該是

if(conditionA && conditionB) { 
    play sound 2 
} else if(conditionA) { 
    play sound 1 
} 
if(conditionB) { 
    change drawable 
} 

您只需要其他的播放聲音。不用於更改drawable。

0

如何:

if (conditionA && conditionB) { 
    // play sound2 
} else if (conditionA) { 
    // play sound1 
} else if (conditionB) { 
    // change drawable 
} 

如果這兩個條件不滿足,它會運行那是條件的代碼。如果他們都滿意,它只會運行//play sound2

0

只要把第三個選項作爲第一個選項,然後在其他條件都不滿足的情況下使用其他ifs。

編輯/他說什麼:)

0

這可能不是最完美的解決方案,但它應該工作:

if (conditionA && conditionB) { 
    play sound 2 
} 
else { 
    if (conditionA){ 
    play sound 1 
    } 
    if (conditionB) { 
    change drawable 
    } 
} 
1

這可能不是完全最優的,但它很容易閱讀:

if (conditionA && conditionB) { 
    play sound 2 
} else if (conditionA) { 
    // Implies that conditionB == false 
    play sound 1 
} else if (conditionB) { 
    change drawable 
} 
+0

如果conditionA和conditionB爲真,那麼「change drawable」永遠不會執行。這似乎不是問題中所解釋的預期流程。 – Danny