9 Replies Latest reply: Mar 16, 2010 1:54 PM by kglad RSS

    Global Variables in Flash CS4 (AS3) ?

    Me2LoveIt2 Community Member

      Hi,

       

      I  have another problem with my new project and I am looking to get answers for these two question:

       

      1.) Is there a way to define a global variable which can be accessed inside and movie clip or button? (where do you define it? is it changeable? can you pass it in by reference?)

       

      2.) If i have my main stage with some actionscript and a movie clip which has also some action script, IN WHICH ORDER does it execute?

       

       

      Thank you in advance for any help and tips!

        • 1. Re: Global Variables in Flash CS4 (AS3) ?
          kglad CommunityMVP

          you can create a class that allows you define pseudo-global variables:

           

          package{

          public class glo{

          public static var bal:Object = {};

          }

          }

           

          then import the glo class wherever needed and assign:


          glo.bal.whatever=whatevervalue;

           

          and retrieve

           

          glo.bal.whatever

           

          wherever else it's needed

          • 2. Re: Global Variables in Flash CS4 (AS3) ?
            Me2LoveIt2 Community Member

            Thank you for the answer!

             

            I am however still having problems.

             

            It's giving me this error: 1037:Packages cannot be nested

            I tried to google it couldnt make out the problem though. Any Ideas?

             

            Here is my code:

             

            //This is on frame 1, scene 1.
            package{
                 public class glo{
                      public static var widthOfSlider:Number = 5;
                 }
            }
            
            
            
            
            //This part is in Frame 1 of scene 1 > my_mc.
            
            var numOfCovers = 6;
            var temp = glo.widthOfSlider.Number;
            
            this.addEventListener(Event.ENTER_FRAME, centerB);
            function centerB(event:Event):void{
                 for(var i = 0; i < numOfCovers ; i++ ){ //This loop lines up the buttons inside my_mc
                      this["cover_btn_"+String(i)].y = 0;
                      this["cover_btn_"+String(i)].x = temp;
                      temp += this["cover_btn_"+String(i)].width + 5;
                 }
                 glo.widthOfSlider.Number = temp;
                 this.removeEventListener(Event.ENTER_FRAME, centerB);
            } 
             
             
            //I am trying to get the new width of my_mc . (so i can make my_mc.width = glo.widthOfSlider.Number [because it does not dynamically adjust])
            

             

            Thank you!

            • 3. Re: Global Variables in Flash CS4 (AS3) ?
              kglad CommunityMVP

              your code should be removed.  the code i suggested should be in a file named glo.as

              • 4. Re: Global Variables in Flash CS4 (AS3) ?
                Me2LoveIt2 Community Member

                Sorry for the inconvenience, but could you elaborate a bit?

                Thanks.

                • 5. Re: Global Variables in Flash CS4 (AS3) ?
                  kglad CommunityMVP

                  package{

                  public class glo{

                  public static var bal:Object = {};

                  }

                  }

                   

                  should be in a class file named glo.as

                   

                  click file/new/actionscript file.  paste the above code and save that file as glo.as

                   

                  then in any class where you want to assign a global variable use:

                   

                  import glo;

                  glo.bal.widthOfSlider = 5;

                   

                  and in any class where you want to retrieve that variable, use:

                   

                  import glo;

                  trace(glo.bal.widthOfSlider);

                  • 6. Re: Global Variables in Flash CS4 (AS3) ?
                    Me2LoveIt2 Community Member

                    Thank you this is what i needed I got it to work now!

                     

                    But I do still have one last question on this topic.

                     

                    This is the Code:

                     

                    //this is in a file Actionscript File called glo.as
                    package{
                         public class glo{
                              public static var widthOfSlider:Number = 5;
                         }
                    }
                    
                    
                    
                    // This code is in scene 1 > my_mc
                    import glo;
                    trace(glo.widthOfSlider);
                    
                    //This code is in scene 1 > my_mc > myNext_mc
                    import glo;
                    var numOfCovers = 6;
                    var temp = glo.widthOfSlider;
                    trace(glo.widthOfSlider);
                    
                    this.addEventListener(Event.ENTER_FRAME, centerB);
                    function centerB(event:Event):void{
                         for(var i = 0; i < numOfCovers ; i++ ){
                              this["cover_btn_"+String(i)].y = 0;
                              this["cover_btn_"+String(i)].x = temp;
                              temp += this["cover_btn_"+String(i)].width + 5;
                         }
                         glo.widthOfSlider = temp;
                         trace(glo.widthOfSlider);
                         this.removeEventListener(Event.ENTER_FRAME, centerB);
                    } 
                    
                    

                     

                    When I run this I get the following output:

                     

                    5

                    5

                    5

                    725.45

                    725.45

                     

                    The first "5" is from the trace(glo.widthOfSlider); in scene 1 > my_mc

                     

                    The next two "5"'s are from the trace(glo.widthOfSlider); in scene 1 > my_mc > myNext_mc

                     

                    Is there a way so that I can first execute the code in scene 1 > my_mc > myNext_mc before the code in scene 1 > my_mc ?

                    I am trying to get 725.45 through the trace(glo.widthOfSlider); in scene 1 > my_mc

                    • 7. Re: Global Variables in Flash CS4 (AS3) ?
                      kglad CommunityMVP

                      you can't change the order in which code is executed in flash.  code in nested movieclips always executes after code in parent movieclips.

                       

                      but you can always put code in a function body and call that function whenever you want.  so, while the code that defines a frame 1 parent function will always execute before code in frame 1 of a child, you can call that function much later, if you want.

                       

                      p.s.  you're still missing an important part of the glo class:  using that one class (the way i defined it) you can define an unlimited number of pseudo-global variables without touching the glo class itself.

                      • 8. Re: Global Variables in Flash CS4 (AS3) ?
                        Me2LoveIt2 Community Member

                        Thank you again this it rally great help and I really do appreciate it!

                         

                        This is the working code:

                         

                         

                        //this is in a file Actionscript File called glo.as
                        package{
                             public class glo{
                                  public static var bal:Object = {};
                             }
                        }
                        
                        
                        // This code is in scene 1 > my_mc FRAME 2
                        import glo;
                        trace(glo.bal.widthOfSlider);
                        
                        // This code is in scene 1 > my_mc FRAME 3
                        stop();
                        
                        //This code is in scene 1 > my_mc > myNext_mc FRAME 1
                        import glo;
                        var numOfCovers = 6;
                        var temp = 5;
                        this.addEventListener(Event.ENTER_FRAME, centerB);
                        function centerB(event:Event):void{
                             for(var i = 0; i < numOfCovers ; i++ ){
                                  this["cover_btn_"+String(i)].y = 0;
                                  this["cover_btn_"+String(i)].x = temp;
                                  temp += this["cover_btn_"+String(i)].width + 5;
                             }
                             glo.bal.widthOfSlider = temp;
                             this.removeEventListener(Event.ENTER_FRAME, centerB);
                        } 
                         
                        

                         

                        Thanks!

                        • 9. Re: Global Variables in Flash CS4 (AS3) ?
                          kglad CommunityMVP

                          you're welcome.