tags:

views:

528

answers:

4

Hi, I am new to XML and am having a small problem in Flash. I have a number of buttons. Each of these buttons need to open up a different URL which is in a xml file (I have added only one button for now (banstead), as I wasn´t sure how to add more).

My XML:

<?xml version="1.0" encoding="utf-8"?>
<banstead targ="_self" href="http://www.marca.com"&gt; </banstead>

My AS:

weblinkXML = new XML();
weblinkXML.ignoreWhite = true;
weblinkXML.load("xml/counties.xml");
weblinkXML.onLoad = function (success)
{
  var url = weblinkXML.firstChild.attributes.href;
  banstead.onRelease = function ()
  {
    getURL(weblinkXML.url);
  }
}

For some reason when I test the movie and click on the button it doesn´t open the URL I requested.

Appreciate any help

A: 

You've defined the variable url, but then called a different (undefined) variable weblinkXML.url in your getURL call.

Try this instead:

weblinkXML.onLoad = function (success)
{
  trace("success is "+success);// success should be TRUE
  var url = weblinkXML.firstChild.attributes.href;
  trace("url = "+url);
  banstead.onRelease = function ()
  {
    trace("button pressed with "+url);
    getURL(url);
  }
}

To add more buttons, you might try changing your XML file to look more like this:

<?xml version="1.0" encoding="utf-8"?>
<button buttonName="banstead" targ="_self" href="http://www.marca.com"&gt; </button>
<button buttonName="chipstead" targ="_self" href="http://www.yahoo.com"&gt; </button>
<button buttonName="tadworth" targ="_self" href="http://www.google.com"&gt; </button>

Then make a loop which goes through the XML and makes a button for each item it finds:

weblinkXML = new XML();
weblinkXML.ignoreWhite = true;
weblinkXML.load("xml/counties.xml");
weblinkXML.onLoad = function(success) {
    trace("success = "+success);
    banstead._visible=FALSE;// hide the original button: we're making copies
    for (var i:Number = 0; i< weblinkXML.childNodes.length; i++) {
        var myName:String = weblinkXML.childNodes[i].attributes.buttonName;
        // make sure banstead is a MovieClip, not a button, otherwise duplicateMovieClip won't work!
        var myButton:MovieClip = banstead.duplicateMovieClip(myName, i+1);
        // I've assumed your button has a dynamic text field in it named buttonLabel:
        myButton.buttonLabel.text=myName;
        myButton._y=banstead._y+ i*(banstead._height+5);// position the new button
        myButton.href = weblinkXML.childNodes[i].attributes.href;
        myButton.onRelease = function() {
            getURL(this.href);// note that each button has it's own href variable
        };
    }
};

I have made some assumptions about how your project is set up... but I've tested it and it seems to work for me

Let me know how you get on (and don't forget to click the big tick mark to accept this answer if it has helped you - thanks).

Richard Inglis
Hi, and thanks for your suggestion. I did do this and tried to test the movie. It still does not work though. It just opens up a blank undefined browser window.
Vickyboy
Strange, it worked when I tried it... How about adding some trace() statements to check everything is working? (I have updated my answer above to show you what I mean)
Richard Inglis
Thanks so much, it now works. Turns out that there was a problem with thew xml format. I creatded it in a normal text editor which must of caused a problem. I then created it in text Wrangler and it worked perfectly - thanks again!
Vickyboy
I do however have one more question - how can I add another button which has a different url. I know how to add it in the XML file, but how do I add it in the as?
Vickyboy
I've made some suggestions (above) for adding buttons.
Richard Inglis
this is great, thanks so much for all your help, I think this will work
Vickyboy
A: 

0 Nice tut.. thanks^^

I was working with the multiple links...The links works perfectly but link's names don't show up -.-

I put it together in "banstead", I named "myName" and it's in dynamic text.

...what am I doing wrong??

Also , I'm using this xml url link for a video player AS2. This player is loaded into a main flash website.. Exporting the player works offline (without the link name)..but when I open the html with the main flash website the xml url doesn't open the url...-.-

Any sugestions^^

Thanks in advance.

Josh

Josh
For a follow up question like this better post it as a new question, not as an answer. Simply put, more people are likely to see your question and answer it well if you ask a new question than if you hide a question in the answers to another question.
sth
A: 

Great tutorial. I have a question. I have put the barnstead MC inside another MC and when it runs the click through URL's don't work. How would you code myButton.onRelease function to work while inside another MC called ward2? Forrest

Forrest Molstad