2007/04/28 | flash_xml实现分页
类别(flash As) | 评论(0) | 阅读(313) | 发表于 12:43

原文:       http://www.flaspx.com/weblog/blog.php?bid=6
效果预览: http://www.flaspx.com/weblog/tutorial/ajaxflashxml/
其实标题只是一个噱头罢了,只是想谈一下,Javascript 与 Actionscript 是如何操作XML的。
希望能帮助一些只用 Javascript  或 只懂 Actionscript 的朋友,了解两者的相同与不同之处。

Flash 与 后台连接有许多种,Actionscript 调用 XML() 算是比较简单的一种了,
而Javascript 调用 xmlHttp ,便形成了现在很流行的Ajax了。

现在就用一个网上常出现的分页效果来对 Flash 和 Ajax 做个入门学习。 

实际运用中一般是通过后台脚本生成XML文件,再对其产生的数据进行操作
由于篇幅关系在本文中将用1.xml 2.xml 3.xml代替。后台脚本不做说明

首先了解一个XML的结构:

[Copy to clipboard]
CODE:
<data>
  <movie id="1" type="爱情">幸福终点站</movie>
  <movie id="2" type="恐怖">绝命终结站</movie>
  <movie id="3" type="喜剧">恐怖电影</movie>
  …
  ….
</data>

从简单的Flash开始吧

[Copy to clipboard]
CODE:
function setxml(page){
  pageXml = new XML();  //申明XML对象
  pageXml.ignoreWhite = true;  //允许空白
  pageXml.load(page+".xml?rid="+Math.random());  //读取XML文件
  pageXml.onLoad = function(success)
  {
    if (success)
      {
      parseXml(pageXml);  //如果读取成功,分析XML文件
    }
  }
}
function parseXml(pageXml){
  xmlroot = ageXml.firstChild;  //定义XML根目录
  for (i=0;i<xmlroot.childNodes.length;i++)
  {
    attachMovie("tr","tr_"+i,i);  //生成行
    this["tr_"+i]._x = 13;
    this["tr_"+i]._y = 25*i+33;
    this["tr_"+i].no = xmlroot.childNodes[i].attributes.id;  //取得一条记录的ID
    this["tr_"+i].name = xmlroot.childNodes[i].firstChild;  //片名
    this["tr_"+i].type = xmlroot.childNodes[i].attributes.type;  //类型
    page = pageXml.firstChild.attributes.page;  //获取当前页
  }
}
if (!page)  //初始页码为第一页 page=1;
  setxml(page);  //初始第一页内容
presetxmlbtn.onRelease = function()
{
  setxml(page*1-1);  //向前翻页,读取内容
}         
nextbtn.onRelease = function()
{
  setxml(page*1+1);  //向后翻页,读取内容
}

0

评论Comments

日志分类
首页[193]
flash As[107]
有的没的[59]
数码照片[4]
自由世界[19]
blender[4]