0
votes

I am trying to convert an array of string to MovieClip names which already exist on stage.

THis is how I am doing it but doe snot seem to work. I have 11 movieclips on stage. THeir instance names are "bmc1", "bmc2" and so on. All these BMC movie clips are inside a clip called "bars_mc". So this is how I am coding it right now.

var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn in myBtnArray){
    bars_mc.MovieClip(getChildByName(btn)).gotoAndPlay('open');
}

This does not work. I have tried doing:

this[btn]

That also didnt work. This is being coded in AS 3.0.

Need someone t0 help me figure out the right way to convert strings to MOvieclips.

Apprecuiate your help.

2
Why not populate your array with the movie clips instead of strings with their names?Amy Blankenship

2 Answers

3
votes

I think the problem is likely that you've added the MovieClip cast in the wrong place, as though there were a property MovieClip on the object bars_mc.

This example involves an extra line but should be a bit clearer:

var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn:String in myBtnArray) {
    var btnClip:MovieClip = bars_mc[btn] as MovieClip;
    btnClip.gotoAndPlay('open');
}

And a version closer to your original would be:

var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn:String in myBtnArray) {
    MovieClip(bars_mc[btn]).gotoAndPlay('open');
}
0
votes

You should just store the MovieClips in the Array like so :

var myBtnArray = [bars_mc.bmc1, bars_mc.bmc2, bars_mc.bmc3, bars_mc.bmc4, bars_mc.bmc5, bars_mc.bmc6, bars_mc.bmc7, bars_mc.bmc8, bars_mc.bmc9, bars_mc.bmc10, bars_mc.bmc11];
for each (var btn in myBtnArray){
    btn.gotoAndPlay('open');
}

Even better, if the buttons you have in that array are the ONLY children of bars_mc, why not just use the display lis of bars_mc instead of an Array like this :

for (var index:int = 0;index < bars_mc.numChildren;index++)
{
   var btn:MovieClip = bars_mc.getChildAt(index) as MovieClip;
   btn.gotoAndPlay('open');
}

However, if you want to go the route you currently have chosen, you can just do this :

var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn:String in myBtnArray) {
    MovieClip(bars_mc[btn]).gotoAndPlay('open');
}