4 Replies Latest reply: Aug 21, 2013 10:02 PM by SeanPercy42 RSS

    How do I change a global variable from inside a function.

    SeanPercy42 Community Member

      There has got to be a semi-simple way to accomplish this. I think it's unbelievable that it's taken me longer than 5 minutes to figure this out. I declared a variable bookPosition as a string with a value of "up". Once a certain function runs I need to change that variable to a value of "down". THAT'S IT! Why I'm having trouble doing that is unexplainable. The variable is declared outside of a function which automatically makes it a global variable (correct?). Then basically all I need to do is change that global variable from inside a function. Here's the code I have to do that and it does not currently change it:

       

      var bookPosition:String = "up";

      var currentPage:String;

       

      function slideServicesIn ():void{

          if (bookPosition == "up"){   

              book.gotoAndStop('webDesignPage');

              TweenLite.to(book, 3, { y:280 } );

              var bookPosition = "down";

              var currentPage = "book.servicesPage";

          }

          if (bookPosition == "down" && currentPage != "book.servicesPage"){

              TweenLite.to(book.servicesPage, 1, { alpha:1, ease:Quad.easeIn, delay: .2 } );

          }

      }

       

      The simplest solution to accomplish what I'm trying to do I will use.

       

      I'm basically assigning these two variables so that they can keep track of whether a book is on the screen or off, and what page it's currently on so that it doesn't slide in if it's already "down" and it doesn't try to crossfade (using TweenLite) to a page it's already on.

        • 1. Re: How do I change a global variable from inside a function.
          kglad CommunityMVP

          you're making bookPosition local to slideServicesIn (and defining a 2nd variable named bookPosition) when you prefix it with var.  use:

           

          var bookPosition:String = "up";

          var currentPage:String;

           

          function slideServicesIn ():void{

              if (bookPosition == "up"){   

                  book.gotoAndStop('webDesignPage');

                  TweenLite.to(book, 3, { y:280 } );

                  bookPosition = "down";

                  currentPage = "book.servicesPage";

              }

              if (bookPosition == "down" && currentPage != "book.servicesPage"){

                  TweenLite.to(book.servicesPage, 1, { alpha:1, ease:Quad.easeIn, delay: .2 } );

              }

          }

          • 2. Re: How do I change a global variable from inside a function.
            SeanPercy42 Community Member

            Alright, that's what I originally had but it wasn't tweening my page in. I changed it back to the way you recommended but it's still not crossfading between pages. Any idea why not?

             

            When the slideServicesIn function runs it set's the bookPosition variable to "down" and the currentPage variable to "book.servicesPage", so since it's already down I should be able to click on another button (slidePortfolioIn for example) and it should crossfade to the portfolio page since the book is down & the currentPage isn't "book.portfolioPage". The functions for each page are identical except for the names of the pages. You can look at the if statement in the slideServicesIn function and replace the book.servicesPage variables with book.portfolioPage to get an idea of what the code looks like for the other functions.

            • 3. Re: How do I change a global variable from inside a function.
              kglad CommunityMVP

              that if-statement is only going to execute when your page loads, not after slideServicesIn() executes.

               

              you probably want to put those tween statements in a function that gets called after assigning your strings variables.

              • 4. Re: How do I change a global variable from inside a function.
                SeanPercy42 Community Member

                Nevermind, I got it working well enough for right now and once I learn more about variables and TweenLite I'll come back in and tweak the code. Thanks for all your help anyway.