程序核心代码:
//===主场景代码 ===
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;
}