我正在创建一个琐事游戏是Flash(AS3),有两个不同的类别。玩家可以选择一个类别,或另一个类别或两个类别。在下面的代码中,我试图将两个xml文件合并到一个新的xml文件中,如果该人选择了这两个类别。我收到了“访问myLoader和myLoader2的未定义属性错误,我不知道为什么。
// start loading of questions
public function xmlImport()
{
var myLoader:URLLoader = new URLLoader();
var myLoader2:URLLoader = new URLLoader();
if (so.data.question_set == "BOTH")
{
myLoader.load(new URLRequest("category1.xml"));
myLoader2.load(new URLRequest("category2.xml"));
myLoader.addEventListener(Event.COMPLETE, loadXML2);
function loadXML2()
{
myLoader2.addEventListener(Event.COMPLETE, combineXML);
}
}
if (so.data.question_set == "ONE")
{
myLoader.load(new URLRequest("category1.xml"));
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
if (so.data.question_set == "TWO")
{
myLoader.load(new URLRequest("category2.xml"));
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
}
public function combineXML()
{
var xmlJoin:XML = <trivia></trivia>;
var i:XML;
for each(i in myLoader)
{
xmlJoin.appendChild(i);
}
for each(i in myLoader2)
{
xmlJoin.appendChild(i);
}
trace(xmlJoin);
}
感谢您提供的任何帮助。
富
答案 0 :(得分:0)
在第三个选项中,您可能没有两个加载器,当每个加载器完成时,结果XML将附加到先前定义的空XML值。
一个粗略的例子:
var xml1:XML =
<trivia>
<question>What is the difference between a duck?</question>
<answer>I have no idea</answer>
</trivia>;
var xml2:XML =
<trivia>
<question>2+2</question>
<answer>4</answer>
</trivia>;
var xmlJoin:XML = <sheet></sheet>;
var i:XML;
for each(i in xml1)
{
xmlJoin.appendChild(i);
}
for each(i in xml2)
{
xmlJoin.appendChild(i);
}
trace(xmlJoin);
答案 1 :(得分:0)
您可以简单地使用appendChild()
对象的XML
方法,如下所示:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var xml1:XML = <rootNode>
<parentNode>
<childNode>1</childNode>
</parentNode>
</rootNode>;
var xml2:XML = <rootNode>
<parentNode>
<childNode>2</childNode>
</parentNode>
</rootNode>;
xml1.parentNode.appendChild(xml2.parentNode.childNode);
trace(xml1); // output: <rootNode>
// <parentNode>
// <childNode>1</childNode>
// <childNode>2</childNode>
// </parentNode>
// </rootNode>
}// end function
}// end class
}// end package
[更新]
您还可以将XML
个对象简单地合并到XMLList
对象中,如下所示:
var xmlList:XMLList = XMLList(xml1.toString().concat(xml2.toString())); // XMLList(xml1 + xml2)
trace(xmlList); // output: <rootNode>
// <parentNode>
// <childNode>1</childNode>
// </parentNode>
// </rootNode>
// <rootNode>
// <parentNode>
// <childNode>2</childNode>
// </parentNode>
// </rootNode>
[更新2]
我重写了您的申请(以及您发布的申请部分),以向您展示您可能想要采取的方法:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.SharedObject;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _sharedObject:SharedObject;
private var _urls:Array;
private var _xmls:Vector.<XML>;
private var _xmlsLoaded:int;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_sharedObject = SharedObject.getLocal("questions");
_sharedObject.data.category = Category.BOTH;
_xmls = new Vector.<XML>();
switch(_sharedObject.data.category)
{
case Category.ONE: loadXml("xml/category1.xml"); break;
case Category.TWO: loadXml("xml/category2.xml"); break;
case Category.BOTH: loadXml("xml/category1.xml", "xml/category2.xml"); break;
}// end switch
}// end function
private function loadXml(...urls):void
{
_urls = urls;
for each(var url:String in urls)
{
var urlLoader:URLLoader = new URLLoader(new URLRequest(url));
urlLoader.addEventListener(Event.COMPLETE, onUrlLoaderComplete);
}// end function
}// end function
private function onUrlLoaderComplete(e:Event):void
{
_xmls.push(XML(URLLoader(e.target).data));
if (_urls.length == ++_xmlsLoaded) traceXMLList();
}// end if
private function traceXMLList():void
{
trace(getXMLList(_xmls)); // output: <category1>
// <question>question 1</question>
// <question>question 2</question>
// </category1>
// <category2>
// <question>question 1</question>
// <question>question 2</question>
// </category2>
}// end function
private function getXMLList(_xmls:Vector.<XML>):XMLList
{
var xmlList:XMLList = new XMLList();
for (var i:uint = 0; i < _xmls.length; i++)
xmlList += _xmls[i];
return xmlList;
}// end function
}// end class
}// end package
internal class Category
{
public static const ONE:String = "one";
public static const TWO:String = "two";
public static const BOTH:String = "both";
}// end class