Customizing the Dropdown Menu

Now, let's proceed to customize the dropdown menu by giving all the movie clip buttons in it some labels, and giving them some actions to perform when they are clicked.

You should be back at the main timeline (Scene 1). Click on the menu instance, and give it an instance name in the properties panel. For our present purposes, just name it "menu." Now click on frame 1 of the actions layer and press F9 to get the actions panel.

Let's begin by giving the heading inside the menu a label. Remember that each MCB instance has a dynamic textfield inside it with an instance name of "myText." So enter the following line of code into the actions panel:

menu.heading.myText.text = "Websites";

Test the movie. You should see the text "Websites" appear on the heading movie clip inside the menu. Next, let's program the text label of the first button of the nested "panel" MovieClip:

menu.panel.b1.myText.text = "Amazon";

Test the movie again. Roll over the menu so that the panel drops down. You will see that this time, no text appears on the first button, and there are no error messages! What could be the problem? Well, it so happens that any time you want to animate text, and any time you want text to appear from behind a mask, you have to embed the text. Here's how to do that:

Double click the MCB symbol in the library. Unlock the text layer. Click on the dynamic textbox "myText" on the stage. In the properties panel, click the Embed button. In the Character Embedding dialog box, choose Uppercase, Lowercase, Numerals, and Punctuation. You can do this by holding down the CTRL key as you click on each choice. When you have selected those four, click OK. Now all those characters will be embedded.

While still in the properties panel, click the "B" button to make the font bold. Lock the text layer again. Now go back to Scene 1. Test the movie. Roll your mouse over the menu. Now the text will show up on the Amazon button, because the font is now embedded. Back in the actions panel, frame 1 of the main timeline, let's proceed to set the labels for the other buttons, too. All the actions so far should look like this:

menu.heading.myText.text = "Websites";
 
menu.panel.b1.myText.text = "Amazon";
menu.panel.b2.myText.text = "Google";
menu.panel.b3.myText.text = "Yahoo";
menu.panel.b4.myText.text = "Actionscript.org";

Next we will make the nested buttons do something when they receive a click. We could add event listeners to each button. But we can also add one event listener to the whole menu, which, because of the event flow, will also trigger the mouse events coming from the children:

menu1.addEventListener(MouseEvent.CLICK, clickHandler);

It only remains to write the clickHandler function:

function clickHandler(event:MouseEvent):void {
	if(event.target == menu.panel.b1) {
		navigateToURL(new URLRequest("http://www.amazon.com"));
	}
	if(event.target == menu.panel.b2) {
		navigateToURL(new URLRequest("http://www.google.com"));
	}
	if(event.target == menu.panel.b3) {
		navigateToURL(new URLRequest("http://www.yahoo.com"));
	}
	if(event.target == menu.panel.b4) {
		navigateToURL(new URLRequest("http://www.actionscript.org"));
	}
}

Ordinarily, using event.target to identify which button was clicked on might cause a problem. The problem is that the nested textfield inside each button could potentially trigger mouse events and be the event target instead of the button itself. But in this case, we've previously already built a movie clip button with a built-in instruction to set mouseChildren to false. So every instance of our custom movie clip button is protected against anything nested inside from receiving mouse events.

Test the movie. The menu should be fully functional now, and each button will open the corresponding web page in your default browser. Here's the download link for the finished file:

download mcb_dropdown_menu_tween_class.fla

Thank You!!!

Thanks you so, so, so much for this. You made it easy and this was the best tutorial for buttons I have found so far. I will also credit your name and .fla document in my records!

Help changing color of menu text

Is there a way to make the dropdown menu text a different color?

Jody Hall's picture

Changing text color

loud068,

Yes, that's easy. Just edit the MCB symbol in the library (double click it to go into edit mode). Next, make sure the nested textbox is selected. Then, in the properties panel, use the color box to choose a different color.

How invert Text color in the different states of the button?

Hi, great tutorial, thank you!

Well everything works perfectly, but I don't know how and if it's possible to invert or switch the color of text in the different states in my button.
Let me explain you:I need an up state with black background graphic (image) and white text, whereas an over state with white background and black text and then switch again in the down state...
Does anybody know how I could define the istance "myText" changing its color during the different states of button?

Thanks for reply

Jody Hall's picture

Here's how

Sigma,

Thank you for your comment!

Double click the MCB symbol to go into its edit mode. On frame 1, in the actions layer, add this line:

myText.textColor = 0xFFFFFF;

Next, click on the actions layer at frame 10 and press the F6 key to add a new keyframe. In the actions panel, when you have that frame selected, type in this line of code:

myText.textColor = 0x000000;

Do the same thing at frame 20: press F6 to add a new keyframe, then in the actions panel, type in this line of code:

myText.textColor = 0xFFFFFF;

Now you have changed the basic button, so that when it is in the up state, it has black text. When it goes to the over state, the text becomes white. In the down state, the text becomes black again. Making these couple of simple changes to the basic MCB button causes every instance of it to change, too, so now the whole menu obeys this new scheme. How to change the colors of the backgrounds ought to be obvious, just edit the backgrounds layer and make the rectangle shapes there any color you want.

Thanks

Well done Jody, thank you very much. Now the button works perfectly.

I get only an error when I try to change the script with "gotoAndStop(FRAME#,"Scene#")" instead of your " navigateToURL", the error is:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main_fla::MainTimeline/clickHandler()

I realize I can't find the solution on my own, help me please ;-)

Another time thank you!

Jody Hall's picture

Send me your file

Sigma,

Send me an email, and attach your file. I will fix the null object error for you and send it back.

Jody
mazoonist at gmail dot com

Drop down menu

Hi!

Another big thank you from Oslo, Norway!

This drop down menu is exactly what I´ve been looking for. Thank you a thousand times! If you have an account, I will be happy to transfer some money for this too. It helped me out so much!

7 menu dropdown

This is a great help for my project. I wanted to make seven menu options for mine. So I just added more where we were able to and the code for everything, but now only half of my menu is visible and it's seperated from my main MCB. What would cause that and how do I fix it.

Thank you for you efforts and help.

Eric

Jody Hall's picture

Send me your file

Eric,

Thanks for the comment. Send me your file and I will check it out to see what might be going on. My email is mazoonist at gmail dot com.

Thanks,

Jody