# How to load an External SWF with code?

**URL:** <https://forum.kirupa.com/t/how-to-load-an-external-swf-with-code/264728>\
**Category:** Uncategorized\
**Created:** [June 29, 2008, 6:57pm UTC](https://forum.kirupa.com/t/how-to-load-an-external-swf-with-code/264728 "2008-06-29T18:57:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![level4designs](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/level4designs/32/2659_2.png) [@level4designs](https://forum.kirupa.com/u/level4designs)\
**Post date:** [June 29, 2008, 6:57pm UTC](https://forum.kirupa.com/t/how-to-load-an-external-swf-with-code/264728/1 "2008-06-29T18:57:52Z")

</div>

This is probably fairly easy, it was at least in AS2, but I have 2 FLA’s, one is design.fla, the other is code.fla…

in the design FLA… I load in the code…

```auto
 
 
var maintimeline:MovieClip;
maintimeline = stage.getChildAt(0);
var [url:String](http://www.kirupa.com/forum/String);
var loader:Loader;
url='codemodule.swf';
var request:URLRequest=new URLRequest(url);
loader=new Loader();
initListeners(loader.contentLoaderInfo);
loader.load(request);
function initListeners(dispatcher:IEventDispatcher):void 
 {
  dispatcher.addEventListener(Event.COMPLETE,completed);
  trace("load complete");  
 }
  
function completed(event:Event):void 
 {
  maintimeline.addChild(loader);
 }
   
stop();

```

then in my code.swf, after it gets loaded, I want to initialize the design.fla

```auto
 
var [URL:String = "http://192.168.2.2/version5](http://www.kirupa.com/forum/String)";
trace("initializing from code");
var maintimeline:MovieClip;
maintimeline = stage.getChildAt(0);
var initilalize:initialize = new initialize(maintimeline, URL);

```

problem is I don’t think my code.swf, where I set the maintimeline, is accessing the proper stage? or at least my initialize function starts to throw a bunch of errors because maintimeline is not correct here?

say I have something in my initialize class that wants to change a textbox\_txt on the stage of my design.fla?

I get this error when I run my design.fla and the code.swf is loading

TypeError: Error #1009: Cannot access a property or method of a null object reference.  
at codemodule\_fla::MainTimeline/frame1()

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 30, 2008, 9:22am UTC](https://forum.kirupa.com/t/how-to-load-an-external-swf-with-code/264728/2 "2008-06-30T09:22:41Z")

</div>

i’m not sure if this is your problem, but try to put in first frame of your code fla something like this:  
[AS]stop();

addEventListener(Event.ADDED\_TO\_STAGE, go);

function go(evt:Event):void  
{  
removeEventListener(Event.ADDED\_TO\_STAGE,go);  
this.gotoAndStop (2);  
}[/AS]

I’ve break it to different keyframes, this.gotoAndStop (2); but instead you could just initialize your class there, dunno, but give it a try.

---

<div class="post-metadata">

**Author:** ![level4designs](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/level4designs/32/2659_2.png) [@level4designs](https://forum.kirupa.com/u/level4designs)\
**Post date:** [June 30, 2008, 2:38pm UTC](https://forum.kirupa.com/t/how-to-load-an-external-swf-with-code/264728/3 "2008-06-30T14:38:20Z")

</div>

Worked like a charm… that is all I needed, thank you…
