views:

2030

answers:

2

action script newbie here :-)

What I want to do is this, 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

+2  A: 

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:

  1. create a new Flash File (Ctrl-N)
  2. open the Components panel (Ctrl-F7)
  3. open the Library panel (Ctrl-L)
  4. drag Components > User Interface > Button to Library
  5. Select the first frame
  6. open the actions panel (F9)
  7. paste the first bit of code here
  8. Save your file (Ctrl-S)
  9. create a new ActionScript File (Ctrl-N)
  10. paste the second bit of code here
  11. Save your file in the same folder as "LinkButton.as" (Ctrl-S)
  12. Go back to your Movie (Ctrl-Tab)
  13. test the movie (Ctrl-Enter)

I strongly recommend you take some more training. Lee Brimelows site http://www.gotoandlearn.com is a good place to start.

Kristof Neirynck
ok, I pasted all of the code in frame 1 on the actions layergot the eror "1083: Syntax error: package is unexpected."does the package need to be put somewhere else?
mjr
the entire LinkButton class (everything from "package" to the end)needs to be placed in an external .as file. you can create this in CS3 by going to File -> New -> ActionScript File.
nerdabilly
I think I've got the class file moved..now I'm getting these errors1017: The definition of base class Button was not found.5000: The class 'customButtons' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
mjr
Wow! It works. Thank you so much. I'm sorry I needed such basic instruction. I tried working through a couple tutorials to get as far as I could, but I agree that I definitely need some more training. I'll be bookmarking the link you mentioned.
mjr
A: 

Hi i have a similar problem, what i want to do is parse thru the XML and find out how many nodes are there and to create buttons dynamucally. Its same as of now and i was able to do.

but i want to change the code in clicKHandler. what i wanna do here is to keep a flvplayback and to laod the video in playback player when ever the button is clicked

vikas1309