3 Replies Latest reply on Apr 19, 2012 7:52 PM by Ned Murphy

    Actionscript 2 syntax question

    jdhughen Level 1

      I have a style sheet that I want to apply to a large group of dynamic text fields but not to the whole fla. Is there a way to address all my content fields and apply the sheet in a single statement ?

       

      now I have

       

      var myStyleSheet:TextField.StyleSheet = new TextField.StyleSheet();

      myStyleSheet.load("styles.css");

      content_box1.styleSheet = myStyleSheet;

      content_box2.styleSheet = myStyleSheet;

      content_box3.styleSheet = myStyleSheet;

      content_box4.styleSheet = myStyleSheet;

      content_box5.styleSheet = myStyleSheet;

       

      etc etc.....

       

      I'm new to actionscript 2 and not sure how to do looping structure. Currently there are 15 of these "boxes" but this could grow, how would i go about doing some kind of "for/next" loop like..

       

      For n=1 thru 15

      content_box(n).styleSheet = myStyleSheet;

      next n

       

      OR

       

      could you do some kind of If/then structure ??

       

      OR just

       

      content_box1.styleSheet, content_box2.styleSheet, content_box3.styleSheet, content_box4.styleSheet, content_box5.styleSheet, content_box6.styleSheet, etc = myStyleSheet;

       

      A solution based on the first concept would be prefered cause I could setup vars updated dynamicly so it would make it very easy to add boxes and content on the fly via the xml file.

       

      Thanks

       

      Joel

        • 1. Re: Actionscript 2 syntax question
          Ned Murphy Adobe Community Professional & MVP

          You can use bracket notation to have strings targeting objects...

           

          for(i=1; i<16; i++){

               this["content_box"+i].styleSheet = myStyleSheet;

          }

          • 2. Re: Actionscript 2 syntax question
            jdhughen Level 1

            Ned, et al,

             

            Thanks for the quick response.  that's perfect.

            A little more explanation... this is a website for a softball team, the content of each of these text fields (boxes) are game results, include 1 game image, 1 graphic (box score) and a html formated text game recap. these are loaded from xml file into each game "box" and the boxes are placed in a mc with a mask window and a scrollbar. Currently when I update the website with new games I have to go back to the fla, add more game "boxes", update, publish, and upload the new swf to the website. While a pain it worked for me since I had everything.... Long story shortened... my kid is graduating and I want to hand off the daily updating of the website. I want to do it all with the xml file. sooo...

             

            In that same statement could I use a dynamic imported var at runtime for my "i", in this case the total number of games reported, so I could use the same var to name the "content_box" in a looped creation statement? Right now I create all the Content boxes seperately, so like the above could i do something like:

             

            imported var, Total_Games = 15

            var total=Total_Games+1

             

            for(i=1; i<total; i++){

             

                 name="game_box"+i;  // not sure how to tell it the "i" value should be considered a string value here and not a number value ?

                 this.createTextField(name, this.getNextHighestDepth(), 10, 10,100, 200);

                 name.border = true;

                 name.html = true;

                 name.multiline = true;

                 name._x = i * 100; // place the box next in the list

                 this[name].styleSheet = myStyleSheet;

                 name.text =  ??? ;         // it's xml provided content

            }

             

            I sure there are more details to figure out (like updating the mc height and scrollbar ranges to include new game "space" ), but would this general concept work so that additional "games" just need to be added to the XML file and they would be included in the swf at runtime?

             

            Hope this made sense

             

            thanks

             

            Joel

            • 3. Re: Actionscript 2 syntax question
              Ned Murphy Adobe Community Professional & MVP

              The general concept looks okay.

               

              There is no need to store the i if you are retrieving the data from an xml file.  If the xml file holds sets of data, the number of sets can be determined when parsing it.

               

              As far as using the "i" for a String, generally you don't need to sweat appending a number to a string, it will work as you show it anyways, especially with AS2.  But if you want to be thorough, you can make it a String... 

               

              name = "game_box"+String(i);