Solved

flash + create instance of movie clip from array

Posted on 2006-06-09
5
2,328 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
works perfect, thanks for all your help
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
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.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now