0
votes

I've tried many different ways to make this work, but I don't know what's going on. Wish Flash gave more specific error information.

I have a loader SWF that loads and starts another (child) SWF. Everything works perfect until I try adding components to the loader (parent) SWF. After adding components: When the child SWF is loaded and executed, many display objects fail to load and I receive the following error multiple times:

TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChildAt()
    at fl.controls::BaseButton/drawBackground()
    at fl.controls::LabelButton/draw()
    at fl.core::UIComponent/drawNow()
    at fl.controls::List/drawList()
    at fl.controls::List/draw()
    at fl.core::UIComponent/callLaterDispatcher()

I think this button does not get completely destroyed, and interferes with button code in the child swf.

I have the following code:

btnStart.addEventListener(MouseEvent.CLICK, start);

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onGameLoadCompletion);


function onGameLoadCompletion (event:Event):void {
    loader.contentLoaderInfo.removeEventListener(Event.INIT,  onGameLoadCompletion);
    stage.addChild(loader.content);
    stage.removeChild(this);
}

function start (event:MouseEvent):void {
    btnStart.removeEventListener(MouseEvent.CLICK, start);
    loader.load(new URLRequest("childSWF.swf"));
}

There is a button on stage, that was made by dragging the button component into my library, and then onto stage.

I've also tried doing this with only AS code as follows:

import fl.controls.Button;

var btnStart:Button = new Button();
stage.addChild(btnStart);
btnStart.x=670.30;
btnStart.y=486.35;
btnStart.setSize(100,22);
btnStart.label="Continue";
btnStart.visible = true;

btnStart.addEventListener(MouseEvent.CLICK, start);

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onGameLoadCompletion);


function onGameLoadCompletion (event:Event):void {
    loader.contentLoaderInfo.removeEventListener(Event.INIT,  onGameLoadCompletion);
    stage.removeChild(btnStart);
    stage.addChild(loader.content);
    stage.removeChild(this);
}

function start (event:MouseEvent):void {
    btnStart.removeEventListener(MouseEvent.CLICK, start);
    loader.load(new URLRequest("childSWF.swf"));
}

Where there is a button in my library but not on stage.

What could be causing this error? I think maybe it is because the child SWF has buttons in it as well. So, how do I properly delete the button in the parent SWF before the child SWF is loaded so that it doesn't affect the child SWF?

This question is from a stand point of figuring out what is different in the child SWF's environment when there used to be a component on stage before it was loaded. Let's pretend I can't modify the child swf. Why does it work perfectly fine before I add components to the parent swf?

Thanks for the help.

2
I suppose problem not in code you're showing. Firstly, error says problem in function addChildAt() - but you do addChild. Look at exact line number where error occurs. Secondly, try to trace() loader.content - is it OK or not? - Dmitry Malugin
the problem is it doesn't show line numbers where the error occured! :( What I put in the question is all the information I have. I'd rather not mess with the child.swf as it's the change of content in the parent swf that causes this. But I'll consider changing the contents of the child.swf if nothing can be done. - CausingUnderflowsEverywhere

2 Answers

0
votes

You try to remove this (the classinstance which holds your loader). What do you want to remove after loading. Notice that in AS3 in opposite to AS2 and js, this means the class her self - not the event-target.

0
votes

OK. You never need to add the loader itself. Only its content. But you have to cast it and write it to a local var to handle it later. After that you can use the Loader.unloadAndStop() -method (AFAIK since Flashplayer 11[?]). And everything should be fine. Try this:

import fl.controls.Button;
import flash.display.MovieClip;

var swfContent:MovieClip;
initButton();

function initButton():void
{
    //you don't need to create your button as a class-var 
    var btnStart:Button = new Button(); 
    btnStart.x = 670.30;
    btnStart.y = 486.35;
    btnStart.setSize(100, 22);
    btnStart.label = "Continue";
    btnStart.visible = true;    
    btnStart.addEventListener(MouseEvent.CLICK, start);
    addChild(btnStart);
}

function start(event:MouseEvent):void
{
    var btnStart:Button = event.target as Button;   
    btnStart.removeEventListener(MouseEvent.CLICK, start);
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onGameLoadCompletion);
    loader.load(new URLRequest("childSWF.swf"));
}

function onGameLoadCompletion(event:Event):void
{
    var loader:Loader = event.target as Loader;
    loader.contentLoaderInfo.removeEventListener(Event.INIT, onGameLoadCompletion);
    swfContent = loader.content as MovieClip;
    if (!swfContent)
    {
        trace("no valid SWF found");
        return;
    };
    addChild(swfContent);

    loader.unloadAndStop();
    //do things with your swf, e.g.
    //swfContent.play();   
}

I've coded it only hier in the SO-Editor. Maybe there are little typo-mistakes... I hope, it helps! Greetings André