Loading a SWF with a custom Document class

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

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:


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:

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 - 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? :wink:

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

you should handle Event.ADDED_TO_STAGE in your level swfs.

Need more information :slight_smile:

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:

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?

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:


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.

Aah, right. Now I’m starting to understand :smiley:

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


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

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


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

Man, that’s… really picky of Flash :wink: 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 :wink:

Aah well. Thanks for all your help!