当我为登录按钮添加鼠标事件侦听器时,我收到一个空对象错误。 (查看构造函数中的注释)
我正在使用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");
}
}
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
我将回答未来访客的问题。问题的原因我已经注意到了,但我弄清楚的是如何绕过它。我只是将我的代码从文档类粘贴到框架中,它在那里工作得非常好。