[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

flash + create instance of movie clip from array

Posted on 2006-06-09
5
Medium Priority
?
2,334 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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
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 how to record live broadcast.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

656 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