7 Replies Latest reply on Apr 6, 2010 4:56 PM by Rothrock

    Possible to have if statement within filters code?

    celebritymusic Level 1

      Hi

       

      I have some filters set up, and have assigned some global vars to them, which I then use in my Filters code to display the filters:

       

      myText:Filters [globals.data.glow1, globals.data.stroke1, globals.data.shad1]

       

      Works perfectly.

       

       

      Now I want to assign a global var to each global filter var to determine if it should be shown or not.  So...

       

      globals.data.glow1On = true;

      globals.data.stroke1On = true;

      globals.data.shadOn = true;

       

      ... and then somehow incorporate these new vars into an if conditional to determine if the filter global vars get included in the myText:Filters code.

       

      However, I cannot seem to code the if conditional into the myText:Filters code correctly.

       

      I even tried seperating it out like so:

       

      if (globals.data.glow1On = true) {myText:Filters [globals.data.glow1];

      if (globals.data.stroke1On = true) {myText:Filters [globals.data.stroke1];

      if (globals.data.shad1On = true) {myText:Filters [globals.data.shad1];

       

      But Flash only makes the last filter true (kinda makes sense to me now why).

       

      Is there a way to do it without having to make an if conditional with 7 permutations?

       

      Cheers

       

       

      Shaun

        • 1. Re: Possible to have if statement within filters code?
          Ned Murphy Adobe Community Professional & MVP

          You are not coding the conditional properly.  "=" is for assignment, "==" is for comparing equality.

           

          if (globals.data.glow1On == true) {myText:Filters [globals.data.glow1];

          if (globals.data.stroke1On == true) {myText:Filters [globals.data.stroke1];

          if (globals.data.shad1On == true) {myText:Filters [globals.data.shad1];

           

          Also, a conditional evaluates whether or not something is true, so for Boolean values you do not need to explicitly compare them...

           

          if (globals.data.glow1On) {myText:Filters [globals.data.glow1];

          if (globals.data.stroke1On) {myText:Filters [globals.data.stroke1];

          if (globals.data.shad1On) {myText:Filters [globals.data.shad1];

           

          Lastly, for what I see that you showed, the conditionals are all missing their closing brackets, and while you can use them, the brackets are not needed for single command conditionals...

           

           

          if (globals.data.glow1On){ myText:Filters [globals.data.glow1]; }

          if (globals.data.stroke1On){ myText:Filters [globals.data.stroke1]; }

          if (globals.data.shad1On){ myText:Filters [globals.data.shad1]; }

           

          OR

           

           

          if (globals.data.glow1On) myText:Filters [globals.data.glow1];

          if (globals.data.stroke1On) myText:Filters [globals.data.stroke1];

          if (globals.data.shad1On) myText:Filters [globals.data.shad1];

          1 person found this helpful
          • 2. Re: Possible to have if statement within filters code?
            celebritymusic Level 1

            Thanks Ned

             

            I found that if I code...

             

            globals.data.glow1On == true;

            globals.data.stroke1On == true;

            globals.data.shad1On == true;

             

            if (globals.data.glow1On)  myText.filters = [globals.data.glow1];

            if  (globals.data.stroke1On) myText.filters = [globals.data.stroke1];

            if (globals.data.shad1On)  myText.filters = [globals.data.shad1];

             

            ...Flash only applies the last filter.  So in the above instance, only a shadow is applied, and in the following instance where the last two lines are swapped....

             

            if (globals.data.glow1On)  myText.filters = [globals.data.glow1];

            if (globals.data.shad1On)  myText.filters = [globals.data.shad1];

            if  (globals.data.stroke1On) myText.filters = [globals.data.stroke1];

             

            ...only the stroke is applied.  It's like each successive filters statement cancels the previous ones.

            • 3. Re: Possible to have if statement within filters code?
              Ned Murphy Adobe Community Professional & MVP

              You're still messing up on the = (assign) vs == (compare)

               

              globals.data.glow1On = true;

              globals.data.stroke1On = true;

              globals.data.shad1On = true;

               

              The last being applied is probably true, but I haven't worked enough with applying filters with code to have noticed.  The right side of you equations are arrays, and they have to include all of the filters you intend to apply.  So if your intention is to add another filter then you have to reaasign.

               

              Here's something you might try (meaning I would, but never have)  ...

               

              var filterArray:Array = new Array();

               

              if (globals.data.glow1On)  filterArray.push(globals.data.glow1);

              if  (globals.data.stroke1On)  filterArray.push(globals.data.stroke1);

              if (globals.data.shad1On)  filterArray.push(globals.data.shad)];

               

                myText.filters = filterArray;

              1 person found this helpful
              • 4. Re: Possible to have if statement within filters code?
                Rothrock Level 5

                Yes. Each object just has one filters property. It is an array, so if you want a bunch of different filters you need to put them in the array:

                 

                var temp:Array=[]

                 

                if(globals.data.glow1On){

                temp.push(globals.data.glow1);

                }

                if(globals.data.stroke1On){

                temp.push(globals.data.stroke1);

                }

                etc..

                 

                myText.filters=temp;

                 

                Of course push is pretty slow and if you are doing this a lot then there are probably better ways to do this whole thing.

                • 5. Re: Possible to have if statement within filters code?
                  celebritymusic Level 1

                  Ahh - speed is imperative here.

                   

                  If not a push array, then what other ways would be more efficient?

                   

                  Thanks for taking a look at this guys.

                  • 6. Re: Possible to have if statement within filters code?
                    celebritymusic Level 1

                    I just tried the array, and it seems to be working fine, but is there a more efficient way to code for the same result?

                     

                    PS - thanks for the array code guys

                    • 7. Re: Possible to have if statement within filters code?
                      Rothrock Level 5

                      I'm not sure. The whole design seems a bit wonky to me. But then again I don't really know all your requirements.

                       

                      The whole use of globals.data seems strange to me. What is that all about?

                       

                      And what changes glow1On (and the others) from true to false and vice versa?

                       

                      Are those booleans used for anything else?

                       

                      With three booleans you've got a maximum of 8 different states. Are all combinations possible?

                       

                      And the speed issue for this will really only be an issue if it is polling the data every frame  or some short timer interval. So perhaps they aren't changing all that often?

                       

                      In that case maybe some kind of event based system?