子菜单无法正常工作的菜单

时间:2012-12-28 10:20:46

标签: actionscript-3 flash

我从AS3开始,一开始,我看到很多教程,但现在看来我正在打墙。

我的项目:

  • 我有一个主菜单(menu),其子菜单(menu_seul)包含三个按钮(btn_checkbtn_datebtn_com)。
  • 点击后,会出现不同的文字。
    我放了三个不同的menu_seul帧(因为1是空的)。

有没有办法简化它,因为我只有一个EventListener调用...
如果没有,为什么我的代码不起作用?

感谢您的帮助!

menu.addEventListener(MouseEvent.ROLL_OVER, menu_on, false, 0, true);
menu.addEventListener(MouseEvent.ROLL_OUT, menu_out, false, 0, true);

btn_com.addEventListener(MouseEvent.ROLL_OVER, btn_com_on, false, 0, true);
btn_com.addEventListener(MouseEvent.ROLL_OUT, btn_com_out, false, 0, true);
btn_date.addEventListener(MouseEvent.ROLL_OVER, btn_date_on, false, 0, true);
btn_date.addEventListener(MouseEvent.ROLL_OUT, btn_date_out, false, 0, true);
btn_check.addEventListener(MouseEvent.ROLL_OVER, btn_check_on, false, 0, true);
btn_check.addEventListener(MouseEvent.ROLL_OUT, btn_check_out, false, 0, true);

function menu_on(event:MouseEvent):void{
menu.gotoAndPlay(2);
}

function menu_out(event:MouseEvent):void{
menu.gotoAndPlay(25);
}

function btn_com_on(event:MouseEvent):void{
menu_seul.gotoAndPlay(2);
}

function btn_com_out(event:MouseEvent):void{
menu.gotoAndPlay(1);
}

function btn_date_on(event:MouseEvent):void{
menu_seul.gotoAndPlay(3);
}

function btn_date_out(event:MouseEvent):void{
menu.gotoAndPlay(1);
}

function btn_check_on(event:MouseEvent):void{
menu_seul.gotoAndPlay(4);
}

function btn_check_out(event:MouseEvent):void{
menu.gotoAndPlay(1);
}

再次感谢!

2 个答案:

答案 0 :(得分:1)

试试这个:

创建一个函数来为按钮操作添加一个函数:

function addAction(target:*, event:String, action:Function, params:Array) {
    target.addEventListener(event, function (event:Event) { action(params); });
}

然后,为每个按钮添加每个事件的操作:

addAction(menu, MouseEvent.ROLL_OVER, menu.gotoAndPlay, [2]);
addAction(menu, MouseEvent.ROLL_OUT, menu.gotoAndPlay, [25]);
addAction(btn_com, MouseEvent.ROLL_OVER, menu_seul.gotoAndPlay, [2]);
addAction(btn_com, MouseEvent.ROLL_OUT, menu.gotoAndPlay, [1]);
...

如果你是什么,你可以创建一个数组来注册所有按钮及其动作:

var buttons:Array = [
    {button:menu, overAction:menu.gotoAndPlay, overActionParams:[2], outAction:menu.gotoAndPlay, outActionParams:[25]},
    {button:btn_com, overAction:menu_seul.gotoAndPlay, overActionParams:[2], outAction:menu.gotoAndPlay, outActionParams:[1]},
    {button:btn_date, overAction:menu_seul.gotoAndPlay, overActionParams:[3], outAction:menu.gotoAndPlay, outActionParams:[1]},
    {button:btn_check, overAction:menu_seul.gotoAndPlay, overActionParams:[4], outAction:menu.gotoAndPlay, outActionParams:[1]}
];

for each (var item:Object in buttons)
{
    addAction(item.button, MouseEvent.ROLL_OVER, item.overAction, item.overActionParams);
    addAction(item.button, MouseEvent.ROLL_OUT, item.outAction, item.outActionParams);
}

这就是全部!!

答案 1 :(得分:0)

我解决了。 实际上,我的代码并没有错,现在就是这样:

menu.addEventListener(MouseEvent.ROLL_OVER, menu_on, false, 0, true);
menu.addEventListener(MouseEvent.ROLL_OUT, menu_out, false, 0, true);

btn_com.addEventListener(MouseEvent.ROLL_OVER, btn_com_on, false, 0, true);
btn_com.addEventListener(MouseEvent.ROLL_OUT, btn_com_out, false, 0, true);
btn_date.addEventListener(MouseEvent.ROLL_OVER, btn_date_on, false, 0, true);
btn_date.addEventListener(MouseEvent.ROLL_OUT, btn_date_out, false, 0, true);
btn_check.addEventListener(MouseEvent.ROLL_OVER, btn_check_on, false, 0, true);
btn_check.addEventListener(MouseEvent.ROLL_OUT, btn_check_out, false, 0, true);

function menu_on(event:MouseEvent):void{
menu.gotoAndPlay(2);
}

function menu_out(event:MouseEvent):void{
menu.gotoAndPlay(25);
}

function btn_com_on(event:MouseEvent):void{
menu_seul.gotoAndPlay(2);
}

function btn_com_out(event:MouseEvent):void{
menu_seul.gotoAndPlay(1);
}

function btn_date_on(event:MouseEvent):void{
menu_seul.gotoAndPlay(3);
}

function btn_date_out(event:MouseEvent):void{
menu_seul.gotoAndPlay(1);
}

function btn_check_on(event:MouseEvent):void{
menu_seul.gotoAndPlay(4);
}

function btn_check_out(event:MouseEvent):void{
menu_seul.gotoAndPlay(1);
}

这只是我的对象,我只是在对象,出现和AS可导出名称之间区别对待它们! 所以,Flash告诉我他们不是常数(Class)!

再次感谢你。