1

当我为登录按钮添加鼠标事件侦听器时,出现空对象错误。(看构造函数中的注释)

我使用的是 Flash CS6,logInbutton 和 screen_log_in 等对象是 .fla 文件中的实例名称。这是 .as 文件。

我得到的错误是:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at actions::indexPage()

我的 AS3 代码:

package actions
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.events.IEventDispatcher;
    import flash.net.URLRequest;
    import flash.display.Loader;
    import fl.motion.MotionEvent;
    import flash.events.MouseEvent;

public class indexPage extends MovieClip
{
    public function indexPage():void
    {
        loadSWF("http://mathlympics.cu.cc/loginsystem.swf");

        //THIS IS THE LINE WHICH IS CAUSING THE ERROR
        //WHEN I COMMENT IT OUT THE ERROR IS GONE
        logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
    }

    var _swfLoader:Loader;
    var _swfContent:MovieClip;

    public function loadSWF(path:String):void 
    {
       var _req:URLRequest = new URLRequest();
       _req.url = path;

       _swfLoader = new Loader();
       setupListeners(_swfLoader.contentLoaderInfo);

       _swfLoader.load(_req);
    }

    function setupListeners(dispatcher:IEventDispatcher):void 
    {
       dispatcher.addEventListener(Event.COMPLETE, addSWF);
    }

    function addSWF(event:Event):void 
    {
       event.target.removeEventListener(Event.COMPLETE, addSWF);
       event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
       _swfContent = event.target.content;
       screen_log_in.addChild(_swfContent);
    }

    function unloadSwf():void
    {
        _swfLoader.unloadAndStop();
        screen_log_in.removeChild(_swfContent);
        _swfContent = null;
    }

    function goToLogIn(e:MouseEvent):void
    {
        unloadSwf();
        screen_log_in.loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
    }

    function goToRegister(e:MouseEvent):void
    {
        unloadSwf();
        screen_log_in.loadSWF("http://mathlympics.cu.cc/register.swf");
    }
    }
}
4

2 回答 2

2

在阶段可用之前,您无法访问阶段。

public function indexPage():void
{
    addEventListener(Event.ADDED_TO_STAGE,init)
}

public function init(e:Event):void
{
    removeEventListener(Event.ADDED_TO_STAGE,init)
    loadSWF("http://mathlympics.cu.cc/loginsystem.swf");

    //THIS IS THE LINE WHICH IS CAUSING THE ERROR
    //WHEN I COMMENT IT OUT THE ERROR IS GONE
    logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
}
于 2013-01-07T22:41:15.373 回答
0

我只是为未来的访客回答我的问题。我注意到问题的原因能够弄清楚,但我想出的是如何解决它。我只是将我的代码从文档类复制到框架中,它在那里工作得非常好。

于 2013-03-01T00:26:53.540 回答