# Loading a SWF with a custom Document class

**URL:** https://forum.kirupa.com/t/loading-a-swf-with-a-custom-document-class/263357
**Category:** flash
**Created:** [June 15, 2008, 12:26am UTC](https://forum.kirupa.com/t/loading-a-swf-with-a-custom-document-class/263357 "2008-06-15T00:26:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)
#### Post date: [June 15, 2008, 12:26am UTC](https://forum.kirupa.com/t/loading-a-swf-with-a-custom-document-class/263357/1 "2008-06-15T00:26:27Z")

</div>

Okay… now my knowledge of AS3 is beginning to be stretched 🙂

I’ve written my basic game engine that has separate SWF files for each “level”. Now I’m looking at creating a single SWF that will load in each level as required, and provide basic common UI controls such as score, mouse cursor and so forth.

The level SWF has a custom document class, GameStage, that will be common to all the level SWFs and contains all the code and support classes to let the player move about the level etc. The SWF file contains both manually placed MovieClip content as well as dynamic, scripted-only content.

Here’s my code to test just loading the game SWF into the “global” SWF:

```auto

var gameLoader:Loader = new Loader();
gameLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
gameLoader.load(new URLRequest('FirstLevel.swf'));
function completeHandler(evt:Event)
{
 //addChild(gameLoader);
}

```

I’ve commented out the addChild line for the time being, but it seems even the simple act of loading the level SWF throws me a:

```auto
TypeError: Error #1009: Cannot access a property or method of a null object reference.
 at GameStage()

```

However, the level SWF on its own works perfectly - it’s only when loaded by the second SWF that the null reference error occurs.

I took at look at  
[http://www.kirupa.com/forum/showthread.php?p=2123134](http://www.kirupa.com/forum/showthread.php?p=2123134) - is this relevant to the issue?

Unfortunately the error Flash is throwing me is not a very helpful one, akin to “Well, _something_ went wrong” - no, really? 😉

I figure that it’s something I haven’t grasped properly yet, or need to add to make Flash happy. Thoughts? More information needed?

---

<div class="post-metadata">

### Author: ![bonnieraymond](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@bonnieraymond](https://forum.kirupa.com/u/bonnieraymond)
#### Post date: [June 15, 2008, 4:54am UTC](https://forum.kirupa.com/t/loading-a-swf-with-a-custom-document-class/263357/2 "2008-06-15T04:54:38Z")

</div>

you should handle Event.ADDED\_TO\_STAGE in your level swfs.

---

<div class="post-metadata">

### Author: ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)
#### Post date: [June 15, 2008, 5:15am UTC](https://forum.kirupa.com/t/loading-a-swf-with-a-custom-document-class/263357/3 "2008-06-15T05:15:08Z")

</div>

Need more information 🙂

I’m not sure how you mean I should do that - do I add a handler as a new method? If so, where do I add the listener - in my mini loader, or in the level’s document class (GameStage) itself?

And after that, what should the handler _do_?

For example, in an abridged form:

```auto
public class GameStage extends MovieClip
{
    public function GameStage():void
    {
         addEventListener(Event.ADDED_TO_STAGE, stageHandler);
    }
 
    public function stageHandler(evt:Event):void
    {
        trace ('hello');
    }
}

```

…I don’t understand how that solves things?

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [June 15, 2008, 5:19am UTC](https://forum.kirupa.com/t/loading-a-swf-with-a-custom-document-class/263357/4 "2008-06-15T05:19:21Z")

</div>

What he meant is that you need to make sure the code contained in your child SWF’s constructor does not run until the ADDED\_TO\_STAGE event is fired. It’s best to put all code that is currently in the constructor into an “init” function, and then run the init on ADDED\_TO\_STAGE:

```auto

public function MyConstructor()
{
     addEventListener(Event.ADDED_TO_STAGE, init);
}

```

You’re currently probably trying to access the stage before the loader has been added to the stage, which causes problems.

This should really be in a sticky.

---

<div class="post-metadata">

### Author: ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)
#### Post date: [June 15, 2008, 5:35am UTC](https://forum.kirupa.com/t/loading-a-swf-with-a-custom-document-class/263357/5 "2008-06-15T05:35:46Z")

</div>

Aah, right. Now I’m starting to understand 😃

I also found these resources, one from our very own guru Senocular:

> **[ActionScript 3 Tip of the Day - Page 14](https://www.kirupa.com/forum/showthread.php?p=1950401)**
>
> Got a web design/development question or just want to hang out with the most awesome people on the planet? Drop on in!

  
[http://freethemedia.blogspot.com/2007/09/very-important-event-in-as3.html](http://freethemedia.blogspot.com/2007/09/very-important-event-in-as3.html)

I also traced my problem to this in the variable initialisation:

```auto

private var stageWidth:int = stage.stageWidth;
private var stageHeight:int = stage.stageHeight;

```

Man, that’s… really picky of Flash 😉 Seems a bit counterintuitive to have a Constructor method that does nothing but set up a listener for being added, and it’s THAT method that does all the stuff the constructor should otherwise have done 😉

Aah well. Thanks for all your help!
