0

我正在尝试创建一个可以显示 XML 选项列表的应用程序。然后,当单击一个选项时,从不同的 XML 加载数据以显示他们选择的选项,即 [j]。

我在这里的代码在通过循环后只进入最后一个变量,所以无论我点击哪个选项,它都会进入最后一个选项的数据。它也只删除原始按钮的最后一个子按钮。

有谁知道我怎样才能让它进入正确的页面?

import flash.events.Event;
import flash.text.TextField;

var xmlLoader:URLLoader = new URLLoader();
var xmlReq:URLRequest = new URLRequest("http://praxis2.cit.ie/~patrickhallihan/DynamicContent/autumn/classxml.php");
xmlLoader.load(xmlReq);

xmlLoader.addEventListener(Event.COMPLETE, getClasses);
function getClasses(e:Event):void
    {
        var xmlobject:XML = new XML(e.target.data);
        var classlist:XMLList = xmlobject.classgroup;
        var classArray:Array = new Array();

        for (var i:int = 0; i<classlist.length(); i++)
            {
                var classname:String = xmlobject.classgroup.classname[i];
                var classtxt:TextField = new TextField();

                classtxt.width = 70;
                classtxt.height = 20;
                classtxt.border = true; 
                classtxt.borderColor = 0x000000;
                classtxt.background = true; 
                classtxt.backgroundColor = 0x888888;
                classtxt.textColor = 0xFFFFFF;
                classtxt.text = "" + classname;
                addChild (classtxt);
                classArray[i] = classtxt;
            }

        for (var j:int = 0; j<classlist.length(); j++)
            {
                classArray[j].y = 40*j;
                classArray[j].x = 125;
                classArray[j].addEventListener(MouseEvent.CLICK, gotoSchedule);

                function gotoSchedule(event:MouseEvent):void
                    {
                        removeChild (classtxt);

                        var xmlLoader1:URLLoader = new URLLoader();
                        var xmlReq1:URLRequest = new URLRequest("http://praxis2.cit.ie/~patrickhallihan/DynamicContent/autumn/getschedule.php?id="+ j);
                        xmlLoader1.load(xmlReq1);

                        xmlLoader1.addEventListener(Event.COMPLETE, getExams);
                        function getExams(e:Event):void
                            {
                                trace (xmlLoader1.data);
                                var examtxt:TextField = new TextField();
                                examtxt.width = 260;
                                examtxt.height = 400;
                                examtxt.y = 30;
                                examtxt.x = 30;
                                examtxt.border = true; 
                                examtxt.borderColor = 0x000000;
                                examtxt.background = true; 
                                examtxt.backgroundColor = 0x888888;
                                examtxt.textColor = 0xFFFFFF;
                                examtxt.text = (xmlLoader1.data);
                                addChild (examtxt);
                            }
                    }
            }
    }
4

1 回答 1

0

不确定这是否会解决您的问题(重构您的代码),但在其他函数中不要有一些匿名函数会更干净。另外,您完全不需要第二个 for 循环。

import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;

var classArray:Array = new Array();    

var xmlLoader:URLLoader = new URLLoader();
var xmlReq:URLRequest = new URLRequest("http://praxis2.cit.ie/~patrickhallihan/DynamicContent/autumn/classxml.php");

xmlLoader.addEventListener(Event.COMPLETE, getClasses); //add your listeners BEFORE you call load
xmlLoader.load(xmlReq);

function getClasses(e:Event):void
{
    var xmlobject:XML = new XML(e.target.data);
    var classlist:XMLList = xmlobject.classgroup;

    for (var i:int = 0; i<classlist.length(); i++)
    {
        var classname:String = xmlobject.classgroup.classname[i];
        var classtxt:TextField = new TextField();

        classtxt.width = 70;
        classtxt.height = 20;
        classtxt.border = true; 
        classtxt.borderColor = 0x000000;
        classtxt.background = true; 
        classtxt.backgroundColor = 0x888888;
        classtxt.textColor = 0xFFFFFF;
        classtxt.text = "" + classname;
        addChild (classtxt);
        classArray[i] = classtxt;

        classtxt.y = 40 * i;
        classtxt.x = 125
        classtxt.addEventListener(MouseEvent.CLICK, gotoSchedule);
    }
}    

function getIndexFromTextField(tf:TextField):int {
    for (var i:int = 0; i < classArray.length; i++) {
        if (classArray[i] == tf) {
            return i;
        }
    }
    return -1;
}

function gotoSchedule(event:MouseEvent):void
{
    var tf:TextField = event.currentTarget as TextField;
    removeChild(tf);

    xmlLoader = new URLLoader();
    var xmlReq1:URLRequest = new URLRequest("http://praxis2.cit.ie/~patrickhallihan/DynamicContent/autumn/getschedule.php?id=" + getIndexFromTextField(tf));
    xmlLoader.addEventListener(Event.COMPLETE, getExams);
    xmlLoader.load(xmlReq1);
}

function getExams(e:Event):void
{
    trace(xmlLoader.data);
    var examtxt:TextField = new TextField();
    examtxt.width = 260;
    examtxt.height = 400;
    examtxt.y = 30;
    examtxt.x = 30;
    examtxt.border = true; 
    examtxt.borderColor = 0x000000;
    examtxt.background = true; 
    examtxt.backgroundColor = 0x888888;
    examtxt.textColor = 0xFFFFFF;
    examtxt.text = (xmlLoader.data);
    addChild(examtxt);
}
于 2012-08-22T16:46:51.017 回答