flash + create instance of movie clip from array

Posted on 2006-06-09
Last Modified: 2012-06-27
I have an array of names, i want to dynamically create one instance of a movie clip for each name in the array, the movieclip is a simple rectangle box.  Also once the instance is created i want the name from the array to be displayed inside the rectangle

how would i do this.

Question by:dkilby
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 16874375

Assuming there is a label or textbox or dynamic text box called "myText" in the movieclip...

for (i=0; i<myArray.length; i++) {
    this.createClassObject(eval(myArray[i]), myArray[i] + "inst", 1, {_x:0, _y:0, ...});
    eval(myArray[i] + "inst").myText.text = myArray[i];

That should do it. This assumes that there are objects in your library with the names contained in "myArray".


Accepted Solution

madgett earned 500 total points
ID: 16875152
To do the whole thing dynamically in code you would use this approach (you can replace the values in the "myArr" with your array's values):

// a container for your rectangles
var rect_mc:MovieClip = this.createEmptyMovieClip("rect_mc", 0);
var myArr:Array = new Array("Rect 1", "Rectangle 2", "Rect 3");
// loop through array and create rectangle
for (var i = 0; i<myArr.length; ++i)
      // create dynamic movieclip
      var dynRect_mc:MovieClip = rect_mc.createEmptyMovieClip("dynRect_mc"+i, i);
      // create textfield
      dynRect_mc.createTextField("label_txt", 1, 0, 0, 0, 0);
      var label_txt:TextField = dynRect_mc.label_txt;
      label_txt.autoSize = true;
      // assign the text stored in the array to the textfield
      label_txt.text = myArr[i];
      // create rectangle background
      var bg_mc:MovieClip = dynRect_mc.createEmptyMovieClip("bg_mc", 0);
      // specify rectangle width/height
      var w:Number;
      var h:Number = 25;
      // calculate the width dynamically
      w = label_txt._width+20;
      // if you want each rectangle to have the same width then just set w = 50 or some other number
      with (bg_mc)
            // border
            lineStyle(1, 0x333333, 100);
            // color of the fill
            beginFill(0x006699, 60);
            lineTo(w, 0);
            lineTo(w, h);
            lineTo(0, h);
            lineTo(0, 0);
      // center text inside rectangle
      label_txt._x = (bg_mc._width-label_txt._width)/2;
      label_txt._y = (bg_mc._height-label_txt._height)/2;
      // finally, position the rectangle
      var offsetY:Number = 10;
      dynRect_mc._y = i*(dynRect_mc._height+offsetY);

Author Comment

ID: 16875181

the code works great is there anyway of add actionscript dynamically to each of the movie clips it would be the same for each one.

i want the ability to drag the rectangles around so i just want to add something like


to each movieclip



Expert Comment

ID: 16875309
Yeah, to do that you would add this code at the end (after: dynRect_mc._y = i*(dynRect_mc._height+offsetY);)

dynRect_mc.onPress = function()
dynRect_mc.onRelease = function()

Author Comment

ID: 16875349
works perfect, thanks for all your help

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question