Solved

flash + create instance of movie clip from array

Posted on 2006-06-09
5
2,329 Views
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.

thanks
0
Comment
Question by:dkilby
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:muxxter
ID: 16874375
hmm:

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".

Cheers
MuXx
0
 
LVL 7

Accepted Solution

by:
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);
            endFill();
      }
      // 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);
}
0
 

Author Comment

by:dkilby
ID: 16875181
madgett,

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

on(press){
   this.startDrag();
}

to each movieclip

thanks

0
 
LVL 7

Expert Comment

by:madgett
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()
{
      this.startDrag();
}
dynRect_mc.onRelease = function()
{
     this.stopDrag();
}
0
 

Author Comment

by:dkilby
ID: 16875349
works perfect, thanks for all your help
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

776 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