Actionscript 3 - List error when loading another a

2019-03-04 19:11发布

问题:

I am working on a AS3/Flash game and we are running into an issue when we load our home page swf into our login swf after someone successfully logs in.

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.controls::Button/draw()
at fl.core::UIComponent/drawNow()
at fl.controls::List/drawList()
at fl.controls::List/draw()
at fl.core::UIComponent/callLaterDispatcher()

We are developing in Flash Builder, importing a .swc with the artwork and components into our project. We load our homepage swf and add it as a display object like this:

private function LoadComplete(e:Event):void
    {
        //trace("LoadComplete");
        m_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, LoadProgress);
        m_homePage = e.target.content as DisplayObject;
    }

Adding it:

addChild(m_homePage as DisplayObject);

Is there a better way to load a swf into another swf? Why would we be getting errors when loading the homepage swf through our login swf but not when we are debugging the home page separately?

Any advice would be very helpful.

回答1:

If you want to execute external swf inside your own swf, you'll need to use SWFLoader

<mx:SWFLoader
  id="sfwLoader"
  width="100%"
  height="100%"/>

And load code:

protected function loadSWF():void
{
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.BINARY;
    loader.addEventListener(Event.COMPLETE, onSWFLoaded);
    loader.load(new URLRequest("app-storage:/myOther.swf")); // sample location
}


protected function onSWFLoaded(e:Event):void
{
    var loader:URLLoader = URLLoader(e.target);
    loader.removeEventListener(Event.COMPLETE, onSWFLoaded);
    var context:LoaderContext = new LoaderContext();
    context.allowLoadBytesCodeExecution = true;
    context.applicationDomain = ApplicationDomain.currentDomain;
    sfwLoader.loaderContext = context;
    sfwLoader.addEventListener(Event.COMPLETE, loadComplete);
    sfwLoader.load(loader.data);
}

protected function loadComplete(completeEvent:Event):void
{
    var swfApplication:* = completeEvent.target.content;
}

This laods any swf, so it's potential security hole. Regards providing security you can examine: http://mabulous.com/air-applications-that-can-be-updated-without-requiring-admin-rights