3 Replies Latest reply on May 5, 2006 11:20 AM by Peter Lorent

    Need a little help on Scope

    ChesStrategy
      As a fairly new user of Flash MX and Actionscript, I have a couple of questions regarding the scope of both variables and functions in actionscript.

      Since Flash is usually used to design linear movies. I was wondering exactly how Actionscript in Frames within the timeline work. Does the script only run when the playhead reaches that particular frame?

      If so, how does a conditional loop work within a script within a frame? Does Flash wait for the loop to finish before moving to the next frame? Also, if I make a user-defined function within a frame of the timeline, what the the valid "locations" in which I can call or re-use the function?

      I've searched tirelessly for an answer from most other Flash coder forums and still have not recieved a response.

      Thanks for taking this matter into consideration.
        • 1. Re: Need a little help on Scope
          scottPadgett
          Yes, a script placed on a frame only runs once the player has reached that frame. Not exactly sure, but I believe that Flash runs all code in the frame before advancing to the next, because I know an infinite loop in a frame's code will stop the player.

          I know this with regard to defining functions:

          You can define a function in frame one as a prototype method of the MovieClip class and then call that method on any movieclip that you've got in your movie or have created programatically at any time later in the movie. Like so:


          Frame 1:

          MovieClip.protoype.thisMethod = function() {

          function stuff;

          }


          then you can do this in frame 2 or whatever:

          createEmptyMovieClip("this_mc", 88);
          this_mc.thisMethod();


          Hope this helps. I'm no expert compared to other forum members....

          • 2. Re: Need a little help on Scope
            ChesStrategy Level 1
            quote:

            You can define a function in frame one as a prototype method of the MovieClip class and then call that method on any movieclip that you've got in your movie or have created programatically at any time later in the movie.


            I'm not entirely sure what you mean by 'prototype method', but as for the other part of your comment...can you call the method defined in (for example) frame 1, in any other frame on any other layer within that timeline?

            You also mentioned that you can call the method on any movieclip within the entire flash movie, does that include calling the 'frame' function from another object/script inside the movieclip? (or are you only limited to calling the function from the actions panel of that movieclip?

            Since Ive only used Actionscript a little, is it true that you can only initialize variables at the very beginning of the movie (a.k.a. _root and in the first few frames) or can you initialize variables anywhere? If you are able to initialize variables anywhere within the entire flash movie, exactly what is the scope of the variables defined/initialized in the function that is within the frame? Do these variables get erased or deleted as soon as the playhead passes the frame?

            Thanks for helping so far.
            • 3. Re: Need a little help on Scope
              Peter Lorent Level 2
              A few steps back.
              There are three kinds of scope:
              1. local
              A variable with a local scope is only accessible from within the code block where it was declared. Once there is no use for the variable anymore it is (automatically) deleted. A local variable is declared using the var keyword.
              function traceMe():Void{
              var test_string="Just a test"; // variable local to the function
              }
              2. timeline
              Timeline-scoped variables belong to a particular object, such as a MovieClip, but are accessible from anywhere else using a target path. They exist as long as the object to which they belong exists or they are specifically removed using the delete keyword.
              3. global
              Accessible from anywhere in your movie. You do not need to use the _global identifier to access the variable, only when declaring it. There can only be one global variable with the name you assigned it.

              Functions are blocks of code that are defined once and then called whenever they are required to be executed. Reusability is their primary benefit. Once a function has been defined it can be called from any timeline, including the timeline of a loaded swf, provided the correct target paths are used. You can use the _global identifier to declare a function to all timelines without having to use a specific target path. When the Flash Player reaches a frame that contains a function declaration, the function is created. The statements within the body of the function will not execute untill the function is called.