6 Replies Latest reply: Nov 30, 2010 3:04 PM by kglad RSS

    Loaded SWF runs code on parent timeline

    goddogsrunning

      Any help GREATly appreciated!

       

      I have a parent movie that contains several buttons that load several child SWF files.

      The parent timeline has only 1 frame, and in that frame all the ActionScript (version 3) for the parent movie is imported with a single statement:

       

      include "script.as";

       

      The problem is that when a child movie loads, IF the child contains ANY ActionScript calls, it also somehow runs the code in "script.as", which accesses many elements of the parent movie which are not present in the child. This produces "NULL object reference" errors which crash the script:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.

       

      If I remove all ActionScript calls from the child's timeline, then the child doesn't run the parent's code - But if place so much as a stop(); call in the child timeline, then the child runs the parent's code after being loaded in.

       

      I originally wrote this script for another project, which works fine, and then copied and pasted it to this one, so I don't understand why it's not working the same way. Earlier on I suspected the trouble might be from re-saving my current project as ActionScript 3, when before this upgrade was saved as ActionScript 2. So I went back and re-created each file in fresh AS3 FLAs, but it didn't solve the problem.

       

      The child SWFs are loaded with this code from "script.as".


          var ldr:Loader = new Loader();
          addChild(ldr);
          var url:String = "child.swf";
          var urlReq:URLRequest = new URLRequest(url);
          ldr.load(urlReq);

       

      Also in "script.as" is a simple trace call at the top of the file:

       

      trace('script.as is running');

       

      When de-bugging the file, the message 'script.as is running' appears in the output panel when each child SWF is loaded, which tells me that the child files are running the parent's code. Is there any way to prevent this from happening?

       

      Thanks much.

        • 1. Re: Loaded SWF runs code on parent timeline
          kglad CommunityMVP

          that's not default behavior.  your child swf must be including that as file, too.

          • 2. Re: Loaded SWF runs code on parent timeline
            kglad CommunityMVP

            actually, if your child swf included that file, you would get a loop of repeated loads.  use trace(this) to see what you get and assign different document classes to your two swfs.

             

            copy and paste the trace output.

            • 3. Re: Loaded SWF runs code on parent timeline
              goddogsrunning Community Member

              kglad:

               

              That's just the problem - the child SWF does not include anything. Yet, it somehow runs that code anyway.

               

              The only ActionScript in the child.swf is a single stop(); command in the first frame. If I remove that, then the child.swf doesn't run the parent code when it's pulled in. Doesn't make any sense...

              • 4. Re: Loaded SWF runs code on parent timeline
                kglad CommunityMVP

                change the name of your fla and swf files to something other than Untitled-x and retest.  if that fails:

                 

                use trace(this) to see what you get and assign different document classes to your two swfs.

                 

                copy and paste the trace output.

                • 5. Re: Loaded SWF runs code on parent timeline
                  goddogsrunning Community Member

                  Yes, it does load the code repeatedly.

                   

                  The code is like:

                  loaderInfo.addEventListener(Event.COMPLETE, initMain,false,0,true);
                  function initMain(e:Event):void {
                       loaderInfo.removeEventListener(Event.COMPLETE, initMain);
                       trace('initial load complete: '+ unescape(loaderInfo.url.replace(/^.+\/([^\/]+)$/, "$1")));
                  }
                  

                   

                  And the resulting output, after the child.swf is loaded:

                  script.as is running
                  initial load complete: parent.swf
                  (loading clip: child)
                  script.as is running
                  initial load complete: child.swf
                  

                   

                  The stop() command in the child fails because of the errors that script.as is throwing by trying to access movie clips that don't exist on the child timeline, and so the child.swf plays in a continuous loop. Each time it plays, script.as is loaded and run again.

                   

                  Question: how do you apply different document classes?

                  • 6. Re: Loaded SWF runs code on parent timeline
                    kglad CommunityMVP

                    comment-out all the code that leads to errors so you just execute the code shown in this forum.   then copy and paste the output.