How To Create A Dynamic XML Navigation Menu

in Tutorials | 32 Comments

This tutorial covers how to create a dynamic Flash XML navigation menu using Actionscript and XML. Each button has a roll over and roll out animation. Control the click throughs, button titles and the total number of buttons with the external XML file. Once the Flash file is created you can update the menu just by editing the XML file.

Preview Final Product

Step 1.

Open a new flash file
Save it and name it Navigation Menu
Set the Stage to 120pixels by 250 pixels

Step 2.

Create a new movie clip symbol name it btnWhite
On Layer 1 create a background for the button that is 120pixels by 25 pixels as shown in figure 1


figure 1

Step 3.

Create a new movie clip symbol name it btnRed
On Layer 1 create a highlight for the button that is 120pixels by 25 pixels as shown in figure 2


figure 2

Step 4.

Create a new movie clip symbol name it whiteTxt
On layer 1 create a Dynamic Text box. Give it an instance name of Txt.
Width: 110
Height: 20
Font: Verdana
Font Color: white
Font Size: 12
Justification: Left
Embed the font
See figure 3


figure 3

Step 5.

Create a new movie clip symbol name it blackTxt
On layer 1 create a Dynamic Text box. Give it an instance name of Txt.
Width: 110
Height: 20
Font: Verdana
Font Color: black
Font Size: 12
Justification: Left
Embed the font
See figure 4


figure 4

Step 6.

Create a new movie clip symbol name it mask.
One layer 1 create something similar to figure 5


figure 5

Step 7.

Create a new movie clip symbol name is button.
Next take all the movie clip symbols and place them on there own layer inside the button symbol. See figure 6


figure 6

Step 8.

Convert the mask layer to a mask.
Place the whiteTxt movie clip symbol under the mask.
On the mask layer place a keyframe at frame 15 and move the mask movie clip symbol to the left until the mask cover the whole button.
Copy the Keyframe from frame 1 of the mask layer and paste is at frame 30 of the same layer.
Between frame 1 and 15 of the mask layer create motion tween and also between frame 15 and 30 of the same layer.
On frame 30 of the whiteTxt, btnRed, blackTxt, btnWhite layers insert a frame so that all symbols are visible throughout all 30 frames.
Create a new layer at the top of the rest of the layers and name it actions.
On frame 1 of the actions layer place the Actionscript stop();
Copy the Keyframe on the first frame of the Actionscript layer and paste it at frame 15 and 30.
See figure 7


figure 7

Step 9.

Make sure you’re on the main timeline (Scene1). Name Layer 1 to actions.
Click on the first frame on the actions layer, and open the actions panel and paste in the Actionscript below.
See figure 8


figure 8
//Store Button Position
var yPosition:Number = 0;

//Declare New XML Object
var myXML:XML = new XML();
//Set Flash to ignore the XML file's white space
myXML.ignoreWhite = true;
//Declare new Array to store the links from the XML file
var links:Array = new Array();
//Declare new Array to store the names from the XML file
var names:Array = new Array();

