开发者

Conditional Statements (please see explanation)

开发者 https://www.devze.com 2023-04-06 14:43 出处:网络
Ok, I have three conditions here. It\'s seems pretty simple, but I can\'t get it working how I want, bear with me as I try to explain it.

Ok, I have three conditions here. It's seems pretty simple, but I can't get it working how I want, bear with me as I try to explain it.

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 These conditions are just coordinates on the touchscreen, and I have them set up as boolean's

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 and ConditionB are referring to different points on the touchscreen. The main question: how on earth can I get it so when conditionA and conditionB are BOTH true开发者_如何学Go, ONLY Play Sound 2 (instead of both sounds)? I have been pulling my hair out the last day trying to figure this out. Please let me know if there is a solution or if you need further details. Thanks!


This may not be perfectly optimal, but it's easy to read:

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


Shouldn't it be

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

You only want the else if for playing the sound. Not for changing the drawable.


How about:

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

If both conditions aren't satisfied, it'll run the code for the condition that is. If they are both satisfied, it'll only run //play sound2.


Just put the third option as the first, and then use else ifs in case not both conditions are fulfilled.

edit/ what he says :)


This may not be the most elegant solution, but it should work:

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

精彩评论

暂无评论...
验证码 换一张
取 消