Solved

flash + create instance of movie clip from array

Posted on 2006-06-09
5
2,331 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
[X]
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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
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 live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

730 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