all groups > flash actionscript > november 2004 >
You're in the

flash actionscript

group:

Assigning actions to buttons based on xml file??



Assigning actions to buttons based on xml file?? Doug Strickland
11/14/2004 6:09:50 PM
flash actionscript: I have a template that I would like to use that has a series of nav buttons
at the bottom. clicking on any of the nav buttons does some script (getURL
type stuff) The thing is, I would really like to assign the specifics of
what those buttons do, effectively their url's, from an external source.
This way I can use the same template, and have the xml identify the correct
url depending on what I'm using the template for.

like:
<xml>
<btnNav1> "http://this.org" </btnNav1>
<btnNav2> "http://that.net" </btnNav2>
....etc
</xml>

Am I on the right track?

It would be really cool if I could just parse the xml at the beginning of
the movie, and have all the buttons know their job :)

If this is something straightforward, please explain where to start.

If this goes beyond forums, please point me to some good reference info
(books, sites, etc.)

thanks in advance.

Re: Assigning actions to buttons based on xml file?? proxmaster
11/15/2004 6:04:39 AM
This isn't overly difficult to do, but it will require a bit of work. The
example I have provided is meant to be a general overview of the process and
the various pieces required. I use this same process of dynamically creating my
buttons as well as other flash objects from an external xml file all the time.

In order for this to work with the following example, you will need create a
movie clip from a button and then define it as a component in flash with the
following parameters:

txtlabel - String
xpos - number
ypos - number
urlpath - String

Let me know if you require more detailed information on creating components
and defining parameters in them.
This code would be on the component:
this.buttonlabel = this.txtlabel;
this._x = this.xpos;
this._y= this.ypos;
this.onRelease=function(){
getURL(this.urlpath);


// The following code should be executed on the root level of the movie. This
example will load and parse the xml file, and create the buttons dynamically on
the initial movie load.

buttons_xml= new XML();
buttons_xml.load ("buttons.xml");
buttons_xml.ignoreWhite = true;
buttons_xml.onLoad = loadXMLData;

function loadXMLData(){

ilen = buttons_xml.childNodes.length;
i=0;

while(i<ilen){
if (buttons_xml.childNodes.nodeName.toLowerCase() == "buttons") {

klen = buttons_xml.childNodes.childNodes.length;
k=0;
while(k<klen){
subObj = buttons_xml.childNodes[k];

if (subObj.nodeName.toLowerCase() == "button") {
_root.ph1.attachMovie("buttoncomponent","newbutton"+k,k);
var btn = _root.ph1["newbutton"+k];
btn.txtlabel=buttons_xml.childNodes[k].attributes.txtlabel;
btn._x=buttons_xml.childNodes[k].attributes.xpos;
btn._y=buttons_xml.childNodes[k].attributes.ypos;
btn.urlpath=buttons_xml.childNodes[k].attributes.urlpath;
}
k++;
}
}
i++;
}
}

//The following is a basic xml file that is saved as buttons.xml in the same
location as the final swf file. I have include a few basic attributes for
creating buttons for demo purposes. You will most likely want to modifiy these
to suit your own needs.

<buttons>
<button txtlabel="button1" xpos="50" ypos="120"
urlpath="http://www.mylocation1.com"/>
<button txtlabel="button2" xpos="50" ypos="140"
urlpath="http://www.mylocation2.com"/>
<button txtlabel="button3" xpos="50" ypos="160"
urlpath="http://www.mylocation3.com"/>
<button txtlabel="button4" xpos="50" ypos="180"
urlpath="http://www.mylocation4.com"/>
<button txtlabel="button5" xpos="50" ypos="200"
urlpath="http://www.mylocation5.com"/>
</buttons>
Re: Assigning actions to buttons based on xml file?? ScooterMX
11/15/2004 7:55:46 AM
Great detail, thanks. I'm reading up on component development, as this
really seems like a useful concept for other development with this client.
I appreciate the time you took to write this out. I'll be back with
questions, no doubt....

One quick question for now: Will the component objects be persistant across
scenes, or do I need to reload them if the client is using scenes for very
long swf's? (training vo/slides that go 2-3 minutes each)??

thanks again,
Doug

[quoted text, click to view]

Re: Assigning actions to buttons based on xml file?? proxmaster
11/15/2004 5:04:41 PM
AddThis Social Bookmark Button