当前位置:软件学习 > Flash >>

as 解析xml到flash

这是我公司一个初学as的同事写的,也把它拿出来吧,他用的是x,y定位法,先来看xml文件的模式吧

<item>
<title>321也时尚</title>
<link>/show.php?id=7</link>
<imgurl>pic/web_08.gif</imgurl>
<date>2008-03-26</date>
</item>

就是标题,连接地址,图片地址,日期,然后我们来看as是怎么解析这个xml文件了.


picW = 20
picH = 80
link1 ="link"
var main:MovieClip = this;
var myXML = new XML()
myXML.ignoreWhite = true
myXML.load("piclist.xml")
myXML.onLoad = function(success) {
 if (success) {
  _root.createEmptyMovieClip(loadPic,-1000)
  _root[loadPic]._y =20

  loadPP()
 } else {
  trace("加载不成功")
 }
}
function loadPP() {
 myList = myXML.firstChild.childNodes
 myListLen = myList.length;
 for ( i =1; i<=myListLen;i++){
  var new_mc = _root[LoadPic].createEmptyMovieClip ("new_mc" + i, i+100);
  new_mc._x = 20;
  new_mc._y = (80 * (i-1));
  new_mc.createTextField ("main",0,100,0,500,20);
  new_mc.createTextField ("rere",-1,100,20,500,20);
  new_mc.createTextField ("lianjie",-2,100,40,500,20);
  new_mc.main.html = true;
  new_mc.main.wordWrap = true;
  new_mc.main.text = String (myXML.firstChild.childNodes[i-1].childNodes[0].childNodes);
  new_mc.rere.text = String (myXML.firstChild.childNodes[i-1].childNodes[3].childNodes);
  new_mc.lianjie.text = link1;
  _root[LoadPic].createEmptyMovieClip("pic"+i,i+1)
  _root[LoadPic]["pic"+i]._x = picW
  _root[LoadPic]["pic"+i]._y =(i-1) * picH
  _root[LoadPic]["pic"+i].loadMovie(myXML.firstChild.childNodes[i-1].childNodes[2].childNodes)
  //设置文字样式
  btntf = new TextFormat ();
  btntf.color = 0x000000;
  btntf.font = "verdana"; //字体型号
  btntf.size = 12;   //字体大小
  btntf.bold = true;
  new_mc.lianjie.setTextFormat (btntf);
    //按钮行为
  new_mc.i = i-1;
  new_mc.onRelease = function () {
   var who:Number = this.i;
   var link:String = myXML.firstChild.childNodes[who].childNodes[1].childNodes
   trace (link);
   getURL (link, "_blank");
  };
  new_mc.onRollOver = function () {
   who=this.i
   this._y = newy + (mhj * who);
   menutf = new TextFormat ();
   menutf.color = 0xff9900;
   this.lianjie.setTextFormat (menutf);
  };
  new_mc.onRollOut = function () {
   menutf = new TextFormat ();
   menutf.color = 0x0000;
   this.lianjie.setTextFormat (menutf);
  }
  
  }
}


虽然写得有一些不规范化但有很多地方值得学习的.

 

原创文章:转载请注明www.226511.cn

补充:flash教程,Action
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,