ActionScript 3.0 学习笔记二
这篇文章只要记一下ActionScript 3.0 和 网站之间的交互,方法很简单,就是借助 xml 来实现。
test.aspx code :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="loginuserinfo.aspx.cs" Inherits="loginuserinfo" %>
test.aspx.cs code
using System.Xml;
string id = "12345678";
string lastName = "st.Cheng";
string headerurl = "01.jpg";
XmlDocument xmldoc = new XmlDocument();
string xml = "<?xml version=/"1.0/" encoding=/"utf-8/"?><loginuser></loginuser>";
xmldoc.LoadXml(xml);
XmlElement xmlelem = xmldoc.CreateElement("user");
XmlElement chielem = xmldoc.CreateElement("id");
chielem.InnerText = id;
xmlelem.AppendChild(chielem);
chielem = xmldoc.CreateElement("lastname");
chielem.InnerText = lastName;
xmlelem.AppendChild(chielem);
chielem = xmldoc.CreateElement("headerurl");
chielem.InnerText = headerurl;
xmlelem.AppendChild(chielem);
xmldoc.DocumentElement.AppendChild(xmlelem);
Response.Write(xmldoc.InnerXml);
Flash 的舞台上画一个动态文本框,命名为str_txt,然后选第一帧,按F9,写AS代码如下
System.useCodePage=true;
var paramList = LoaderInfo(this.root.loaderInfo).parameters;
var url:String="http://localhost/test/test.aspx";
var myRequest:URLRequest=new URLRequest(url);
var myXML:XMLDocument=new XMLDocument();
var myLoader:URLLoader=new URLLoader();
myLoader.dataFormat = URLLoaderDataFormat.BINARY;
myLoader.load(myRequest);
myLoader.addEventListener(Event.COMPLETE,getCon);
function getCon(me:Event) {
var bytes:ByteArray = ByteArray(myLoader.data);
var xmlStr:String=bytes.readMultiByte(bytes.length,"utf-8");
myXML.parseXML(xmlStr);
var lastName:String=myXML.firstChild.firstChild.firstChild.nextSibling.firstChild.nodeValue;
var hImg:String=myXML.firstChild.firstChild.firstChild.nextSibling.nextSibling.firstChild.nodeValue;
myLoader.close();
str_txt.text=lastName+","+hImg;
}
这也是AS和数据库交互的一种方式,还有其他方式,但是我没有深究,就暂时用这种方式和数据库交互了。