A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Load swf through xml

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    8

    Load swf through xml

    Ok the topic name is not the whole problem but to explain.

    i have made a menu movieclip that duplicates itself as much as needed.
    it reads a XML "NAME" node and attribute that shows on the site.

    next what I wanna do is, make the buttons load a swf module that dynamically loads into a movieclip called for example "holder_mc"

    the swf files are also specified in the same XML also thorugh a node and attribute.

    like this site:
    http://activeden.net/item/advanced-x..._preview/20118

    I hope this helps explain a bit more visually XD

    anyway here is my xml and AS i have at the moment.

    Code:
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <content>
        <settings>
            <logo X="5" Y="6">content/logo.png</logo>
            <footer><![CDATA[ here goes Text]]></footer>
            <menu X="160"/>
        </settings>
        <nav>
            <main Name="HOME" swf="home.swf"/>
    	<main Name="DE BAND" swf="band.swf" toLoad="content/content.xml"/>
            <main Name="GIGS" swf="gigs.swf"/>
            <main Name="DISCOGRAFIE" swf="disco.swf"/>
            <main Name="BIO" swf="bio.swf"/>  
            <main Name="CONTACT" swf="contact.swf"/>
        </nav>
    </content>
    and here is my AS:

    Actionscript Code:
    menuSpace = 1;
    bttnSpace = 1;
    tween = 5;
    tweenAlpha = 3;
    maxHeight = 8;
    menu_all.menu_mc._visible = false;
    buildGallery = function () {
        total = xmlNode.childNodes[1].childNodes.length;
        for (i=0; i<total; i++) {
            categoryName[i] = xmlNode.childNodes[1].childNodes[i].attributes.Name;
            subNum[i] = xmlNode.childNodes[1].childNodes[i].childNodes.length;
            menu_all.menu_mc.duplicateMovieClip("menu_mc"+i,i);
            menu_all["menu_mc"+(total-1)].divide_mc._visible = false;
            clip = menu_all["menu_mc"+i];
            clip.txt_mc.categoryName.text = categoryName[i];
            clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.Link;
            clip.toLoad = xmlNode.childNodes[1].childNodes[i].attributes.toLoad;
            clip.ID = i;
            clip.subNum = subNum[i];
            clip.txt_mc.categoryName._width = clip.txt_mc.categoryName.textWidth+30;
            clip.spacer = clip.txt_mc.categoryName.textWidth+30;
            clip.divide_mc._x = clip.spacer;
            clip.bttn._width = clip.spacer;
            clip.bg_mc._width = clip.spacer;
            clip._x = menu_all["menu_mc"+(i-1)]._x+menu_all["menu_mc"+(i-1)].spacer;
            for (j=0; j<subNum[i]; j++) {
                menu_all["menu_mc"+i].item_mc.bttn_mc._visible = false;
                menu_all["menu_mc"+i].item_mc.bttn_mc.duplicateMovieClip("bttn_mc"+j,j);
                menu_all["menu_mc"+i].item_mc._alpha = 0;
                menu_all["menu_mc"+i].item_mc["bttn_mc"+j].ID = i+"-"+j;
                menu_all["menu_mc"+i].item_mc["bttn_mc"+j]._y = (menu_all["menu_mc"+i].item_mc.bttn_mc._height+bttnSpace)*j;
                menu_all["menu_mc"+i].item_mc["bttn_mc"+j].item_name_mc.item_name.text = xmlNode.childNodes[1].childNodes[i].childNodes[j].attributes.Name;
                menu_all["menu_mc"+i].item_mc["bttn_mc"+j].Link = xmlNode.childNodes[1].childNodes[i].childNodes[j].attributes.Link;
                menu_all["menu_mc"+i].item_mc["bttn_mc"+j].toLoad = xmlNode.childNodes[1].childNodes[i].childNodes[j].attributes.toLoad;
            }
            // end of for
        }
        // end of for
        menu_all._x = -(Stage.width-stageW)/2+(Stage.width-menu_all._width)/2;
        menu_all.divider_mc._visible = true;
        loadID();
    };

    Now this is probally not the most clean way to duplictate the menu item dynamically through xml but it works.

    I already tried to link the buttons through xml, but it didn't work seeing as im no Actionscript guru and got help from some other people so far too.
    Im kinda stuck.

    this is what I tried in general for the buttons to work, now they work but they always come back as undefined:

    Actionscript Code:
    trace(_global.clickAction = function(object) {
        if (object.Link.indexOf("http://") == 0) {
            getURL(object.Link, "_blank");
        } else if (object.Link != undefined) {
            _global.loadContent(object);
        }
        // end else if
    });
    loadXML = function (loaded) {
        if (loaded) {
            toLoad="holder";
            xmlNode=this.firstChild;
            categoryName=[];
            subNum = [];
            buildGallery();
            trace(Link = xmlNode.childNodes[1].childNodes[0].attributes.Link);
            toLoad = xmlNode.childNodes[1].childNodes[0].attributes.toLoad;
            ID = 0;
            _root.nav_mc.logo_mc.holder.loadMovie(xmlNode.childNodes[0].childNodes[0].firstChild.nodeValue);
            _root.nav_mc.logo_mc.holder._x = xmlNode.childNodes[0].childNodes[0].attributes.X;
            _root.nav_mc.logo_mc.holder._y = xmlNode.childNodes[0].childNodes[0].attributes.Y;
            _root.footer_mc.txt.htmlText = xmlNode.childNodes[0].childNodes[1].firstChild.nodeValue;
            _root.nav_mc.main_menu_mc._x = xmlNode.childNodes[0].childNodes[2].attributes.X;
            _global.loadStart(Link,toLoad,ID);
        } else {
            trace("Error loading XML");
        }
        // end else if
    };

    I hope somebody understands all this XD seeing as its alot of code and probally not all to clean either.

    thanks in advance tho

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    function buildGallery() {
    	total = xmlNode.childNodes[1].childNodes.length;
    	for (i=0; i<total; i++) {
    		categoryName[i] = xmlNode.childNodes[1].childNodes[i].attributes.Name;
    		subNum[i] = xmlNode.childNodes[1].childNodes[i].childNodes.length;
    		menu_all.menu_mc.duplicateMovieClip("menu_mc"+i,i);
    		menu_all["menu_mc"+(total-1)].divide_mc._visible = false;
    		var clip = menu_all["menu_mc"+i];
    		clip.onPress = clickAction;
    		clip.txt_mc.categoryName.text = categoryName[i];
    		clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.Link;
    		if (clip.Link == undefined) {
    			clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.swf;
    		}
    		clip.toLoad = xmlNode.childNodes[1].childNodes[i].attributes.toLoad;
    		clip.ID = i;
    		clip.subNum = subNum[i];
    		clip.txt_mc.categoryName._width = clip.txt_mc.categoryName.textWidth+30;
    		clip.spacer = clip.txt_mc.categoryName.textWidth+30;
    		clip.divide_mc._x = clip.spacer;
    		clip.bttn._width = clip.spacer;
    		clip.bg_mc._width = clip.spacer;
    		clip._x = menu_all["menu_mc"+(i-1)]._x+menu_all["menu_mc"+(i-1)].spacer;
    		for (j=0; j<subNum[i]; j++) {
    			clip.item_mc.bttn_mc._visible = false;
    			clip.item_mc.bttn_mc.duplicateMovieClip("bttn_mc"+j,j);
    			//clip.item_mc._alpha = 0;
    			var sub = clip.item_mc["bttn_mc"+j];
    			sub.onPress = clickAction;
    			sub.ID = i+"-"+j;
    			sub._y = (menu_all["menu_mc"+i].item_mc.bttn_mc._height+bttnSpace)*j;
    			sub.item_name_mc.item_name.text = xmlNode.childNodes[1].childNodes[i].childNodes[j].attributes.Name;
    			sub.Link = xmlNode.childNodes[1].childNodes[i].childNodes[j].attributes.Link;
    			if (sub.Link == undefined) {
    				sub.Link = xmlNode.childNodes[1].childNodes[i].childNodes[j].attributes.swf;
    			}
    			sub.toLoad = xmlNode.childNodes[1].childNodes[i].childNodes[j].attributes.toLoad;
    		}
    		// end of for    
    	}
    	// end of for    
    	menu_all._x = -(Stage.width-stageW)/2+(Stage.width-menu_all._width)/2;
    	menu_all.divider_mc._visible = true;
    	loadID();
    
    }
    
    function clickAction() {
    	trace(this.Link)
    	if (this.Link.indexOf("http://") == 0) {
    		getURL(this.Link, "_blank");
    	} else if (this.Link != undefined) {
    		_global.loadContent(this);
    	}
    }

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Awesome thanks Dawsonk.

    I had a good look at the difference between your and my code.
    And I think I grasp the basic things that are different, so thats good, im still not through it completely figuring out what exactly was the switch that made yours work and mine not.
    But im slowly getting there ^^

    Thank you very much

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Hey

    Sorry for the double post.
    But i've been trying to figure out how the code works and I still get the basics of it.

    But there is one problem, somehow the clickaction ****tion doesn't work anymore.
    it outputs correctly as in it reads the link is clickable but it doesn't do anything beyond that, and the trace doesn't doe anything either :S

  5. #5
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Tested it, like this...
    XML file - named 'menuxml.xml'

    On stage, empty movie clip 'holder_mc'
    On stage, movie clip 'menu_all', containing movie clip 'menu_mc', containing movie clip 'txt_mc', containing dynamic text field 'categoryName'

    Code on main time line...
    Code:
    var menuSpace = 1;
    var bttnSpace = 1;
    var tween = 5;
    var tweenAlpha = 3;
    var maxHeight = 8;
    var categoryName = new Array();
    
    menu_all.menu_mc._visible = false;
    var xmlNode = new XML();
    xmlNode.ignoreWhite = true;
    xmlNode.onLoad = loadXML;
    xmlNode.load("menuxml.xml");
    
    function loadXML(loaded) {
    	if (loaded) {
    		toLoad = "holder";
    		xmlNode = this.firstChild;
    		categoryName = [];
    		buildGallery();
    		ID = 0;
    	} else {
    		trace("Error loading XML");
    	}
    }
    
    function buildGallery() {
    	total = xmlNode.childNodes[1].childNodes.length;
    	for (i=0; i<total; i++) {
    		categoryName[i] = xmlNode.childNodes[1].childNodes[i].attributes.Name;
    		menu_all.menu_mc.duplicateMovieClip("menu_mc"+i,i);
    		var clip = menu_all["menu_mc"+i];
    		clip.onPress = clickAction;
    		clip.txt_mc.categoryName.text = categoryName[i];
    		clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.Link;
    		if (clip.Link == undefined) {
    			clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.swf;
    		}
    		clip.txt_mc.categoryName._width = clip.txt_mc.categoryName.textWidth+30;
    		clip.spacer = clip.txt_mc.categoryName.textWidth+30;
    		clip._x = menu_all["menu_mc"+(i-1)]._x+menu_all["menu_mc"+(i-1)].spacer;
    	}
    }
    
    function clickAction() {
    	trace(this.Link);
    	if (this.Link.indexOf("http://") == 0) {
    		getURL(this.Link, "_blank");
    	} else if (this.Link != undefined) {
    		holder_mc.loadMovie(this.Link);
    		//_global.loadContent(this);
    	}
    }

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Awesome Thanks this one works

    I also see what the problem was many thanks for the help

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    3

    Thumbs up How can I change color of menu items and arrange them vertically??

    Quote Originally Posted by dawsonk View Post
    Tested it, like this...
    XML file - named 'menuxml.xml'

    On stage, empty movie clip 'holder_mc'
    On stage, movie clip 'menu_all', containing movie clip 'menu_mc', containing movie clip 'txt_mc', containing dynamic text field 'categoryName'

    Code on main time line...
    Code:
    var menuSpace = 1;
    var bttnSpace = 1;
    var tween = 5;
    var tweenAlpha = 3;
    var maxHeight = 8;
    var categoryName = new Array();
    
    menu_all.menu_mc._visible = false;
    var xmlNode = new XML();
    xmlNode.ignoreWhite = true;
    xmlNode.onLoad = loadXML;
    xmlNode.load("menuxml.xml");
    
    function loadXML(loaded) {
    	if (loaded) {
    		toLoad = "holder";
    		xmlNode = this.firstChild;
    		categoryName = [];
    		buildGallery();
    		ID = 0;
    	} else {
    		trace("Error loading XML");
    	}
    }
    
    function buildGallery() {
    	total = xmlNode.childNodes[1].childNodes.length;
    	for (i=0; i<total; i++) {
    		categoryName[i] = xmlNode.childNodes[1].childNodes[i].attributes.Name;
    		menu_all.menu_mc.duplicateMovieClip("menu_mc"+i,i);
    		var clip = menu_all["menu_mc"+i];
    		clip.onPress = clickAction;
    		clip.txt_mc.categoryName.text = categoryName[i];
    		clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.Link;
    		if (clip.Link == undefined) {
    			clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.swf;
    		}
    		clip.txt_mc.categoryName._width = clip.txt_mc.categoryName.textWidth+30;
    		clip.spacer = clip.txt_mc.categoryName.textWidth+30;
    		clip._x = menu_all["menu_mc"+(i-1)]._x+menu_all["menu_mc"+(i-1)].spacer;
    	}
    }
    
    function clickAction() {
    	trace(this.Link);
    	if (this.Link.indexOf("http://") == 0) {
    		getURL(this.Link, "_blank");
    	} else if (this.Link != undefined) {
    		holder_mc.loadMovie(this.Link);
    		//_global.loadContent(this);
    	}
    }
    Firstly, thanks for the code dawsonk. I would like to know 2 things related to this code.

    1. The menu items are currently arranged horizontally. How can I have them arranged vertically?
    2. I would like menu items to change color on roll over. How can I achieve this?

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    function buildGallery() {
    	total = xmlNode.childNodes[1].childNodes.length;
    	var yVal = 0;
    	for (i=0; i<total; i++) {
    		categoryName[i] = xmlNode.childNodes[1].childNodes[i].attributes.Name;
    		menu_all.menu_mc.duplicateMovieClip("menu_mc"+i,i);
    		var clip = menu_all["menu_mc"+i];
    		clip.onPress = clickAction;
    		clip.onRollOver = doOverBtn;
    		clip.onRollOut = doOutBtn;
    		clip.txt_mc.categoryName.text = categoryName[i];
    		clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.Link;
    		if (clip.Link == undefined) {
    			clip.Link = xmlNode.childNodes[1].childNodes[i].attributes.swf;
    		}
    		clip.txt_mc.categoryName._width = clip.txt_mc.categoryName.textWidth+30;
    		clip.spacer = clip.txt_mc.categoryName.textWidth+30;
    		clip._y = yVal;
    		yVal += clip._height;
    	}
    }
    var my_OverFmt:TextFormat = new TextFormat();
    my_OverFmt.color = 0xFF0000; 
    
    var my_OutFmt:TextFormat = new TextFormat();
    my_OutFmt.color = 0x0000FF; 
    
    function doOverBtn() {
    	this.txt_mc.categoryName.setTextFormat(my_OverFmt);
    }
    function doOutBtn() {
    	this.txt_mc.categoryName.setTextFormat(my_OutFmt);
    }

  9. #9
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Works like a charm. Thanks dawsonk. <big thumbs up!>

  10. #10
    Junior Member
    Join Date
    Jul 2010
    Posts
    3

    Thumbs up Sub-menu not loading

    Code:
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <content>
        <settings>
            <logo X="5" Y="6">content/logo.png</logo>
            <footer><![CDATA[ here goes Text]]></footer>
            <menu X="160"/>
        </settings>
        <nav>
            <main Name="HOME" swf="home.swf"/>
    	<main Name="DE BAND" swf="band.swf" toLoad="content/content.xml"/>
            <main Name="GIGS" swf="gigs.swf"/>
            <main Name="DISCOGRAFIE" swf="disco.swf"/>
            <main Name="BIO" swf="bio.swf"/>  
            <main Name="CONTACT" swf="contact.swf"/>
        </nav>
    </content>
    dawsonk, correct me if I am wrong but I assume this line is for sub-menu navigation:
    Code:
    <main Name="DE BAND" swf="band.swf" toLoad="content/content.xml"/>
    I created a file called 'content.xml' and put it in a folder labeled 'content'. However, the flash swf is not displaying this sub-menu when I click on 'DE BAND'

    Is there something wrong in the code? How can I get the sub-menu to work?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center