7 Replies Latest reply on Dec 22, 2008 3:00 PM by kglad

    Scope within Functions

    Damo1954
      Hi,

      Im having a few hickups with what i believe to be a scope issue. I have a frame of code that i run in one of my movieclip's time lines. This code obviously runs as soon as the timeline reaches that frame which works perfectly. However, now i would like to stop that code from running on that frame and have it run when i want it to. For this reason I captured all the relevant code into a function so that i could call on it when i need it. This on the other hand does not work perfectly and has problems loading some images amoungst other things. Is this a scope issue that can be resolved easily or do i need to dive into the whole hierachial structure to determine what scope problems im having??

      Thanks heaps in advance for any help

      Damien

      PS: as you can see at the top and bottom of the code i have remarked out the function i tried to nest the code in.
        • 1. Re: Scope within Functions
          kglad Adobe Community Professional & MVP
          you can't nest named functions and expect the inner function to be defined. using an anonymous function for the inner function works.
          • 2. Re: Scope within Functions
            Damo1954 Level 1
            thanks kglad

            but the inner functions are not called upon by anything else but one another. But your saying that if the inner functions (those that i am trying to nest in one generic function) are anonymous, then i will not have any problems?? So all my problems are caused by the inner functions having names?? This doesnt make sense to me. Why would the names of the inner functions effect the scope??

            I think i must be misinterpreting what your saying. Further explanation would be greatly appreciated.

            Thanks again for you help
            Damien
            • 3. Re: Scope within Functions
              kglad Adobe Community Professional & MVP
              if your nested function is only called by the outer function, you should be ok.

              but that's terrible coding: you have 3 nested functions. unnest them unless absolutely necessary.

              and why are you checking if i>12? when could it be anything other than 1?

              and "this" in your onMotionFinished() function is probably referencing the tween. use the trace() function to check.
              • 4. Re: Scope within Functions
                Damo1954 Level 1
                hi kglad,

                It does look a bit messy but its not my coding. I am editing a template to perform tasks i want it to do. All i want is for that code i posted there not to run on the entry of that frame. I want to be able to call it on an event. Can you suggest a better way of doing this??

                Also it is checking if i>12 because it is only loading 12 items. And it can be a value greater then 1. See how it sets an interval to run the Gen function, not the effects function where you are seeing the "var i = 0".

                And it is referencing the tween but why will that make a difference.

                Thanks again for your help,
                Damien
                • 5. Re: Scope within Functions
                  kglad Adobe Community Professional & MVP
                  oh, i see. try:

                  • 6. Re: Scope within Functions
                    Damo1954 Level 1
                    YES!!! It finally worked.

                    Thanks for ya help kglad. Will no doubt be in touch with another problem soon lol

                    Thanks Again
                    Damien
                    • 7. Re: Scope within Functions
                      kglad Adobe Community Professional & MVP
                      you're welcome.