我正在尝试创建一个可以显示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);
}
}
}
}
答案 0 :(得分:0)
不确定这是否能解决您的问题(重构您的代码),但更清楚的是不要在其他函数中包含许多匿名函数。另外,你完全不需要第二次循环。
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);
}