由于接到任务要修改一个FLASH游戏的任务,所以找了些资料,现在把FLASH调用XML数据的代码帖出来,做个学FLASH的留念,也给也需要的朋友做个例子。如果有什么问题请留言给我,谢谢。
这个实例是显示排行榜数据的,分别是序号strNo,名字strName,评价strPrice,分数strScore。首先建立FLASH文件,创建一个MovieClip,命名game,在game里面建立四个动态文本框,strNo,strName,strPrice,strScore,目的是为了显示XML文件里的四个数据,然后新建一层代码层,代码如下:
System.useCodepage=true; //show chinese
pageXml = new XML(); //create XML object
pageXml.ignoreWhite = true; //ignore space
pageXml.load("gamexml.asp"); //quote XMl file
//function for handle XMl
pageXml.onLoad = function (success){
if (success) {
xmlroot = pageXml.firstChild;
//trace (xmlroot);
for (i=0;i<xmlroot.childNodes.length;i++){
_root.attachMovie("game","game_"+i,i)
_root["game_"+i]._x = 44
_root["game_"+i]._y = 30*i+33
_root["game_"+i].strNo = xmlroot.childNodes[i].attributes.strNo
_root["game_"+i].strName = xmlroot.childNodes[i].attributes.strName
_root["game_"+i].strPrice = xmlroot.childNodes[i].attributes.strPrice
_root["game_"+i].strScore = xmlroot.childNodes[i].firstChild
}
}
}
XML文件如下:
<?xml version='1.0' encoding='big5'?>
<gameroot>
<game strNo='1' strName='ybfqlyq' strPrice='二等兵'>8</game>
<game strNo='4' strName='ybfq' strPrice='實習生'>3</game>
<game strNo='5' strName='ybfqcom' strPrice='猴子'>2</game>
<game strNo='6' strName='ybfqnet' strPrice='猴子'>1</game>
<game strNo='7' strName='flasher' strPrice='猴子'>1</game>
<game strNo='8' strName='phper' strPrice='猴子'>1</game>
</gameroot>
注意的问题:
Macromedia的建议:尽量不用文本节点,而是使用属性来存放数据的值。
可以用tract(xmlroot.childNodes[i].firstChild)这样去测试是否接收到数据的值。
attachMovie的调用必须得在game(MC)里设置链接linkpage,点击F11调出元件库面板,选择game,点击右键选择linkpage,勾选linkpage里的Expore for ActionScript和Export in first frame,然后在identifier中写上game,只有选择了这个linkpage Properties,attachMovie才能够调用。
另附ASP生成XML文件的例子:
<%
Set cnnDB = Server.CreateObject("ADODB.Connection") 'Build connection
Con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("data.mdb") 'Setting connection
cnnDB.Open Con
Set rstObj = Server.CreateObject("ADODB.Recordset")
strSQL = "Select * FROM data order by total desc"
rstObj.Open strSQL, cnnDB,1,1
for i=1 to rstObj.recordcount
record=record &"<game strNo='"&i&"' strName='"&trim(rstObj("strScore"))& "' strPrice='"&trim(rstObj("addo_txt"))&"'>"&trim(rstObj("re_p_total"))&"</game>"
rstObj.MoveNext
next
strtemp="<?xml version='1.0' encoding='big5'?>" & vbcrlf
response.write strtemp&"<root>"&record&"</root>"
rstObj.Close
Set rstObj = Nothing
cnnDB.Close
Set cnnDB = Nothing
%>
发表于 @ 2006年09月26日 10:11:00|评论(loading...)|编辑