create dynamic navigation from xml file in flash cs3

I want to hand an xml file to a swf and have the swf generate a dynamic text box and button for each of the links in the xml file

a rudimentary navigation

here's the xml

<?xml version="1.0" encoding="UTF-8"?>
    <page>
    <page name="Page Name 1" url="/page-1/" />
    <page name="Page Name 2" url="/page-2/" />
    <page name="Page Name 3" url="/page-3/" />
    <page name="Page Name 4" url="/page-4/" />
    </page>

and in my fla I have a button in my library named 'nav_button'

there's a layer named actions and in frame 1 I have this

var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();

var button:Button = new Button();


xmlLoader.load(new URLRequest("links.xml"));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
xml = XML(event.target.data);
xmlList = xml.children();
trace(xml.length());

for(var i:int = 0; i < xmlList.length(); i++)
{
    button = new Button();
    button.x = 25;
    button.y = i * 50 +25; 
    addChild(button);
}
}

the xml imports fine, but when it comes to the for loop and adding the buttons and text boxes to the stage, I'm toast


Your xml file is invalid. It can only contain a single root node.

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<pages>
<page name="Page Name 1" url="/page-1/" />
<page name="Page Name 2" url="/page-2/" />
<page name="Page Name 3" url="/page-3/" />
<page name="Page Name 4" url="/page-4/" />
</pages>

This is the code for your first frame:

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("links.xml"));

function xmlLoaded(event:Event):void {
    var pages:XML = new XML(event.target.data);
    var i:int = 0;
    for each (var page:XML in pages.page) {
        var button:LinkButton = new LinkButton();
        button.x = 25;
        button.y = i * 50 + 25; 
        button.label = page.attribute("name");
        button.url = page.attribute("url");
        addChild(button);
        trace(i++);
    }
}

And you'll need a custom button class:

package {
    import fl.controls.Button;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;

    public class LinkButton extends fl.controls.Button {
        public var url:String;
        public function LinkButton(){
            super();
            this.addEventListener(MouseEvent.CLICK, clickHandler);
        }
        private function clickHandler(mouseEvent:MouseEvent) {
            flash.net.navigateToURL(new URLRequest(url), "_blank");
        }
    }
}

Maybe I need to explain this in a bit more detail:

  • create a new Flash File (Ctrl-N)
  • open the Components panel (Ctrl-F7)
  • open the Library panel (Ctrl-L)
  • drag Components > User Interface > Button to Library
  • Select the first frame
  • open the actions panel (F9)
  • paste the first bit of code here
  • Save your file (Ctrl-S)
  • create a new ActionScript File (Ctrl-N)
  • paste the second bit of code here
  • Save your file in the same folder as "LinkButton.as" (Ctrl-S)
  • Go back to your Movie (Ctrl-Tab)
  • test the movie (Ctrl-Enter)
  • I strongly recommend you take some more training. Lee Brimelows site is a good place to start.


    I realize this is old, but it was new to me today.

    I don't understand why you'd want to create a custom button class. I don't think it's necessary to accomplish what you were trying to do.

    function xmlLoaded(event:Event):void {
        var pages:XML = new XML(event.target.data);
        var i:int = 0;
        for each (var page:XML in pages.page) {
            var button:LinkButton = new LinkButton();
            button.x = 25;
            button.y = i * 50 + 25; 
            button.label = page.attribute("name");
            button.url = page.attribute("url");
            addChild(button);
            trace(i++);
        }
    }
    

    The function above is fine as far as it goes, but why not add the event listener in the for loop? Add the line:

    button.addEventListener(MouseEvent.CLICK, gotoURL);
    

    within the for loop, then add the function:

    function gotoURL(e:MouseEvent):void
    {
        navigateToURL(new URLRequest(e.target.url));
    }
    

    This would eliminate the need for creating external files and custom classes.

    链接地址: http://www.djcxy.com/p/17804.html

    上一篇: Flash电影的表现真的很奇怪,可能是腐败的FLA?

    下一篇: 在flash cs3中从xml文件创建动态导航