文件管理 · 2024年1月25日

flash遍历xml教程|如何用flash动态文本获取xml数据(内详)

A. 用flash调用xml文件里的图片,每个图片都带有链接,代码怎么写

假设xml格式<xml><pic url="图片地址" linkurl="连接地址"。。。></pic></xml>//图片容器public class PicClass extends Sprite{private var link_url:String;private var pic_url:String;private var loader:Loader;public function PicClass(){loader = new Loader();addChild(loader);}public function init(xml:XML):void{this. pic_url = xml.@url;this.link_ulr = xml.@linkurl;loader.load(this.pic_url);}}//主类public class Main extends Sprite{public function Main():void{var loader:URLLoader = new URLLoader();loader.addEventListener(Event.COMPLETE, onLoadedxml);loader.load(new URLRequest("xml地址"));}private function onLoadedxml(evt:Event):void{var xml:XML = new XML(evt.target.data);for(var i:int = 0;i < xml.pic.length();i ++){var pic:PicClass = new PicClass();pic.init(xml.pic[i]);pic.x = i*(pic.width + 10);addChild(pic);}}}简略介绍:通过主类加载xml,通过xml创建图片,并通过图片容器类初始化内容

B. flash读取xml

//xml转为数组 functionpraseXML(){ for(var_loc1=0;_loc1<myXML.firstChild.childNodes.length;++_loc1){ var_loc2=myXML.firstChild.childNodes[_loc1].attributes; myArray.push({imgURL:_loc2.imgURL}); } //endoffor xpic(a); } //Endofthefunction importflash.external.ExternalInterface; varsmallPicWidth=0; varsmallPicHeight=0; varloadmcoldw=0; varloadmcoldh=0; varbigPicWidth=640; varbigPicHeight=580; varchargewidth=0; varchargeheight=0; varmyXML=newXML(); varmyArray=newArray(); //vara=picid; //varxmlurl=albumid+".xml"; //开始要显示的是数组中的第8张图片 vara=7; i=7; varxmlurl="img.xml"; myXML.ignoreWhite=true; this.createEmptyMovieClip("container",100); myMovieLoader=newMovieClipLoader(); varlistener=newObject(); myXML.onLoad=function(ok){ if(ok){ albumText=myXML.firstChild.attributes.albumText; praseXML(); } //endif }; functionxpic(mm){ picurl=myArray[mm].imgURL; myMovieLoader.addListener(listener); myMovieLoader.loadClip(picurl,picLoader); } //Endofthefunction //显示进度条 listener.onLoadProgress=function(target,loadedBytes,totalBytes){ mystring="正在载入"; x=loadedBytes; y=totalBytes; mystring=100*(x/y); zhede._width=mystring*2; zhede._x=194+mystring*1.070000E+000; mystring="Loading…"+int(mystring)+"%"; addwidth.visible=true; lesswidth.visible=false; }; myMovieLoader.onLoadComplete=function(target){ mystring=""; zhede._width=0; zhede2._width=0; zhede3._width=0; }; myMovieLoader.onLoadError=function(target){ trace(target+"loaderror!"); }; varpicCounter=this.createEmptyMovieClip("picCounter",60000); picCounter.onEnterFrame=function(){ if(picLoader._width>0){ //用ExternalInterface把长和宽传给网页的js, //就在这里,传出的都是第一张图片的大小,后面的图片都没有取得数据 ExternalInterface.call("tishi1('"+picLoader._width+"X"+picLoader._height+"')"); loadmcoldw=picLoader._width; loadmcoldh=picLoader._height; if(picLoader._width>bigPicWidthpicLoader._height>bigPicHeight){ var_loc2=Math.min(bigPicWidth/picLoader._width,bigPicHeight/picLoader._height); picLoader._width=_loc2*picLoader._width; picLoader._height=_loc2*picLoader._height; chargewidth=picLoader._width; chargeheight=picLoader._height; } //endif picLoader._x=(bigPicWidth-picLoader._width)/2; picLoader._y=(bigPicHeight-picLoader._height)/2; picLoader.onPress=function(){ startDrag(this); }; picLoader.onRelease=function(){ stopDrag(); }; deletepicCounter.onEnterFrame; } //endif }; Button.prototype.setTip=function(msg){ _root.createTextField("tipText",1000,_xmouse+10,_ymouse+10,10,10); _root.tipText.text=msg; _root.tipText.autoSize=true; _root.tipText.border=true; _root.tipText.background=true; _root.tipText.selectable=false; if(!_root.onMouseMove){ _root.onMouseMove=function(){ updateAfterEvent(); _root.tipText._x=_xmouse+10; _root.tipText._y=_ymouse; }; _root.tipMouseMove=true; } //endif }; Button.prototype.dropTip=function(){ if(_root.tipMouseMove){ delete_root.onMouseMove; delete_root.tipMouseMove; } //endif _root.tipText.removeTextField(); }; //按钮1 addwidth.onRollOver=function(){ this.setTip("原始图片"); }; addwidth.onRollOut=function(){ this.dropTip(); }; //按钮2 lesswidth.onRollOver=function(){ this.setTip("缩小图片"); }; lesswidth.onRollOut=function(){ this.dropTip(); }; //按钮3 charge.onRollOver=function(){ this.setTip("最适大小"); }; charge.onRollOut=function(){ this.dropTip(); }; downpic.onRollOver=function(){ this.setTip("下载图片"); }; downpic.onRollOut=function(){ this.dropTip(); }; downpic.onPress=function(){ this.setTip("暂时禁止下载图片"); }; //用网页的js控制图片换页, //下一张图片 ExternalInterface.addCallback("onnext",null,nextAlert); //上一张图片 ExternalInterface.addCallback("onprev",null,prevAlert); functionnextAlert(){ if(i<myXML.firstChild.childNodes.length-1){ i++; xpic(i); } } functionprevAlert(){ if(i>0){ i–; xpic(i); } } myXML.load(xmlurl); 网页的js <scripttype="text/javascript"> <!– functionthisMovie(movieName){ if(navigator.appName.indexOf("Microsoft")!=-1){ returnwindow[movieName] }else{ returndocument[movieName] } } functiondonext(){ thisMovie("showpic").onnext(); } functiondoprev(){ thisMovie("showpic").onprev(); } functiontishi1(myts){ document.getElementById("suju1").innerHTML=myts; } functiontishi2(myts){ document.getElementById("suju2").innerHTML=myts; } //–> </script>

C. 如何用flash动态文本获取xml数据(内详)

以下内容在来flash 8中测试通过 新建一个文本自文件,文本内容如下: infor= 继承 1、什么是继承和派生 2、复杂的继承和派生 3、继承和派生如何在C 中实现 4、单一继承1 5、单一继承2 6、公有型、保护型和私有型 另存为data.xml注意,Text=并不是要显示的内容,而是flash读取文件的标识,后面将用到; 新建flash文档,与data.txt保存在同一目录下,在适当层适当帧出放置一个动态文本框,大小视文本内容多少而定,在属性面板中为其分配变量名为“infor”,字体和大小可随意选; 新建一图层,来放置代码: System.useCodepage=true; loadVariables("data.xml",_root);