dcsimg
A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 35

Thread: How to control a buttons link using xml

  1. #1
    Member
    Join Date
    May 2006
    Posts
    85

    How to control a buttons link using xml

    Hi

    Does anyone know how I could control a buttons link using xml? That way the link could be changed anytime without having to open the flash source file and re-export the movie.

    I will appreciate it much

    ciao

    :-)

  2. #2
    Can't Re-Member madzigian's Avatar
    Join Date
    Apr 2004
    Location
    Boston MA
    Posts
    2,662
    are you wanting to change a list of links?.. of just a single link attached to a single button?... if you need something like a list box populated through XML i can post a source file for you.. For an many child nodes as you have... it will attach a button from the library, populate the text and assign a link....
    Please DO NOT PM me simply for individual help. Post in the public forums...that's what they are for, and they allow others to benefit as well. - Thx
    OS X 10.3.9 || Flash MX04 Pro
    | del.icio.us || furl || Helpful Source Files || Free Pixel Fonts |

  3. #3
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    you would just use the value in that particular node as the value of the VAR in the getURL method.

    example:

    Code:
    button1.onPress = function() {
        getURL(xmlDocument.childNode[0].firstChild.nodeValue);
    }
    or wherever the node really is.

    heres a quick example: (although Im not sure HOW your link is going to be set up..here I just used a textField to not only DISPLAY the value of the NODE.but use that value AS the URL to go to:

    example:
    Code:
    var myXML:XML = new XML("<Details><Username>(ip) R o l i e (ip) ~ (#)</Username><PSM>http://www.nike.com</PSM><Status>Online</Status></Details>");
    
    trace("WHOLE XML: "+myXML);
    /////
    trace(newline);
    ////
    //username node
    trace("Full User Node: "+myXML.firstChild.childNodes[0]);
    trace("User Node Name: "+myXML.firstChild.childNodes[0].nodeName);
    trace("User Value: "+myXML.firstChild.childNodes[0].firstChild.nodeValue);
    /////
    trace(newline);
    ////
    //psm node
    trace("Full PSM Node: "+myXML.firstChild.childNodes[1]);
    trace("PSM Node Name: "+myXML.firstChild.childNodes[1].nodeName);
    trace("PSM Value: "+myXML.firstChild.childNodes[1].firstChild.nodeValue);
    linkField_txt.text = myXML.firstChild.childNodes[1].firstChild.nodeValue;
    var myformat:TextFormat = new TextFormat();
    myformat.url = myXML.firstChild.childNodes[1].firstChild.nodeValue;
    linkField_txt.setTextFormat(myformat);

  4. #4
    Member
    Join Date
    May 2006
    Posts
    85

    link control using xml

    Hi Whispers

    I was thinking more of a situation like this:

    1. I have created my buttons within flash.

    2. The links for each of these buttons will be taken from an external xml file

    That way, when I want to lets say disable a link, I could do that from the xml file or change the link easily.

    As simple as that..

    Will appreciate

  5. #5
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    use the code above... its the same no matter how you apply it..I just chose to output it a textField..and use THAT as the button/link..

    Code:
    var myXML:XML = new XML("<links><link>http://www.nike.com</link><link>http://www.google.com</link><link>http://www.yahoo.com</link></links>");
    
    trace("WHOLE XML: "+myXML);
    /////
    trace(newline);
    ////
    //link1 node
    trace("Full Link Node: "+myXML.firstChild.childNodes[0]);
    trace("Link Node Name: "+myXML.firstChild.childNodes[0].nodeName);
    trace("Link Value: "+myXML.firstChild.childNodes[0].firstChild.nodeValue);
    link1 = myXML.firstChild.childNodes[0].firstChild.nodeValue;
    /////
    trace(newline);
    ////
    //link2 node
    trace("Full Link Node: "+myXML.firstChild.childNodes[1]);
    trace("Link Node Name: "+myXML.firstChild.childNodes[1].nodeName);
    trace("Link Value: "+myXML.firstChild.childNodes[1].firstChild.nodeValue);
    link2 = myXML.firstChild.childNodes[1].firstChild.nodeValue
    now on your buttons you could have:

    button1:
    Code:
    on(press) {
        getURL(link1);
    }
    button2:
    Code:
    on(press) {
        getURL(link2);
    }
    same as doing:

    button1:
    Code:
    on(press) {
        getURL(myXML.firstChild.childNodes[0].firstChild.nodeValue);
    }
    and this is exactly what I just posted before... unless I am missing something?

  6. #6
    Member
    Join Date
    May 2006
    Posts
    85

    Yea, thanks

    But whispers

    according to the script it looks like the xml is embedded in the flash file.

    How would I link what you have given me to an external xml file??

    thanks, will appreciate

    :-)

  7. #7
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    ahhhh..I see I didnt know you need to know how to LOAD the XML as well your first post gave me the impression you knew how already. basically you would use an XML Object...(same as a loadVar Object but for XML)...

    this is how a basic XML object is created and used:

    1.) to import an XML file...first you need to create an XML object to refer to your data..

    2.) you then need to define the actions FLASH wll perform once the XML data "IS" loaded..

    3.) you then need to make a call to actually try and LOAD the XML file.


    Code:
    // create xml object (step1)
    myXML = new XML();
    // define actions to do once XML loads (step2)
    myXML.onLoad = function(success) {
    	if (!success) {
            	trace("XMLfailed to load");
    	   	// plus any other actions you want to execute
            }else {
            	trace("load was good"); 
    		// plus any other actions you want to execute              
    	}    
    };       
    // call the actual XML file (step3) 
    myXML.load("whateverFile.xml");
    so your code might be similar to this:
    Code:
    // create xml object (step1)
    myXML = new XML();
    // define actions to do once XML loads (step2)
    myXML.onLoad = function(success) {
    	if (!success) {
            	trace("XMLfailed to load");
    	   	// plus any other actions you want to execute
            }else {
            	trace("load was good"); 
    		link1 = myXML.firstChild.childNodes[0].firstChild.nodeValue;
    		link2 = myXML.firstChild.childNodes[1].firstChild.nodeValue; 
    	}    
    };       
    // call the actual XML file (step3) 
    myXML.load("whateverFile.xml");

  8. #8
    Junior Member
    Join Date
    Apr 2007
    Posts
    11
    Hi,

    I have tried this code and it doesent work for me - hope someone will help me find what im doing wrong...

    I have 9 buttons in one flash (menu bar) and for that 9 buttons I need that they get links form xml file so links can be changed without flash.

    I'm using flash mx

    this is what my xml looks like

    <menu>
    <menu1>
    <url>home.php</url>
    <alt>Home</alt>
    <title>Home</title>
    </menu1>
    <menu2>
    <url>news.php</url>
    <alt>News</alt>
    <title>News</title>
    </menu2>
    <menu3>.... and so on.

    I have tried with xml like in this example here

    <links>
    <url><![CDATA[home.php]]></url>
    <url><![CDATA[news.php]]></url>...

    and put code on each button

    Button 1

    on (release) {
    myButton.onPress = function() {
    getURL(urls[0].firstChild.nodeValue);
    };
    }

    Button 2

    on (release) {
    myButton.onPress = function() {
    getURL(urls[1].firstChild.nodeValue);
    };
    }

    Button 3

    ... getURL(urls[2].firstChild.nodeValue... changing number after urls

    but it doesent work - xml loads and when I test movie links show in output window but when I click buttons nothing happens.

    If someone could help me I would be gratefull - and maybe advice what needs to be changed so I can use my xml...

    Thanks

  9. #9
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    I think it is your button code/syntax..

    you have an onRelease checking for an onPress function.. probably not gonna happen.

    1.) is the code ON the buttons.. or in a FRAME?
    2.) you do have button instance names for them...correct?

  10. #10
    Junior Member
    Join Date
    Apr 2007
    Posts
    11
    Thanks for replying - I think it also has something with flash mx - using as1 and I think this code is for as2... dont like coding - im more visual man...
    1. I have 9 buttons on one layer and code is on each button
    2. er... donno... probaly not...

    Please help...

  11. #11
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    lol..

    ok..no problem.. we'll get it figured out.

    sorry I missed you were using MX..

    the code for your buttons should be the

    on(press) {
    //code / path to xml value goes here
    }

    syntax.

    what is your code for parsing the XML into your movie?..

    do you have a VAR (url[]) that is a shortcut to the url node/element??

    you sure its correct?

  12. #12
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    are you using the XML code posted above?

    that is for XM2004 and above....

    I dont think MX let you strict type your vars yet..

    if there is any in any of the code your using.

  13. #13
    Junior Member
    Join Date
    Apr 2007
    Posts
    11
    This is code from above I used for loading xml into flash

    Code:
    links_xml = new XML();
    links_xml.load("menulink.xml");
    links_xml.ignoreWhite = true;
    links_xml.onLoad = function(success) {
    	if (success) {
    		urls = this.firstChild.childNodes;
    		for (i=0; i<urls.length; i++) {
    			trace(urls[i].firstChild.nodeValue);
    		}
    	} else {
    		trace("error loading xml");
    	}
    };
    dont see any var thingies...

    Also I tried your code for button and it works but only if links are in same folder - like <url><![CDATA[menu.html]]></url> but if its <url><![CDATA[http://www.google.com]]></url> - outside link than it doesent work - i get message "cant find //C:/localfolder/www.google.com"

    Also what I would need to change to use xml in format like this

    Code:
    <menu>
        <menu1>
            <url>home.php</url>
            <alt>Home</alt>
            <title>Home</title>
        </menu1>
        <menu2>
            <url>news.php</url>
            <alt>News</alt>
            <title>News</title>
        </menu2>
        <menu3>...etc
    Thanks for helping me!

  14. #14
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    Code:
    links_xml = new XML();
    links_xml.load("menulink.xml");
    links_xml.ignoreWhite = true;
    links_xml.onLoad = function(success) {
    	if (success) {
    		trace("XML: " + links_xml);
    		totalLinks = links_xml.firstChild.childNodes.length;
    		trace("TOTAL LINKS: " + totalLinks+newline);
    		for (i = 0; i < totalLinks; i++) {
    			trace("LINK " + i + " DETAILS:");
    			urlVar = links_xml.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue;
    			trace("URL: " + urlVar);
    			altVar = links_xml.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue;
    			trace("ALT: " + altVar);
    			titleVar = links_xml.firstChild.childNodes[i].childNodes[2].firstChild.nodeValue;
    			trace("TITLE: " + titleVar);
    			trace("-------------------------------------");
    		}
    	} else {
    		trace("error loading xml");
    	}
    };
    I havent tested it..but it should work for your XML set-up..like you want.

  15. #15
    Junior Member
    Join Date
    Apr 2007
    Posts
    11
    Thanks! - tried it, doesn't work for me - i guess button code should also be changed...

  16. #16
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    there is NO button code here.. this is JUST to get your XML..and be able to trace/find EACH element/value in EACH menu item

    I just tested it.. using the sample XML you supplied.. I found ALL values..

  17. #17
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    does flash MX have loadVar objects?? even?..that may be the problem? I cant remember.. oh wait..you said your loadVar object was working...right?..

  18. #18
    Junior Member
    Join Date
    Apr 2007
    Posts
    11
    And how do I insert that values on to button? ... I'm being boring... awww...

    One last favour if I can ask - I'll use code that worked with button but only if you have any idea why it doesent work when link has http://ww... in it - as I posted few posts ago - if I can make that work that would be it...

    Thank you one more time.

  19. #19
    Junior Member
    Join Date
    Apr 2007
    Posts
    11
    Yes code you gave me works it shows all data that is in xml nodes but I dont know to put it on buttons...

  20. #20
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    real quick before I leave for work:

    example code:

    Code:
    links_xml = new XML();
    links_xml.load("menulink.xml");
    links_xml.ignoreWhite = true;
    links_xml.onLoad = function(success) {
    	if (success) {
    		trace("XML: " + links_xml);
    		totalLinks = links_xml.firstChild.childNodes.length;
    		trace("TOTAL LINKS: " + totalLinks+newline);
    		for (i = 0; i < totalLinks; i++) {
    			trace("LINK " + i + " DETAILS:");
    			_root.urlVar = links_xml.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue;
    			trace("URL: " + urlVar);
    			_root.altVar = links_xml.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue;
    			trace("ALT: " + altVar);
    			_root.titleVar = links_xml.firstChild.childNodes[i].childNodes[2].firstChild.nodeValue;
    			trace("TITLE: " + titleVar);
    			trace("-------------------------------------");
    		}
    	} else {
    		trace("error loading xml");
    	}
    };

    example button code:

    on(press){
    getURL("http://www.yourdomain.com/folder/+"_root.urlVar);
    }

    is an example.. however all buttons will have the LAST url added.. dsince you are only using 1 var.. need to use _root["urlVar"+i]

    basically since your in MX you can ASSIGN button actions..as your button dont have instance name (do they?) so you need to dump the value to a VARIABLE so the button can grab it in its execution..

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