A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: AS2 XML Hyperlink help

  1. #1
    Billionaire Philanthropist
    Join Date
    May 2010
    Posts
    11

    AS2 XML Hyperlink help

    Hey Guys, I have a project I'm working on for my cousin and I can't seem to figure out how to perfect a certain piece of it. Granted I haven't done this in a while so it's a bit of a re-hash for me... basically I have a few items in my XML file and I want to be able to successfully click on them and activate the hyperlink within them via my flash project.

    Here is the XML Code:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <links>
    <item number="001" label="001: Item One" url="http://www.my-eccomerce-site/item-1.html" />
    <item number="002" label="002: Item Two" url="http://www.my-eccomerce-site/item-2.html" />
    <item number="003" label="003: Item Three" url "http://www.my-eccomerce-site/item-3.html" />
    </links>

    Here is the AS2 Code:
    var xml:XML = new XML();
    xml.ignoreWhite = true;
    xml.onLoad = function()
    {
    var nodes = this.firstChild.childNodes;
    }
    {
    var item;
    item.url = info[i].attributes.url;
    item.label = info[i].attributes.label;
    item.label.onRelease = released;
    }
    xml.onLoad=function()
    {
    var props=this.childNodes[0].attributes;
    var info = this.childNodes[0].childNodes;
    for (var i=0; i<info.length; i++) {
    gen1List.addItem( {label:info[i].attributes.label, label:info[i].attributes.label, label:info[i].attributes.url} );
    }
    }
    //on release function
    function released()
    {
    getURL(this._root.url);
    }
    xml.load("xml/items.xml");

    -------------------------------------------------

    By the way "gen1List" is the instance name of the list component within the flash file.
    I release that it very well may be incomplete or inaccurate but as I mentioned before, it is a re-hash for me as I haven't done this in quite a while....
    Any help of this would be much appreciated as it is quite urgent!

  2. #2
    Senior Wabbit
    Join Date
    Jul 2008
    Location
    Winchester, Uk
    Posts
    215

    Complete Remake

    Okay I have looked at your code, and your right about a re-hash :P

    I have completely re-written your code.
    I have kept your variable names though, as well as the list instance name.
    Please read though the comments which are in the quotes thoughout the post as it will help explain what I have done, and help fix future issues for you.
    This code will work, with the instance name of gen1List
    Tested in Flash CS3, AS2.

    I have re-written the xml as elements: My reasoning below
    Quote Originally Posted by "Mr Wabbit
    You should always avoid attributes issues with attributes are as follows:
    > Attributes make the xml confusing, often causing mistakes. With Elements you can create easy to read trees.
    > Attributes are not designed for easy expansion, with elements you can add as much information as you like.
    Code:
    <?xml version="1.0" encoding="iso-8859-1" ?> 
    <links>
    	<item>
    		<label>001: Item One</label>
    		<url>http://www.my-eccomerce-site/item-1.html</url>
    	</item>
    	<item>
    		<label>002: Item Two</label>
    		<url>http://www.my-eccomerce-site/item-2.html</url>
    	</item>
    	<item>
    		<label>003: Item Three</label>
    		<url>http://www.my-eccomerce-site/item-3.html</url>
    	</item>
    </links>
    NB: To add another item to your list all you need to do is add another item with the label and url as shown above
    The Action script is designed to check the number of item nodes you have in your xml, so will always call the correct amount.

    On to the actionscript.
    Again I have done a large amount of changes to your original actionscript.
    Quote Originally Posted by "Mr Wabbit
    > First I have moved the onLoad to a separate function: This is to create a easier to read and well layed out code.
    > I have also added the else statement to the load function and well as a loaded variable
    This will stop any errors with the infomation which is called.
    Secondly, you will get a trace if the XML has failed to load, in which case check the url of the xml where xmlData.load (4th line)
    > Fixed your checking of node lengths, you also had 2 loadXML functions which may cause a clash, where data is called twice.
    > Added a single addItem instance, your lab and link vars will update with the firstChild.childNodes[i], again this helps for a nicer layout and smarted code
    > OnRelease wont work with a class, (list class inc) you need to create a listener on your list class. The listen checks the list class at the frame rate. If you change selection it will get the url or the item in the list.
    Code:
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("xml/items.xml");
    
    function loadXML(loaded) {  //This way the functions only happen if the xml is loaded (loaded is our var)
    	if (loaded) {  //If loaded
    		var nodes = xmlData.firstChild.childNodes;//Find the number of nodes to add
    		for (i=0; i<nodes.length; i++) {
    			_root.lab = this.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue;
    			_root.link = this.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue;
    			gen1List.addItem( {label:_root.lab, link:_root.link} );
    		}
    	} else {
    		trace("Your XML has not been loaded");
    	}
    }
    
    var listener:Object = new Object();
    listener.change = function(evt) {
    getURL(evt.target.selectedItem.link);
    };
    
    gen1List.addEventListener("change",listener);
    Wow, that took me 30 mins to type up on here, and an hour to re-write and test :P
    Lucky I had time, I did notice your post earlier though didn't have the time as knew it would take some time.

    Remember to mark as resolved, and best of luck on your project.

    Trust my code, and not my english.

  3. #3
    Billionaire Philanthropist
    Join Date
    May 2010
    Posts
    11
    Mr Wabbit, Thanks so much that works perfectly, I must admit I feel a little silly that it quite click. I do have one more question, sort of a little side avenue in case my cousin wants the eCommerce section to be done in flash as well...

    How would I go about replacing the url in the XML file with that of an AS2 Button...

    i.e: when I press "Item 1" instead of going to a url it unloads the movie clip of the previous item and goes to a specific frame in the timeline

    I've tested out with a generic button of the stage which is easy enough:

    Code:
    on (press) {
    	unloadMovieNum(1);
    	gotoAndPlay("i");
    	genI.gotoAndPlay("item-1");
    }
    but how to I then apply that code to the XML label?

    I apologize for the inconvenience...

  4. #4
    Senior Wabbit
    Join Date
    Jul 2008
    Location
    Winchester, Uk
    Posts
    215
    There is a few methods. - Guessing you wish to keep the list component and xml for the choice.
    Your request seems to be asking 2 different items.
    One - is having items on different frames
    Two - is having the items called in to a holder mc, So you could effectively have each item to call in a different swf, or you could even call in images and videos.

    Or none of the above
    Option 3 - Call in all the information you need, ie; All the images, videos, and text. Being an eCommerce section you would want a buy option. (More advance)

    Method 1;

    Place top of the frame
    Code:
    stop();
    then give each frame you which to include a name, ie Boots, Coats, Shirts, Pants.
    So we have 4 more frames
    Then change the labels and url in the xml to the frame name.
    Code:
    <links>
    	<item>
    		<label>Boots</label>
    		<url>boots</url>
    	</item>
    	<item>
    		<label>Coats</label>
    		<url>coats</url>
    	</item>
    	<item>
    		<label>Shirts</label>
    		<url>shirts</url>
    	</item>
    	<item>
    		<label>Pants</label>
    		<url>pants</url>
    	</item>
    </links>
    In the actionscript, Change
    Code:
    getURL(evt.target.selectedItem.link);
    To
    Code:
    gotoAndStop(evt.target.selectedItem.link);
    Method 2;
    Similar idea though you would place the url of the swf, video or image you wish to call in
    Then change the getURL to
    Code:
    mcholder.unloadMovie();
    mcholder.loadMovie(evt.target.selectedItem.link);
    So you would unload the item in mcholder, then load in the new one.
    The mcholder would be a movieclip on the timeline. With the instance name mcholder.
    You can then call in multiple item types.

    If you need more information shout.

    Trust my code, and not my english.

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