4 Replies Latest reply on Jul 21, 2007 6:07 PM by Some1Won

    Method calls on timeline (scope?)

    Some1Won Level 1
      Hello,

      I am using flash 8/AS 2.

      I am trying to call methods on the timeline from a class, and sometimes it works, and sometimes it doesn’t.

      A simplified example:
      In my fla, I have 2 layers, one called “script” and the other called “commScript”. In layer ‘script’ at frame ‘n’ (let’s say 5) and only frame ‘5’, I have:

      var applesFunction:Function = function():Void {
      trace(“apples called.”);
      }

      In layer “commscript”, in the first blank keyframe (and this keyframe extends through the timeline), I have:

      var grapesFunction:Function = function():Void {
      trace(“grapes called.”);
      }

      In some method in some class (let's call “fruitMixer”), I go:

      public function test():Void {
      _root.applesFunction();
      _root.grapesFunction();
      }

      This object is also created in frame 1, in the top layer (which would be ‘script’, “commScript” is on the layer below). When I run and call test() in the fruitMixer object, I get the output:

      “apples called.”

      On further poking, I find it actually can't even see the other method, a ‘trace(_root.grapesFunction);’ in the test() shows up as “undefined.”

      However, when I swap my layer order, suddenly, I get:

      “apples called.”
      “grapes called.”

      While, it works, it seems pretty random. My question is: why is it working? How does flash really handle this kind of thing? Does layer order really matter, or was it some sort of odd fluke/ “magic”?
        • 1. Method calls on timeline (scope?)
          Greg Dove Level 4
          This is because you have declared your functions as variables. And the variables are defined as the code executes. This same thing will happen even with code on the same layer in the same frame.
          e.g:


          I think you'll find different behaviour if you change it to:

          function grapesFunction():Void {
          trace(“grapes called.”);
          }

          Here's a simple example all on one frame:
          • 2. Re: Method calls on timeline (scope?)
            Some1Won Level 1
            Hi GWD,

            What you say makes sense: I am trying to use a variable before it is defined. However, the reason why I have my functions in variables is originally I tried just a straight declaration:

            Layer 1 (frame 1):
            function apples():Void {
            trace(“apples”);
            }

            Layer 2 (frame 1):
            function grapes ():Void {
            trace(“grapes”);
            }


            And my object is created in layer 1.

            From that object I couldn’t access grapes();. I noticed I was able to access variables anywhere, so that’s why I had put them in variables and called them that way.

            However, from playing with it just now, I noticed it had nothing to do with layer order... but it worked when I created my object in layer 2, rather then layer 1.

            From this, it looks like my object is able to access methods only in the ‘layers above’ ? And that is the scope it can access?: if I put it in the top layer, it can only access the top layer and if I create my object on the bottom layer, it should be able to access any method in any layer above?

            And likewise on frames, if a method is on frame 5, an object in a layer below on frame 1 can’t access it, but on or past frame 5, it can (as the play head/flash has reached the frame and ‘processed’ it)?
            • 3. Re: Method calls on timeline (scope?)
              Greg Dove Level 4
              There are two things here...

              You last point is absolutely correct.. all code executes for the first time in frames when the playhead reaches that frame. This is why sometimes people code preloader loops in the first few frames of large swfs and only send the code on to the main part of the swf timeline when the bytesLoaded = bytesTotal.

              When the playhead enters a new frame all the code is executed from top to bottom of layers/frame. Which leads me back to your first point - you noticed that layer order seems to matter.

              I ran some tests and came up with the same results as you in terms of function visibility by layer. I had thought (incorrectly) that functions were visible to all code within the same frame. The reason I thought this is that layers don't exist when actionscript is compiled to byte code (as a swf file). Layers are just a way of breaking things up inside the flash authoring environment to help us work on things visually in terms of how we think and organise things.

              When its compiled , and the layers are converted into the swf, all the author-time visual objects on stage retain their depth order but from an actionscript perspective its as if all the code (on the same frame number) in different layers was running in the same frame. A frame lower down just has its code 'appended' to the frames above. Or so I thought....

              Here's what I think now (I don't know if its accurate - perhaps someone else can either verify or correct):
              I assume its got something to do with how the compiler does things. I guess it determines what it does from the code within each frame. My guess is that
              a) when functions are compiled from each frame they are essentially treated the same as variable function assignments but
              b) are compiled to run as variable assignments at the beginning of each original frame's code block in the order that they appear in the frame based actionscript so
              c) they are visible to all code in that frame (and all subsequent code in the same frame on different 'layers' in flash authoring).

              Example code:
              • 4. Re: Method calls on timeline (scope?)
                Some1Won Level 1
                Hm, I guess for now, since your conclusion is similar to mine, (and I have yet to find any thing countering it) I will assume that this is how flash processes script on frames. If anyone else wants to correct/add on, they are welcome to it.

                Thanks GWD.