PDA

View Full Version : Naming MovieClip



Vincentius
February 26th, 2010, 01:01 AM
I'm trying to make it so I dynamically create MovieClips and I'm wondering how to do it in AS3 (I know AS2 I think).


sCharm1:letterA = new letterA;I am trying this, but this doesn't work... Any suggestions? I've been at this for days, any help would be greatly appreciated.



var alphabet:Array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");

for(var i = 0; i < 26; i++){

var sCharm[i]:("letter"+alphabet[i]) = new ("letter"+alphabet[i]);

}
Thank you for your help,

Vincent



P.S. I'm not including all the other code. The rest works correctly. I included enough to get the point across.

snickelfritz
February 26th, 2010, 01:26 AM
This creates a row of movieclips on the stage, each containing a textfield with a letter from variable alphabet.


var clipHolder:Sprite = new Sprite();
var clips:Array = new Array();

var alphabet:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

createClips(alphabet.length);

function createClips(n:int):void
{
var mc:MovieClip;
var t:TextField;
var padding:int = 10;

for (var i:int = 0; i < n; i++)
{
mc = new MovieClip();
t = new TextField();

t.text = String(alphabet.substr(i,1));
t.width = t.textWidth + padding;
t.x = clipHolder.width;

mc.addChild(t);
clipHolder.addChild(mc);
clips.push(mc);
}
addChild(clipHolder);
}

The letters can be individually loaded from the "clips" array.

Vincentius
February 26th, 2010, 02:52 AM
Thank you, and that is very helpful and I'll keep note of that. However, I suppose I wasn't clear enough. What I'm trying to do is load some symbols from my library (that are movieclips) and each name I've given (through linkage) was "letterA, letterB, letterC" (etc). So what I'm trying to do is make it so I can easily call them all. I have done it the long way before, but I'm trying to learn of this for more than just this instance. I'd like to know if it is possible for future applications of the method. Again, thanks.

andalusite
February 26th, 2010, 04:08 AM
I suggest you use getDefinitionByName.


var letterArray = new Array();

for(var f:int=0; f<26; f++){
var letterRef:Class = getDefinitionByName("Letter"+f) as Class;
var L:MovieClip = new letterRef();
letterArray.push(L);
addChild(L);
}
Although this requires you to rename your library clips as "letter0", "letter1" etc.

To keep the library names you have, you could use an array which holds the alphabet:


var letterArray = new Array();
var alphaArray = new Array("A","B","C", etc etc...............

for(var f:int=0; f<26; f++){
var letterRef:Class = getDefinitionByName("Letter"+alphaArray[f]) as Class;
var L:MovieClip = new letterRef();
letterArray.push(L);
addChild(L);
}

snickelfritz
February 26th, 2010, 05:30 AM
A technique I always use for stuff like is to place the graphics/text/bitmaps/whatever on sequential frames of a single MovieClip symbol in the library.
ie: A is on frame1, B is on frame2, and so forth.
Instead of 26 different MovieClip symbols with their own unique export class names and content, you only have 1 symbol to manage.
I give this symbol a class export name, for example "Letters".


var letters:Letters = new Letters();// this is just to obtain the totalFrames value for Letters
var lettersArray:Array = new Array();
var lettersHolder:Sprite = new Sprite();

createClips(letters.totalFrames);

function createClips(n:int):void
{
var mc:MovieClip;

for(var i:int = 0; i < n; i++)
{
mc = new Letters();
mc.gotoAndStop(i+1);// each pass through the loop advances the playhead 1 frame
mc.id = i;// the id number matches the array index of the symbol
mc.x = lettersHolder.width;
lettersArray.push(mc);
lettersHolder.addChild(mc);
}
addChild(lettersHolder);
}

andalusite
February 26th, 2010, 07:06 AM
snickelfritz
Nice. I will add that to my arsenal.