我想在闪存AS3中添加几行代码。如果这些行在xml中,并且所需的数据是使用加载xml的函数获取的,我将如何能够这样做。 我需要在flash中添加这些行吗?我怎么能这样做?
<?xml version="1.0" encoding="utf-8"?>
<game>
<!-- "Level id" number is not required. Just I added it for fast calculating -->
<!-- Page 1 -->
<Level id="1" star3="6" star2="10">
<Row>2,2,2,2</Row>
<Row>2,2,0,0</Row>
<Row>2,2,2,2</Row>
<Row>1,1,4,4</Row>
<Row>1,1,4,4</Row>
</Level>
<Level id="2" star3="8" star2="12">
<Row>2,2,2,2</Row>
<Row>2,2,2,2</Row>
<Row>0,1,1,0</Row>
<Row>3,3,4,4</Row>
<Row>3,3,4,4</Row>
</Level>
<Level id="3" star3="8" star2="12">
<Row>1,0,1,1</Row>
<Row>1,0,1,1</Row>
<Row>1,1,4,4</Row>
<Row>1,1,4,4</Row>
<Row>1,1,1,1</Row>
</Level>
<Level id="4" star3="8" star2="12">
<Row>3,1,1,0</Row>
<Row>3,1,1,0</Row>
<Row>2,2,2,2</Row>
<Row>3,3,4,4</Row>
<Row>3,3,4,4</Row>
</Level>
.
.
.
etc
加载此xml的功能如下:
//playBtn.visible=false;
loadXML();
//loading data from the xml file.
function loadXML():void {
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE,completeXMLHandler);
//defining request.;
var request:URLRequest = new URLRequest("data.xml");
//try catch any error
try {
loader.load(request);
} catch (error:Error) {
trace('error');
}
}
//loading function
function completeXMLHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
var result:XML = new XML(loader.data);
//defining new xml document
var myXML:XMLDocument=new XMLDocument();
myXML.ignoreWhite = true;
myXML.parseXML(result.toXMLString());
//defining node;
var node:XMLNode = myXML.firstChild;
//defining levelNum from the lenght of the node
totalLevel = int(node.childNodes.length);
//pushing the words and their clues to the array
//creating a temporary array for each level, than pushing temporary array to the main array (dataArray)
for (var i:int=0; i<totalLevel; i++) {
scoreArray.push(9999)
var temp_array:Array=new Array();
var row:int = int(node.childNodes[i].childNodes.length);
starInfoArray.push(node.childNodes[i].attributes.star3+"_"+node.childNodes[i].attributes.star2);
for (var j:int=0; j<row; j++) {
var myData = node.childNodes[i].childNodes[j].firstChild.nodeValue;
temp_array.push(myData);
}
dataArray.push(temp_array);
}
}
如果您需要更多详细信息,请告诉我。
答案 0 :(得分:0)
您可以将xml嵌入到现有的actionscript 3代码中并立即访问它。
这就是我的意思:
var gameXML:XML = <game>
<Level id="1" star3="6" star2="10">
<Row>2,2,2,2</Row>
<Row>2,2,0,0</Row>
<Row>2,2,2,2</Row>
<Row>1,1,4,4</Row>
<Row>1,1,4,4</Row>
</Level>
<Level id="2" star3="8" star2="12">
<Row>2,2,2,2</Row>
<Row>2,2,2,2</Row>
<Row>0,1,1,0</Row>
<Row>3,3,4,4</Row>
<Row>3,3,4,4</Row>
</Level>
<Level id="3" star3="8" star2="12">
<Row>1,0,1,1</Row>
<Row>1,0,1,1</Row>
<Row>1,1,4,4</Row>
<Row>1,1,4,4</Row>
<Row>1,1,1,1</Row>
</Level>
<Level id="4" star3="8" star2="12">
<Row>3,1,1,0</Row>
<Row>3,1,1,0</Row>
<Row>2,2,2,2</Row>
<Row>3,3,4,4</Row>
<Row>3,3,4,4</Row>
</Level>
</game>;
//traverse the game config xml
for each(var level:XML in gameXML.Level.*){//go through each Leve node in the XML
scoreArray.push(9999);
starInfoArray.push(level.@star3+"_"+level.@star2);//access the start3 and start2 attributes
var temp_array:Array = [];
for each(var row:XML in level.Row.*) //go through each Row node in a Level and get the data
temp_array.push(row.text().toXMLString());//you might want to parse these strings at some point and get the int values
dataArray.push(temp_array);
}
请注意,如果您遇到语法错误,可能需要格式化XML标记(目前无法测试代码)