Skip to main content
June 10, 2012
Answered

nested if "logic" question

  • June 10, 2012
  • 1 reply
  • 868 views

I've got 6 locations in an object that will be clickable (color pallet red-purple).  I am determining which color was clicked on by the mouse location at on(press), in comparision to the objects location.

Question:

Is there a better way then a nested if else if else.... to get to the same answer?

If(mouse_location = red_location) { 'color = red' }

else {

if(mouse = orange) { 'color = orange'}

else {

if(mouse = yellow) { 'color = yellow' }

else {

if(mouse = green) { 'color = green' }

else {

if(mouse = blue) {'color = blue'}

else { 'color = purple'}

}}}}

This topic has been closed for replies.
Correct answer Ned Murphy

A more standard way of writing a series of conditionals would be...

if(){

} else if(){

} else if(){

} else {

}

and when you compare values use "==", not "="

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
June 10, 2012

A more standard way of writing a series of conditionals would be...

if(){

} else if(){

} else if(){

} else {

}

and when you compare values use "==", not "="

June 10, 2012

Ya it was just an example, actual code compares _xmouse and _ymouse with this._x and this._y, and the color isn't a string variable.  Thanks for the example wasn't aware of not needing the extra brackets for each else.

Ned Murphy
Legend
June 10, 2012

You're welcome.

As far as better ways go, there are probably better ways, but without knowing the details of what you are doing, it's easier to just go with what you have.   You would have an easier time using AS3 as you could have the object clicked identified in the click handling function and wouldn't have to use any conditionals to sort it out.