加载外部文件
//加载XML var myXML:XML = new XML(); var XML_URL:String = "xx.xml"; var myXMLURL:URLRequest = new URLRequest(XML_URL); var myLoader:URLLoader = new URLLoader(myXMLURL); myLoader.addEventListener(Event.COMPLETE, xmlLoaded); function xmlLoaded(evtObj:Event) { myXML = XML(myLoader.data); trace("数据装载完成."); trace(myXML); }
import flash.display.Bitmap; //加载外部图片 var myImg:URLRequest = new URLRequest("xx.jpg"); var loader:Loader = new Loader ; loader.load(myImg); //loader.load(new URLRequest("xx.jpg")); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadComplete); function loadComplete(e:Event):void { var a:Bitmap = loader.content as Bitmap; trace("图片的宽度---"+a.width); loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadComplete); addChild(loader); trace("加载完成"); }
/*加载外部swf文件*/ var loader:Loader = new Loader(); var mySwf:URLRequest = new URLRequest("a.swf"); loader.load(mySwf); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError); function onProgress(e:ProgressEvent):void { trace("已经下载"+Math.round((e.bytesLoaded)/(e.bytesTotal)*100) + "%"); } function onComplete(e:Event):void { addChild(loader); trace("加载完成"); } function onError(e:IOErrorEvent):void { trace("加载出错"); }
/*处理xml abc.xml <info> <user id="1"> <name>jimluck</name> <email>luckstarzb@173.com</email> <age>33</age> </user> <user id="2"> <name>john</name> <email>johnluck@sohu.com</email> <age>21</age> </user> <user id="3"> <name>lucy</name> <email>lucy@163.com</email> <age>18</age> </user> </info> */ var myXml:XML = new XML(); var XML_URL:String = "abc.xml"; var myXMLURL:URLRequest = new URLRequest(XML_URL); //如果省略该参数,则不开始加载操作。 如果已指定参数,则立即开始加载操作 var myLoader:URLLoader = new URLLoader(myXMLURL); myLoader.addEventListener(Event.COMPLETE, xmlLoaded); function xmlLoaded(evtObj:Event) { myXml =new XML(myLoader.data); trace(myXml.user[0].@id);//1 trace(myXml.user[0].name);//jimluck trace(myXml.user[1].name);//john trace(myXml.user[2].name);//lucy //多节点时结果: trace(myXml.user.name); //<name>jimluck</name> //<name>john</name> //<name>lucy</name>
trace(myXml.user.name[0]); //输出id=3的user节点的name值 trace(myXml.user.(@id=="3").name);//lucy //输出name值为john的age节点值 trace(myXml.user.(name=="john").age);//21 } //1. myXml.user[0]还等同于myXml.child(0),如: // trace(myXml.child(0).name); // trace(myXml.child(0).email); // 2. 可以通过for each in来遍历xml文档: /*for each (var property:XML in myXml.user) { var uname:String = String(property.name); var uemail:String = String(property.email); total += 1; trace("用户名"+uname+" 邮件"+uemail) } trace("用户数:", total);*/ /*结果: 用户名jimluck 邮件luckstarzb@173.com 用户名john 邮件johnluck@sohu.com 用户名lucy 邮件lucy@163.com 用户数: 3*/
//加载音乐 var sound = new Sound ; var mySound:URLRequest = new URLRequest("xx.mp3"); sound.load(mySound); sound.addEventListener(Event.COMPLETE,completeHandler); function completeHandler(e:Event):void { trace("音乐加载完成"); sound.play(); }
var myxml:XML = <root> <data attr="attribute1">this is the string1</data> <data attr="attribute2">this is the string2</data> </root>; myxml.data[0]. @ attr1 = "这是新创建的属性"; myxml.data[1] = "第2个节点的值改变了!"; myxml.data[2] = "第3个节点的值改变了!"; delete myxml.data[1]; trace(myxml);