AS3 / XML issues

Commenta 27th October , 2009
I’m a designer trying to learn AS3, so please bear with me…

I’m creating a new portfolio site with Flash/AS3 and XML so when I want to add new examples, I only have to touch the XML file. Basically, I’m creating some buttons dynamically through XML. That part i’ve figured out. What I need to do, depending on which of those dynamically buttons is clicked, I want to also pull the project title (title attribute in the XML).

Here’s the XML:

<?xml version="1.0" encoding="utf-8"?>

<site>
<links>
<link name="1" title="Project 1" />
<link name="2" title="Project 2" />
<link name="3" title="Project 3" />
<link name="4" title="Project 4" />
</links>
</site>

Here’s the AS:

import fl.transitions.Tween;
import fl.transitions.easing.*;

var xmlPath:String = "data/identity.xml";
var settingsXML:XML;

var loader = new URLLoader();
loader.load (new URLRequest(xmlPath));
loader.addEventListener (Event.COMPLETE, xmlLoaded);

function xmlLoaded (e:Event):void {

if ((e.target as URLLoader) != null ) {
settingsXML = new XML(loader.data);
settingsXML.ignoreWhitespace = true;
createMenu ();
}

}

function createMenu ():void {
var menuItem:MenuItem;

var i:uint = 0;
for each (var link:XML in settingsXML.links.link) {
menuItem = new MenuItem();

menuItem.menuLabel.text = link.@name;
menuItem.name = "project" + menuItem.menuLabel.text;

menuItem.menuLabel.autoSize = TextFieldAutoSize.CENTER;

menuItem.x = -200;
menuItem.y = -100 + i*60;

menuItem.buttonMode = true;
menuItem.mouseChildren = false;

menuItem.addEventListener (MouseEvent.CLICK, mouseClicked);

addChild (menuItem);
i++;
}
}

function mouseClicked(e:Event):void {
trace(e.target.name);
}

I’ve tried several different approaches in the mouseClicked function, but I can’t anything to work. Suggestions please?

Thanks,
Bill

Original post by billcoons

Lascia un Commento