PDA

View Full Version : Attaching a movieclip and reacting to a mouse click. Once so easy - now not so much.



katyjack
October 13th, 2008, 09:52 AM
This is my first as3 project and it's fairly simple: read in values from an xml file (love that), and then loop through the results. For each result, I want to:

1. Attach a movieclip
2. Change the text in the clip to match the text in the xml
3. Be able to click on the clip and have it echo back the text I just added.

I've got this code so far
[PHP]
function boxClick(event:MouseEvent):void {
trace("box clicked");
}

function onLoaded(e:Event):void
{

xml = new XML(e.target.data);
var slideList:XMLList = xml.slides.slide;
var newX:int = 50;
var newY:int = 25;
var slide1:XMLList = slideList[0].options;

//for (var i:int = 0; i < slide1.length(); i++) {
//trace(slide1.option.optionlabel.text());
//}

for (var i:int = 0; i <slide1.option.length(); i++) {
var box:mcAvatar = new mcAvatar();
box.x = newX;
addChild(box);
newX += 100;
trace(slide1.option[i].optionlabel.text());
box.gender.text = slide1.option[i].optionlabel.text();
trace(box.gender.text);
box.addEventListener(MouseEvent.CLICK, boxClick);
}
}</PHP>

but, although I draw the boxes out and I get the right value in each box's test field, it won't respond to any mouse clicks. What am I doing wrong?

graylensman
October 13th, 2008, 10:15 AM
Something I stumbled across, as I've been trying to learn AS3 (coming from AS2): you have to tell movie clips they're buttons.

Add in

box.buttonMode = true;
in your onLoaded function. See if that makes a difference. And let me know! :)

katyjack
October 13th, 2008, 10:30 AM
Worked Perfectly! Thanks so much.

graylensman
October 13th, 2008, 11:24 AM
You're welcome. And now you've pretty much exhausted my range of AS3 knowledge. :P