4 Replies Latest reply on Feb 27, 2010 5:35 PM by celebritymusic

    How to use Global Functions in AS3?

    celebritymusic Level 1

      Hi

       

      I have about 10 as3 coded buttons that use the same AS3 coded parameters to control the font and filters of their text fields.

       

      I'd like to create a global function to hold these parameters, and then just call the function within each button, and then also code each button to have it's own myText.text = "blahblah";

       

      I can set up a global function no problem using an external as file, but am having trouble getting Flash to accept the parameters.  For example, the following parameters work fine when used within the text fields of the buttons, but as so as I try to put them in an external global function, Flash doesn't like the parameters being external:

       

       

      ------------------

       

      var myFont:Font = new Font1();
      var myFormat:TextFormat = new TextFormat();
      myFormat.font = myFont.fontName;

       

       

       

      myText.defaultTextFormat = myFormat;
      myText.autoSize = TextFieldAutoSize.LEFT;
      myText.embedFonts = true;

       

       

       

      myText.text = "blahblah";

       

      ------------------

       

      and here's my global code:

       

      package     

       

      {          public class globals 
              {   
                       
                public static function performAction():void {

      var myFont:Font = new Font1();
      var myFormat:TextFormat = new TextFormat();
      myFormat.font = myFont.fontName;
      myFormat.size = globals.data.mainFontSize;
      myFormat.color = globals.data.mainFontColor;

       

      myText.defaultTextFormat = myFormat;
      myText.autoSize = TextFieldAutoSize.LEFT;
      myText.embedFonts = true;
      myText.filters = [globals.data.upStroke, globals.data.upShadow];

       

                 
                }
               
              }

       

      }

       

       

      -----------

       

      and the new button code:

       

      globals.perfromaction();

       

      myText.text = "blahblah";

       

       

       

      So what can I use in the external function?

       

      Thanks for your time and help

       

      Shaun

        • 1. Re: How to use Global Functions in AS3?
          RossRitchey Level 4

          A couple quick tweaks should get this working for you:

           

          package     
          {
               public class globals  
               {    
                            
                    public static function performAction(myText:TextField):void {
                         var myFont:Font = new Font1();
                         var myFormat:TextFormat = new TextFormat();
                         myFormat.font = myFont.fontName;
                         myFormat.size = globals.data.mainFontSize;
                         myFormat.color = globals.data.mainFontColor;
          
                         myText.defaultTextFormat = myFormat;
                         myText.autoSize = TextFieldAutoSize.LEFT;
                         myText.embedFonts = true;
                         myText.filters = [globals.data.upStroke, globals.data.upShadow];
                    }
               }
          }
          
          

           

          New button code:

           

          globals.perfromaction(myText);
          
          myText.text = "blahblah";
          
          

           

           

          Essentially, because the object myText doesn't exist in the function, it will give you errors.  I worked around this by passing a reference to the TextField into the function.

          • 2. Re: How to use Global Functions in AS3?
            celebritymusic Level 1

            Thanks Ross

             

            I get an error saying - type was not found or was not a compile time constant - TextField, and then refers to

            public static function performAction(myText:TextField):void {

            Also, I had a spelling error in my button code:

            globals.perfromaction(myText);

            myText.text = "blahblah";

            Should have been

            globals.performAction(myText);

            But that didn't make a difference.

             

            Any idea as to why that error appears?

             

            Thanfs for your time and help.

             

            Shaun

            • 3. Re: How to use Global Functions in AS3?
              RossRitchey Level 4

              you need to import the TextField class into the globals package:

               

              import flash.text.TextField

              1 person found this helpful
              • 4. Re: How to use Global Functions in AS3?
                celebritymusic Level 1

                OK

                 

                That returns the following errors:

                 

                1046: Type was not found or was not a compile-time constant: Font. - var myFont:Font = new Font1();
                1046: Type was not found or was not a compile-time constant: TextFormat. - var myFormat:TextFormat = new TextFormat();
                1180: Call to a possibly undefined method TextFormat. - var myFormat:TextFormat = new TextFormat();
                1120: Access of undefined property TextFieldAutoSize. - myText.autoSize = TextFieldAutoSize.LEFT;

                 

                 

                I tried importing the classes for above too, but that didn't make a difference.  Is my coding correct?

                 

                package

                 

                 

                {          public class globals 

                 

                        {

                 

                        import flash.text.TextField;

                 

                         public static function performAction(myText:TextField):void {

                 

                 

                var myFont:Font = new Font1();

                 

                var myFormat:TextFormat = new TextFormat();

                 

                myFormat.font = myFont.fontName;

                 

                myFormat.size = globals.data.mainFontSize;

                 

                myFormat.color = globals.data.mainFontColor;

                 

                myText.defaultTextFormat = myFormat;

                 

                myText.autoSize = TextFieldAutoSize.LEFT;

                 

                myText.embedFonts = true;

                 

                myText.filters = [globals.data.upStroke, globals.data.upGlow, globals.data.upShadow];

                 

                }        

                 

                 

                }

                 

                }

                 

                 

                and my button code:


                globals.performAction(myText);

                 

                myText.text = globals.data.menu1;

                 

                 

                stop();

                 

                 

                Perhaps it's the airshow that's going on outside my window that's to blame - very pleasurably distracting.

                 

                Cheers

                 

                Shaun