Monsieur Bonhomme
Monsieur Bonhomme

Reputation: 31

Menu with Submenu not working

I'm beginning on AS3, very beginning, I saw lot of tutorials, but it seems I am hitting a wall now.

My project:

Is there a way to simplify it as I have just one EventListener calling...
If not, why does my code not work ?

Thanks for your help!

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);
}

Thanks again !

Upvotes: 0

Views: 478

Answers (2)

Monsieur Bonhomme
Monsieur Bonhomme

Reputation: 31

I solved it. Actually, my code wasn't so wrong, this is it now :

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);
}

It was just my objects, I had just to name them differently between object, occurence and AS exportable name ! So, Flash said to me they weren't constant (Class) !

Thank you again.

Upvotes: 0

fenixkim
fenixkim

Reputation: 363

try this:

Create a function to add a function for a button action:

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

Then, add the action for each event for each button:

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]);
...

If you what, you can create an array to register all buttons and its actions:

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);
}

That's all!!

Upvotes: 1

Related Questions