在另一个事件函数As3中调用事件函数

时间:2016-01-24 03:54:03

标签: actionscript-3 flash

我在下面的另一个函数中调用函数:

import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;


var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE,loadXML);

var xmlData:XML = new XML();

function loadXML(e:Event = null):void{

    xmlData = new XML(e.target.data);
    //var production:String = xmlData.production.app_id.text();
    trace(xmlData);
    var states:String = xmlData.state.place.text();
    var desc:String = xmlData.state.description.text();
    var image:String = xmlData.state.image.text();
    trace('this is working');   
}
xmlLoader.load(new URLRequest('test.xml'));
obj.addEventListener(MouseEvent.MOUSE_OVER,fl_MouseOverHandler);
function fl_MouseOverHandler(event:MouseEvent):void
{
    loadXML();
}

在这种情况下发生警告:Cannot access a property or method of a null object reference 我该如何解决这个问题?以上提到的是完整的代码。

1 个答案:

答案 0 :(得分:0)

在没有输入的情况下调用你的函数时,e.target为null,这是例外,你最常使用“if”。

您的解决方案是:

import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;


var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE,loadXML);

var xmlData:XML = new XML();

function loadXML(e:Event = null):void
{

    if (e!=null)
    {
        xmlData = new XML(e.target.data);
    }
    //var production:String = xmlData.production.app_id.text();
    trace(xmlData);
    var states:String = xmlData.state.place.text();
    var desc:String = xmlData.state.description.text();
    var image:String = xmlData.state.image.text();
    trace('this is working');
}
xmlLoader.load(new URLRequest('test.xml'));
obj.addEventListener(MouseEvent.MOUSE_OVER,fl_MouseOverHandler);
function fl_MouseOverHandler(event:MouseEvent):void
{
    loadXML();
}

祝你好运