帝国软件 首页 > 网络学院 > Flash教程 > 正文 返回 打印

FLASH+XML构造的下拉菜单

  2008-07-16 21:41:19  

用Tree组件来制作下拉菜单,会比较方便。但用FLASH AS打造下拉菜单会更灵活,而且最重要的是占用空间更小,只有2K多。一般用组件来完成的项目,都会自己开发的东西大一些。主要是使用XML分析。分析并获取XML的节点。转换成FLASH里面的元素。
1、制作显示菜单,以及下拉菜单;
2、编写XML文件;
3、添加AS。

fscommand("allowscale", "false");
//创建一个新的空 XML 对象
var my_xml:XML = new XML();
//取消空白节点输出
my_xml.ignoreWhite = true;
//加载xml
my_xml.load("
http://www.taoshaw.com/taoshaw/study/xml_listxiala/List.xml");
//判断是否加载成功,如果成功,则继续前进
my_xml.onLoad = function(ok:Boolean) {
  if (ok) {
    xml_Length = my_xml.firstChild.childNodes.length;
    //读取xml子节点的长度
    for (i=0; i<xml_Length; i++) {
      _root.attachMovie("mc", "mc"+i, i);
      _root["mc"+i]._y = 100;
      _root["mc"+i]._x = i*70+110;
      _root["mc"+i].txt = my_xml.firstChild.childNodes[i].attributes.nume;
      //读取菜单名称
    }
    //根据子节点来设置菜单
  } else {
    trace("加载XML失败!");
  }
};
_root.onMouseDown = function() {
  for (i=0; i<xml_Length; i++) {
    xml_Length2 = my_xml.firstChild.childNodes[i].childNodes.length;
    if (_root["mc"+i].hitTest(_root._xmouse, _root._ymouse)) {
      if (my_xml.firstChild.childNodes[i].hasChildNodes()) {
        for (j=0; j<xml_Length2; j++) {
          //调用子菜单元件
          _root.attachMovie("xialamc", "xialamc"+i+j, i+j+50);
          //读取子菜单名称
          _root["xialamc"+i+j].subtxt = my_xml.firstChild.childNodes[i].childNodes[j].attributes.subnume;
          //设置MC的默认座标
          _root["xialamc"+i+j]._x = i*70+168;
          _root["xialamc"+i+j]._y = j*30+128;
        }
      }
      //否则删除该菜单下的子菜单 
    } else {
      for (j=0; j<10; j++) {
        removeMovieClip(_root["xialamc"+i+j]);
      }
    }
  }
  //当然K为真时,打开网址
  if (k) {
    getURL(my_urll, _blank);
  }
};
//当鼠标触到菜单时,显示该菜单的子菜单 
_root.onMouseMove = function() {
  k = false;
  //当鼠标触及菜单时,菜单改变颜色 
  for (i=0; i<xml_Length; i++) {
    if (_root["mc"+i].hitTest(_root._xmouse, _root._ymouse)) {
      _root["mc"+i].gotoAndStop(2);
    } else {
      //否则仍旧为默认颜色
      _root["mc"+i].gotoAndStop(1);
    }
    for (j=0; j<10; j++) {
      //鼠标触发子菜单时,改变子菜单的颜色
      if (_root["xialamc"+i+j].hitTest(_root._xmouse, _root._ymouse)) {
        _root["xialamc"+i+j].gotoAndStop(11);
        //读取url值,并赋值给变量“my_urll”
        my_urll = my_xml.firstChild.childNodes[i].childNodes[j].attributes.url;
        //设置k为真
        k = true;
      } else if (_root["xialamc"+i+j]._currentframe != 10 && _root["xialamc"+i+j]._currentframe != 11) {
        _root["xialamc"+i+j].play();
      } else {
        //将xialamc内时间轴停在第10帧
        _root["xialamc"+i+j].gotoAndStop(10);
      }
    }
  }
};



http://www.py001.com/wangluoxueyuan/Flashjiaocheng/2008-07-16/290.html