3 Replies Latest reply on Jun 13, 2016 10:41 AM by jimboslyce

    Child functions are undefined on the first frame

    jimboslyce

      I am trying to write a script on the main timeline that looks like this:

       

      // main timeline frame 0
      this.stop();
      console.log(this.Foo.Bar());
      

       

      Which has a error of "Bar is not a function" (it's undefined).

      This is because the this.frame_0() function has not been run on the child object (symbol)

       

      // Foo timeline frame 0
      this.stop();
      this.Bar = function()
      {
           return "Hello World";
      };
      

       

      I have tried moving the main timeline to frame 1, which works

       

      // main timeline frame 1
      this.stop();
      console.log(this.Foo.Bar()); // prints "Hello World!"
      

       

      However, whenever I have a grandchild object, this problem bubbles up the timeline

      For example, this does not work:

       

      // FooBar timeline frame 0, child of Foo
      this.stop();
      this.Bar = function()
      {
           return "Hello World";
      };
      

       

      // Foo timeline frame 0
      this.stop();
      this.Bar = function()
      {
           return "Hello World";
      };
      

       

      // main timeline frame 1
      this.stop();
      console.log(this.Foo.Bar()); // works
      console.log(this.Foo.FooBar.Bar()); // error, Bar() is not a function
      

       

      But, as expected, this DOES work

       

      // FooBar timeline frame 0, child of Foo
      this.stop();
      this.Bar = function()
      {
           return "Hello World";
      };
      

       

      // Foo timeline frame 1
      this.stop();
      this.Bar = function()
      {
           return "Hello World";
      };
      

       

      // main timeline frame 2
      this.stop();
      console.log(this.Foo.Bar()); // works
      console.log(this.Foo.FooBar.Bar()); // works
      
      

       

      And obviously, this does not work

       

      // main timeline frame 1
      this.stop();
      console.log(this.Foo.FooBar.Bar()); // works
      console.log(this.Foo.Bar()); // Error, Bar is not a function
      

       

      My question is: How do I solve the problem of maintaining the ability to call child functions, without suffering from timeline creep?

      I would prefer all of these to be on frame_0 so I need either:

       

      a method where I can place the functions directly on the generated classes so that I don't need to wait for frame functions to be called

      OR

      a method to reverse the order of frame_0 function calls from parent->child to child->parent.

       

      Any help would be greatly appreciated.

        • 1. Re: Child functions are undefined on the first frame
          jimboslyce Level 1

          I have found *a* solution, though it's not the best implementation.

           

          The first step to doing this is on the frame *after* your symbols are placed on the stage (in my case, frame_1), you can do the following:

           

          // FooBar, child of Foo, Frame_0
          this.stop();
          this.Bar = function()
          {
               return "I am FooBar";
          }
          

           

          // Foo, child of main timeline, Frame_0
          this.stop();
          var $this = this; // for the proper scoping of "this"
          this.Bar = function()
          {
               return "I am Foo";
          }
          this.FooBarBar = function()
          {
               return $this.FooBar.Bar();
          }
          

           

          // Main timeline, Frame_1
          this.stop();
          stage.addChild(exportRoot);
          stage.update();
          console.log(exportRoot.Foo.Bar()); // works
          console.log(exportRoot.Foo.FooBar.Bar()); // also works
          console.log(exportRoot.Foo.FooBarBar()); // even this works
          

           

          The only downside to this is that there is a small *flicker* due to Frame_0 having none of my child functions executed, and so their init states aren't set until Frame_1, but other than this it works pretty well.

          • 2. Re: Child functions are undefined on the first frame
            Colin Holgate MVP & Adobe Community Professional

            I've had cases where things need to get set up in the first frame. The easy solution to the problem is to put the setup code into the symbol itself.

            • 3. Re: Child functions are undefined on the first frame
              jimboslyce Level 1

              How do you avoid duplicate symbols in that case? I'd rather not duplicate a ton of function code just for the sake of avoiding a flicker.