Reputation: 1555
Im trying to create a loop that will go though movie clip names allowing you to attach an image.
Here is my code so far:
private var mc:MovieClip;
private var instance:String;
public function showFriends(e:Event)
{
var str:String = e.target.data;
var num:uint;
var i:Number;
var rex:RegExp = /[\s\r\n]*/gim;
var friends_array:Array = [];
num = friends_array.length;
friends_array = str.split(',');
for(i=0; i<num; i++){
var fri_id = friends_array[i].replace(rex,'');
mc = "P"+i; // THIS IS THE PROBLEM LINE
instance = 'DP';// Sets the Instance Name
load_DP(fri_id);
}
}
I am trying to work out how i can add the number from the loop (var i) to the name of the MovieClip.
Currently this code does not work giving off an error:
Implicit coercion of a value of type String to an unrelated type flash.display:MovieClip.
Which i have worked out means that it is using a String name as a MC and therefor not working.
if i change it too:
mc = P1; // with no quotes
This works but obviously only uses one MC.
If you could help please let me know.
Thank You.
Eli
Upvotes: 0
Views: 2502
Reputation: 10325
If all of the MovieClip instances you're trying to access are children of the current component, you can use the following notation.
mc = this["P" + i];
If they are all children of some other component, you can also access them as...
mc = myComponent["P" + i];
Upvotes: 1
Reputation: 214
Check out DisplayObjectContainer's getChildByName method. If showFriends is in your Document class, then it should work as \
mc = this.getChildByName("P" + i);
This should work -* however *- this is relatively slow and not good practice. It would be better to add these MovieClips to the stage dynamically (in code, instead of dragging to the stage) so you already have a reference to them.
Upvotes: 1