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);