Jon Mitten
Jon Mitten

Reputation: 2055

ActionScript 3 buttons and links from XML

I am successfully pulling in a remote XML document with a roughed out structure into Flash CC via ActionScript 3.0

The AS3 looks like:

import flash.events.Event;
import flash.net.URLLoader;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;

var myXML: XML = new XML();
var myXMLList:XMLList = new XMLList;
var XML_URL: String = "http://dirtymitten.org/xmlbuttons.xml";
var myXMLURL: URLRequest = new URLRequest(XML_URL);
var myLoader: URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);


function xmlLoaded(event: Event): void {

    myXML = XML(myLoader.data);
    trace("data loaded");

    var i:int = 1;
    var number_txt:String;
    for each (var node in myXML.Listing)
    {
        number_txt = "button number " + i.toString();
        trace(node.Title);
        trace(node.Url);
        trace( number_txt );
        i++;
    }

}

The trace statements work as expected, and the iteration also works.

What I am failing to accomplish is being able to get a MovieClip instance to inherit these values. I have a Library object called MyButton with dynamic text fields title_txt and count_txt. When I change my for each loop to instantiate a new MyButton instance, I get some errors:

...
for each (var node in myXML.Listing)
    {
        var myButton:MyButton = new MyButton();
        myButton.title_txt.text = node.Title;
        myButton.count_txt.text = i.toString();
        myButton.url = node.Url;
        number_txt = "button number " + i.toString();
        trace(node.Title);
        trace(node.Url);
        trace( number_txt );
        i++;
    }
...

My errors read Scene 1, Layer 'actions', Frame 1, Line 27, Column 22 1119: Access of possibly undefined property text through a reference with static type String. and another for line 28, which correspond to :

myButton.title_txt.text = node.Title;
myButton.count_txt.text = i.toString();

meaning either the text property isn't supported out-of-the-box or I'm not accessing the MovieClip object, but rather a non-existent property of title_txt from the custom class MyButton. I thought setting up properties in a class that matched the name of the Library object would correspond with one another, but that doesn't seem to be the case.

In the Library object MyButton I have dynamic text fields for title_txt and count_txt which I'd like to populate with XML.

The MyButton.as class definition is a separate file, and is built like:

package  {

    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;


    public class MyButton extends MovieClip {

        // constructor code
        public var url:String;
        public var title_txt:String;
        public var count_txt:String;

        public function MyButton(){
            this.addEventListener(MouseEvent.CLICK, clickHandler);
        }

        private function clickHandler(e:MouseEvent):void{
            if (!url) return;
            navigateToURL(new URLRequest(url), "_blank");
        }
    }

}

I haven't explicitly included the file, as it was self-generated and then edited later, so I presume it's bound to the Library MovieClip and not needing an explicit import MyButton.as. It's targeted by the xml button.fla project, so I assume it's implicitly bound.

How can I generate a button with XML-derived labels and URLs, assuming the XML loads cleanly and custom classes exist for the MovieClip I want to use?

Upvotes: 1

Views: 144

Answers (1)

null
null

Reputation: 5255

I have a Library object called MyButton with dynamic text fields title_txt and count_txt.

No, you don't. You have a class that has String variables of said names:

public class MyButton extends MovieClip {
        public var url:String;
        public var title_txt:String; //THIS IS A STRING, NOT A TEXTFIELD!!!
        public var count_txt:String;

but Strings aren't TextFields. Just like apples aren't oranges.

If you want to have TextFields, you should type your variables accordingly.

Upvotes: 1

Related Questions