Actionscript 3 TypeError:加载外部SWF文件时出现错误#1009

Actionscript 3 TypeError:加载外部SWF文件时出现错误#1009,actionscript-3,flash,Actionscript 3,Flash,我用AS3.0在Flash Player中制作3D文件查看器 我从Away3D示例文件中找到了AWD查看器。 () 它很好用 我把它加载到我的“主”swf文件中。但这不是工作。它不断向我显示错误 错误信息如下 TypeError: Error #1009: Cannot access a property or method of a null object reference. at AWDViewer/initEngine()[C:\Users\wintec\Desktop\Flas

我用AS3.0在Flash Player中制作3D文件查看器 我从Away3D示例文件中找到了AWD查看器。 ()

它很好用

我把它加载到我的“主”swf文件中。但这不是工作。它不断向我显示错误

错误信息如下

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at AWDViewer/initEngine()[C:\Users\wintec\Desktop\Flash3DViewer\source\AWDViewer.as:74]
    at AWDViewer/init()[C:\Users\wintec\Desktop\Flash3DViewer\source\AWDViewer.as:57]
    at AWDViewer()[C:\Users\wintec\Desktop\Flash3DViewer\source\AWDViewer.as:49]
错误线就是这样

line 74 : stage.scaleMode = StageScaleMode.NO_SCALE;
line 57 : initEngine();
line 49 : init();
我知道错误消息意味着没有该名称的属性。 我查过了,没什么问题。 另外,当我在我的“Main”swf中加载另一个swf文件时,这也很有效。奇怪的

我不明白为什么这个错误不断出现。 请帮帮我

下面的代码来自Main.as
该错误意味着您正在尝试访问尚未实例化的内容。当你不确定什么是空值时,你应该设置一些断点并在调试模式下运行你的应用程序


在你的情况下,这个阶段很可能是空的。DisplayObject的Stage属性设置为将此显示对象添加到Stage时应用程序的Stage实例。但是,此显示对象的所有父对象也应添加到后台。因此,在加载AWDViewer之前,请确保Flash3DViewer的实例具有该阶段。

我根据您通过DropBox提供的代码发现了应用程序的问题。正如我所怀疑的那样,
stage
属性是在对象添加到stage之前被引用的,这就是生成空引用错误的原因

调用
init
函数时,
AWDViewer
类过早地从正在调用的函数之一调用了
stage
属性。我已经更新了
Flash3DViewer.as
AWDViewer.as
文件,正确使用了
ADDED\u TO\u STAGE
事件,这样就不会发生这种情况。我还为代码添加了注释,供您遵循。此外,我还必须修改
AWDViewer
类中的
init
函数,以获取类型为
Event
的参数,从而考虑到当
添加到\u STAGE
事件触发时调用该函数的事实

Flash3DViewer.as:

package
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.Event;

    public class Flash3DViewer extends MovieClip
    {
        private var loader:Loader;

        public function Flash3DViewer():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            loadSWF("AWDViewer.swf");
        }
        private function loadSWF(url:String):void
        {
            var urlRequest:URLRequest = new URLRequest(url);
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
            loader.load(urlRequest);
            addChild(loader);
        }
        private function onLoaded(e:Event):void
        {
            var target:AWDViewer = e.currentTarget.loader.content as AWDViewer;
            trace(target);
            //target.init();   // no longer need to call the init function manually as the AWDViewer calls it when it 'knows' it has been added to the stage.  This line can be deleted.
            addChild(target);
        }
    }
}
    public function AWDViewer()
    {
        /* Used ADDED_TO_STAGE event to know when to trigger the init 
           function call to avoid the null reference errors when accessing 
           the 'stage' property */
        addEventListener(Event.ADDED_TO_STAGE,init);

        // init();  // no longer need to manually call the init function as the ADDED_TO_STAGE event listener will take care of this.  This line can be deleted.
    }

    /**
     * Global initialise function
     */
    public function init(e:Event):void
    {
        initEngine();
        initListeners();

        AssetLibrary.enableParser(AWD2Parser);

        //kickoff asset loading
        var loader:Loader3D = new Loader3D();
        loader.load(new URLRequest("assets/monkey.awd"));

        _view.scene.addChild(loader);
    }
AWDViewer.as:

package
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.Event;

    public class Flash3DViewer extends MovieClip
    {
        private var loader:Loader;

        public function Flash3DViewer():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            loadSWF("AWDViewer.swf");
        }
        private function loadSWF(url:String):void
        {
            var urlRequest:URLRequest = new URLRequest(url);
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
            loader.load(urlRequest);
            addChild(loader);
        }
        private function onLoaded(e:Event):void
        {
            var target:AWDViewer = e.currentTarget.loader.content as AWDViewer;
            trace(target);
            //target.init();   // no longer need to call the init function manually as the AWDViewer calls it when it 'knows' it has been added to the stage.  This line can be deleted.
            addChild(target);
        }
    }
}
    public function AWDViewer()
    {
        /* Used ADDED_TO_STAGE event to know when to trigger the init 
           function call to avoid the null reference errors when accessing 
           the 'stage' property */
        addEventListener(Event.ADDED_TO_STAGE,init);

        // init();  // no longer need to manually call the init function as the ADDED_TO_STAGE event listener will take care of this.  This line can be deleted.
    }

    /**
     * Global initialise function
     */
    public function init(e:Event):void
    {
        initEngine();
        initListeners();

        AssetLibrary.enableParser(AWD2Parser);

        //kickoff asset loading
        var loader:Loader3D = new Loader3D();
        loader.load(new URLRequest("assets/monkey.awd"));

        _view.scene.addChild(loader);
    }
虽然我确实尝试编译上面的代码,但更正后的代码不再生成空引用错误,但由于计算机的不同配置,我的计算机上出现了一些编译器错误。您只需要确保这些编译器错误不会出现在您的计算机上

Warning: Library path "C:\Users\wintec\Desktop\3D_VR\source\libs" does not resolve to a valid directory.
Warning: Library path "$(FlexSDK)/frameworks/libs/flex.swc" does not resolve to a valid file.
如果你还有其他问题,请告诉我


干杯。

我以前见过这个错误,通常是当对象在添加到阶段之前尝试引用
stage
对象时发生。你的意思是我必须更改调用方法顺序吗?如果是的话,
ADDED_TO_STAGE
可以解决这个问题吗?我可以告诉你需要尝试什么,但首先你必须向我展示你用来将Flash3DViewer对象添加到STAGE的代码。这是我的全部Flash3DViewer代码。谢谢这很有帮助。错误消失了!但是不幸的是,它没有显示3d对象:-(我也在“Flash3DViewer”中导入了away3d类。但它不起作用。我也找不到任何错误…你能再帮我一次吗?当然,但因为这是一个单独的问题,你应该创建一篇关于该问题的详细信息的新文章,我可以尝试帮助。我写了一篇新文章。请检查一下好吗?