//Set XML onLoad function
myXML.onLoad = function(){
	//Set varible to store the XML childNodes
	//This allows you to get the number of buttons in the XML file.
	//You'll use this tell flash how many times to loop the for loop.
	var linkname:Array = this.firstChild.childNodes;
	//Set a for loop
	for(i=0;i

Step 10.

Open Dreamweaver or a simple text editor such at text edit, or note pad.
Save the file as, links.xml in the same directory as the Navigation Menu flash file.
Paste in this xml code into your file.




   

To change the name of a button edit the text after NAME= and between the single quotes.
To change the URL edit the URL after the LINK= and between the single quotes.
To delete a button just delete the line between the open tag and close tag and the flash file will update automatically.
See figure 9.


figure 9

Conclusion

Once you have the shell of the flash file finished you only have to update the XML file and the flash file will updated dynamically.

Comments

Add a Comment
  1. Create A Dynamic XML Navigation Menu | Flash Speaks Actionscript Says:

    [...] tutorial by Flash Farmer covers how to create a dynamic Flash XML navigation menu using Actionscript 2.0 and [...]

  2. Tutorials | Flash Navigation with Actionscript and XML « Flash Enabled Blog Says:

    [...] Read Tutorial [...]

  3. [ fla:pik ] Blog » Tutorials | Flash Navigation with Actionscript and XML Says:

    [...] Read Tutorial [...]

  4. Tutorials | Flash Navigation with Actionscript and XML | Lemlinh.com Says:

    [...] Read Tutorial [...]

  5. raul Says:

    What about iframes.
    How can i make the link open in an iframe?

  6. motaki Says:

    great tutorial! I was hoping you can answer this question. I changed the ‘getURL’ to gotoAndPlay(); to a designated frame.

    So here’s my issue, If i click on a button once, it’ll go to the frame, but once i’m in that frame if i click the button again, it would jump to a random frame, and on the 3rd click it’ll go where its suppose to.

    is there something quick that can be done to remedy that?

    thank you so much.

  7. John Says:

    I have done this tutorial and copied and pasted the code to exactly but im getting 23 errors.
    Is there an issue with this tutorial?
    Apologies if its me!

  8. admin Says:

    John, Make sure you are publishing as Actionscript 2.0

  9. John Says:

    Ah I was in AS3
    Thanks for the info

  10. Ranjit Says:

    Oaoo what a good script. I love it. Wish more tutorials like this, and thanks a lot.

  11. John Says:

    Also just noticed when I put letters with í ó ú á ect and any of the symbols on number keys and alt !”£$%^& ect they will not appear on the buttons is there a way around this?

  12. brianr Says:

    Love the script. I’m looking to dynamically create sub buttons from xml children. Any ideas?

    John: Try using CDATA tags around your xml with the special characters.

  13. babboor Says:

    I am looking for a tutorial exactly like this one but in Actionscript 3.0
    Anyone knows where I can find one please?

    Babboor

  14. ChristinaSeay Says:

    Is it possible to modify this to make the buttons where they load an image into an empty movie click when one of these buttons is clicked??

  15. Tommcat Says:

    Great script. Searching on the web for some basic reference and landed on this.
    Thanks a ton.

  16. gzy Says:

    nice one… onyone knows how to add scroll panel when there ‘re to many boxes??simple one up and down…

  17. carlos Says:

    I’m trying to get to have 4 buttons in the flash file but keep getting a 5 button. I tried changing the values in .substring(3,5) but keep getting a fifth button can anyone help?

  18. carlos Says:

    Anyone have clue?

  19. harshal Says:

    nice script.it is very useful for me.thanks

  20. piotr Says:

    I would like to do submenu for each button using for this the same button. Could you help me.

  21. kathy Says:

    do you know if the menu can support multilanguage?

  22. John Says:

    thx man It helped much
    there is another trick took from me while till I notice it
    you should select the button symbol in the library and right click-> Properties
    select export for ActionScript

  23. Johan Says:

    Thanx! nice tutorial. helped me with dynamicly creating movieclips en altering them..

    @John: took me a wile too till i figured that out. forgot to mention linkage in the tutorial.

    @raul: if you want to open in certain frames: getURL(links[currentIndex],”framename”);
    if you want to send variables:
    getURL(links[currentIndex],”framename”,”POST”);
    or
    getURL(links[currentIndex],”framename”,”GET”);
    I’m not sure but I think either all or all local variables will be send..

  24. Joel Says:

    Is there any way to make a horizontal nav menu using this method?

  25. dezaasss Says:

    he forgot to mention linkage in the tutorial

    like John said “should select the button symbol in the library and right click-> Properties
    select export for ActionScript”

    and he forgot to mention that instance names were required.
    so in button go to whiteTxt and blackTxt and give them the respective instance name.

    Well that’s obvious, but it should be there.
    Anyway really thnks for the tut. ;) hellped allot

  26. kareem shaik Says:

    Nice Navigation .. Thanks buddy..

  27. Ruwanaru Says:

    very useful thank you…………..

  28. thiago Says:

    Also noticed when I put letters with í ó ú á they will not appear on the buttons is there a way around this?

  29. Toerti Says:

    I am trying to get this to work inside an movieclip. No luck yet.
    any ideas???

  30. Toerti Says:

    Got it working!
    But now I have another problem. I do have an xml with more then 100 entries so as soon as I click the 101th to 109th It links to the 10th. Similar with 110-119 link to 11. 120-129 link to 12. and so on.
    Anyideas???

  31. Johan Says:

    Toerti, you have to embed the symbols! Go to blackTxt en select te dynamic text-box in property’s bar you can click embed. You can simply put those characters you need or select all, just a-z, the numbers, a combination (by ctrl + clicking) and push ok!

    do this for both the dynamic text-boxes. (blackTxt and whiteTxt)

    great succes!

    have not found a solution to your other problem.. you have not altered the for loop:

    var linkname:Array = this.firstChild.childNodes;
    //Set a for loop
    for(i=0;i <linkname.length;i++){

    }

    and the xml has the same structure?

  32. Toerti Says:

    Well the solution to my last prob was to change the start and end value of the substring command. The original one only reads the 2 digits. so by setting the last number one higher solved the prob. (Its not only about reading the code but understanding. LOL

    Thanks anyway.

Leave a Reply

© 2010 Altair Innovative, LLC
Entries RSS Comments RSS Log in