5 Replies Latest reply on Jan 2, 2009 5:30 AM by Ned Murphy

    understanding if.. else and variable scope

    wmohns
      Hi all,
      In the code below I am looping over an array to create MovieClips, and using an if.. else statement to assign colours to the clips based on XML data. The trace of my XML nodes yields values of 2,1,5,3, meaning that in each case, one of the if statements should have been true and assigned a colour value to the myArray _colour variable, but all four of the resulting clips come out black (the "else" value), and the trace yields a zero for each item. I'm guessing that the problem is that I don't properly understand variable scope, but I'm hoping that someone can tell me if that is indeed the case, and then point me in the right direction. Thanks!
        • 1. understanding if.. else and variable scope
          Ned Murphy Adobe Community Professional & MVP
          I'm not an XML expert, but aren't the values delivered actually strings? ( == "1"... etc).

          Are the a_color (etc) variables declared somewhere? If not, then you probably don't need to use anything more than a simple variable within the loop (they won't exist anywhere else otherwise).
          • 2. Re: understanding if.. else and variable scope
            wmohns Level 1
            Thanks for the reply NedWebs. Tried both suggestions, but no change in the result. I declare the variable (and trace it to ensure that the declaration works), but still none of the if conditions are successful, and changing the conditions to evaluate for strings (e.g. =="1") doesn't change the result either.
            Any other thoughts?
            • 3. understanding if.. else and variable scope
              Ned Murphy Adobe Community Professional & MVP
              I just noticed one problem that probably will help you on the road to recovery. Your string of if statements will all be disregarded by the last conditional since it is the only one with an else statement. The only case that might prevail is when the xml value == 5

              If you want any of the first 4 of them to stick, they all need to be tied together with else's.

              if(){

              } else if(){

              } else if(){

              } else ...

              You would be better off using a 'switch' rather than a series of if/else conditionals, but you can implement it with them if you prefer. The help files will provide an example using switch.

              Also, to remove any issue between the values being numbers vs strings, and to ease the coding weight, you can start off your for loop with the line...

              var colorValue = Number(myXML.firstChild.childNodes[ i ].childNodes[3].childNodes);

              NOTE: that [ i ] has extra spaces... this forum treats it as an italics tag otherwise... you can see how your first post was affected in using it.

              and then use if(colorValue == 1){ etc... for your conditionals (the switch approach eliminates the repeated conditional test code).
              • 4. Re: understanding if.. else and variable scope
                wmohns Level 1
                Thanks Ned! There's a lot in your responses that has helped me, and in the end I think you're right about the xml being delivered as strings. I'll have to try it with parsInt().
                • 5. Re: understanding if.. else and variable scope
                  Ned Murphy Adobe Community Professional & MVP
                  You're welcome. That line where I assign var colorValue = Number(...xml...); should take care of the string issue.