PDA

View Full Version : Creating, naming and accessing movie clips dynamically in AS3



mrbelv
August 26th, 2008, 12:25 PM
I have a menu I wrote in AS2 where the menu items are pulled from an XML file. The values for the menu list are written to a text field which is located within a movie clip since there are rollover, rollout and click actions for each one of the menu items.

The code loops through the XML nodes and duplicates the movie clip with the text field for each XML node. The menu item movie clips are created within a movie clip, which is contained inside another movie clip.

In AS2, I have been able to do this with relative ease either writing the movie clips to an array or using the eval function. I'm naming and accessing the clips dynamically by calling them "movieclip" + i within the loop that gets the XML nodes:


for (var i=1; i < slide.length; i++) {
menuPanelMC.panelContentMC.attachMovie("content", "content" + i, i);
slideName = slide[i].firstChild.firstChild;
eval("menuPanelMC.panelContentMC.content" + i + ".menuOverMC")._alpha = 0;
eval("menuPanelMC.panelContentMC.content" + i).fieldName.text = slideName;
}

I realize I could do this with an array as well, but this works fine for what I’m doing here.

I am trying to upgrade this code for AS3. This should be simple to do, but it isn’t.

I have tired different variations – which all tend to look something like this:

for each (var sectionElement:XML in sectionList) {

i++;

var newClip:MovieClip = new MovieClip();
newClip.name = "content" + i;
clips[i] = newClip;

menuPanelMC.panelContentMC.addChild(clips[i]);
}

No matter how I try and get this to work, I get all kinds of errors. It seems to be putting something in the array, but when I try to do this:

menuPanelMC.panelContentMC.clips[i].fieldName.name = whatever the XML node is;

It doesn’t work.

I am at a total loss here, but obviously I am doing something seriously wrong.

Any help would be greatly appreciated.

Anil_kumar
August 27th, 2008, 07:22 AM
may i help you with "AS3" ?

Anil
anilkumarnd@gmail.com

mrbelv
August 27th, 2008, 11:08 AM
huh?

Anil_kumar
August 27th, 2008, 11:17 AM
try this code or download source file :}

///////////////////////////////////////////////////////////////

var listLoader:URLLoader = new URLLoader( new URLRequest("filelist.xml") );
var btnLoader:Loader = new Loader();
listLoader.addEventListener(Event.COMPLETE, gotList);
var xmlData:XML;
function gotList(evt:Event):void {
xmlData = XML(listLoader.data);
var i:int = 1;
for each (var item:XML in xmlData..btn) {
//trace(item.name.toString());
var myMC:MovieClip = new content_mc();
//myMC.y = i*170;
myMC.y = i*myMC.height;
myMC.title_txt.text =item.name.toString();
this.content.addChild(myMC);
i++;
}
listLoader.removeEventListener(Event.COMPLETE, gotList);

}
///////////////////////////////////////////////////////////////

Anil
anilkumarnd@gmail.com

scrabble
September 29th, 2008, 03:28 PM
hi, thanks for the sample file, i understand how this works, but how do you assign an independent function to each button? (i'm transitioning to AS3 from AS2)

Anil_kumar
September 30th, 2008, 01:09 AM
myMC.mouseChildren =false;
myMC.addEventListener(MouseEvent.CLICK,ClickHandle r);
myMC.name="button"+i;



function ClickHandler(e:MouseEvent):void {
trace(e.target.name);
}


Anil
Flash Workshop (http://flash-workshop.blogspot.com/)
anilkumarnd@gmail.com

scrabble
September 30th, 2008, 11:09 AM
awesome! thanks!