dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Address movieclips using "name"+a number?

Hybrid View

  1. #1
    Member
    Join Date
    Mar 2004
    Posts
    51

    Address movieclips using "name"+a number?

    So say I've got a series of movieclips all named "movieclip##" where the ## is 01-10. Is there an easy way to concatenate a reference to a movieclip through a separate number variable? As in, a function that receives a two-digit number, and uses that two digit number with the base name of the movieclip to perform an action.

    Specifically, I'm looking to use tweener to perform an animation on each letter of a sentence that's been broken apart into different movieclips. If I have to write code that specifically references each letter we're talking 100+ lines/function, and I'm trying to keep my overhead down.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What you should do is put your clips in an array rather than relying on naming conventions.

    Code:
    var clips:Array = new Array();
    //code to push clips into array
    
    function doSomethingToClip(which:int):void{
      var clip:MovieClip = clips[which];
      //do stuff with clip
    }
    But, if you've placed all your named clips on stage by hand in the IDE, you can use the bracket syntax to get them. It's much easier if you don't have the leading 0.

    Code:
    var clips:Array = new Array();
    for (var i:int = 1; i <= 10; i++){
      clips.push(this["movieclip"+i];
    }
    If you do have the leading 0, then you seem to really like to make things difficult for yourself. You can use a function like this to get your padded string from an int. This specific function only works for positive numbers for 2 digits.

    Code:
    function twoDigitString(i:int):String{
      if (i < 10){
        return "0" + i;
      }else{
        return ""+i;
      }
    }

  3. #3
    Member
    Join Date
    Mar 2004
    Posts
    51
    Alright, thanks so much for your quick reply!

    I'm not at all attached to leading zeros, so they're out. I'm specifically looking at your second code example - that's basically building the array correct? So for the actual animation function, I could write a similar "for" statement and run through the entire array?

    It looks like this will save me a ton of time.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, exactly. The first two snippets together give you a skeleton for manipulating your clips by putting them in an array and processing that array. Be aware that arrays are 0 based, so if your clips are 1-based then you'll be off by one.

  5. #5
    Member
    Join Date
    Mar 2004
    Posts
    51
    OK, awesome, thanks again! Just a quick clarification now that I'm steeped in code.

    I've got a movieclip (text02) that contains all the iteratively named movieclips I want to address (a1-a255). The text02 movieclip is exported for actionscript and is being added to my class as text02a.

    Now how do I write my array creation code so that it references the a## movieclips inside the text02a movieclip? I get the for notation, but I'm confused when it becomes "this".

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In AS3, "this" always* refers to the instance in which the function is defined. So unless your code is in a method defined in that class, or in framescript on that movie, it won't be "this".

    *except when you explicitly assign a thisvalue when using function.apply or function.call, and then only on non-method functions.

  7. #7
    Junior Member
    Join Date
    Feb 2006
    Posts
    5
    This is all excellent advice, and I'd just like to chime in as someone who made the transition from 2 to 3 and had to do this stuff a lot.

    It's not always appropriate, but one thing to remember is that the display list more or less functions as an array. So if you're just dealing with sorting, sometimes just putting all your movieclips inside another clip and using the display list is the way to go.

    Just my 2 cents

  8. #8
    Member
    Join Date
    Mar 2004
    Posts
    51
    That's an intersting way to do that! Do things exist on the displaylist that are added to a movieclip in the flash UI?

    I've got 255 movieclips in a container movieclip that's being exported as a class. How would I make an array out of the displaylist of that movieclip?

    Thanks!

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The most straightforward way would be like this:
    Code:
    var clips:Array = new Array();
    for (var i:int = 0; i < container.numChildren; i++){
      clips.push(container.getChildAt(i));
    }
    You can then use Array.sort to sort by name if you feel so inclined.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center