flash和xml实现二级菜单效果 [收藏本页]
  • 作者:kinglong 来源:kinglong 发布时间:2006-5-14 13:13:38
  • P.S.
    这个二级菜单程序已经很久之前写得了,这几天有一个网友再一次问起来,所以想一想还是把这个程序也贴出来吧,希望对大家有点帮助!

    程序核心代码:
    //===主场景代码 ===
    stop();
    menu_xml_count = 0;
    menu_y = 30;
    id = 0;
    //读取menu.xml文件;
    menu_xml = new XML();
    menu_xml.ignoreWhite = true;
    menu_xml.onLoad = function(sc) {
    if (sc) {
    _root.init(this.firstChild);
    }
    };
    menu_xml.load("menu.xml");
    function init(nd) {
    menu_xml_count = nd.childNodes.length;
    setMenu(nd.firstChild);
    }
    function setMenu(node) {
    if (node != null) {
    var initObj = new Object();
    initObj._x = 0;
    if (menu_obj != undefined) {
    initObj._y = menu_obj._y+menu_obj._height;
    } else {
    initObj._y = menu_y;
    }
    initObj.id = id;
    initObj.node = node;
    initObj.label = node.attributes.label;
    initObj.target = node.attributes.target;
    initObj.goto = node.attributes.goto;
    menu_obj = _root.attachMovie("menu_mc", "menu"+id+"_mc", id+100, initObj);
    id = id+1;
    setMenu(node.nextSibling);
    } else {
    id = 0;
    fscommand("setHeight", menu_obj._y+menu_obj._height);
    play();
    }
    }
    function moveMenu(menu_id) {
    delete old_obj;
    for (i=0; i<menu_xml_count; i++) {
    var move_obj = eval("menu"+i+"_mc");
    if (menu_id == move_obj.id) {
    if (move_obj.opened) {
    move_obj.close_item();
    } else {
    move_obj.open_item();
    }
    } else {
    move_obj.close_item();
    }
    if (old_obj == undefined) {
    move_obj._y = menu_y;
    } else {
    move_obj._y = old_obj._y+old_obj._height;
    }
    old_obj = move_obj;
    }
    fscommand("setHeight", move_obj._y+move_obj._height);
    } //==== 一级菜单MC里代码====
    //初始化;
    opened = false;
    expand = false;
    sub_id = 0;
    label_en_txt.text = "Menu";
    label_cn_txt.text = "菜单名称";
    //设置label;
    if (label != undefined) {
    var label_arr = this.label.split("|");
    label_cn_txt.text = label_arr[0];
    label_en_txt.text = label_arr[1];
    }
    //===========;
    //设置expand;
    if (node.childNodes.length>0) {
    expand = true;
    }
    //===========;
    //设置target;
    if (this.target == undefined || this.target == "") {
    this.target = "_self";
    }
    if (!this.expand) {
    arrow_mc._visible = false;
    }
    //按钮事件;
    hit_btn.onPress = function() {
    menu_up();
    };
    hit_btn.onRollOver = function() {
    menu_over();
    };
    hit_btn.onRollOut = function() {
    menu_out();
    };
    hit_btn.onRelease = function() {
    menu_out();
    if (expand) {
    _root.moveMenu(this._parent.id);
    } else {
    gotoPage();
    }
    };
    hit_btn.onReleaseOutside = function() {
    menu_out();
    };
    function menu_over() {
    menu_mc.gotoAndPlay("over");
    }
    function menu_out() {
    menu_mc.gotoAndPlay("out");
    }
    function menu_up() {
    menu_mc.gotoAndStop("up");
    }
    function gotoPage() {
    if (this.goto != undefined && this.goto != "") {
    getURL(this.goto, this.target);
    }
    }
    function open_item() {
    this.createEmptyMovieClip("item_list_mc", 10);
    item_list_mc._y = 25;
    setMenu(node.firstChild);
    arrow_mc._rotation = 90;
    opened = true;
    }
    function close_item() {
    item_list_mc.removeMovieClip();
    arrow_mc._rotation = 0;
    opened = false;
    }
    function setMenu(nd) {
    if (nd != null) {
    var initObj = new Object();
    initObj._x = 0;
    if (item_obj != undefined) {
    initObj._y = item_obj._y+item_obj._height;
    } else {
    initObj._y = 0;
    }
    initObj.label = nd.attributes.label;
    initObj.target = nd.attributes.target;
    initObj.goto = nd.attributes.goto;
    item_obj = item_list_mc.attachMovie("item_mc", "item"+sub_id+"_mc", sub_id+100, initObj);
    var sub_obj = new Object();
    sub_obj._x = 0;
    sub_obj._y = 18;
    if (sub_id+1>=node.childNodes.length) {
    item_obj.attachMovie("line_menu_mc", "line_mc", 10, sub_obj);
    } else {
    item_obj.attachMovie("line_item_mc", "line_mc", 10, sub_obj);
    }
    sub_id = sub_id+1;
    setMenu(nd.nextSibling);
    } else {
    sub_id = 0;
    }
    }


    //=====二级菜单MC里的代码====
    //初始化;
    label_txt.text = "菜单名称";
    if (this.label != undefined) {
    label_txt.text = this.label;
    }
    color_out();
    //设置target;
    if (this.target == undefined || this.target == "") {
    this.target = "_self";
    }
    //按钮事件;
    hit_btn.onPress = function() {
    menu_up();
    color_over();
    };
    hit_btn.onRollOver = function() {
    menu_over();
    color_over();
    };
    hit_btn.onRollOut = function() {
    menu_out();
    color_out();
    };
    hit_btn.onRelease = function() {
    menu_out();
    color_out();
    gotoPage();
    };
    hit_btn.onReleaseOutside = function() {
    menu_out();
    color_out();
    };
    function menu_over() {
    item_mc.gotoAndPlay("over");
    }
    function menu_out() {
    item_mc.gotoAndPlay("out");
    }
    function menu_up() {
    item_mc.gotoAndStop("up");
    }
    function gotoPage() {
    if (this.goto != undefined && this.goto != "") {
    getURL(this.goto, this.target);
    }
    }
    function color_over() {
    label_txt.textColor = 0x000000;
    }
    function color_out() {
    label_txt.textColor = 0xffffff;
    }

  • [] [返回上一页] [打 印] [收 藏]
  • 上一篇教程:FLASH AS 2.0 中prototype“原型”的用法和影片翻转 下一篇教程:最常用的Flash语句
Copyright © 2003-2008 站长助手 www.web162.com
辽ICP备05001760号 网络实名:站长助手 转载本站原创教程请注名来源于本站
力倡站长资源文化,崇尚互联共享,做中国最好站长网站,为中国网站提供动力!
始建于2003-3-1 8:10 中国首都·北京 辽宁(分站)
主资源渠道辽宁电信分公司服务器组及带宽!