A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: how to align a duplicated movie clip

  1. #1
    Junior Member
    Join Date
    Apr 2004
    Posts
    23

    how to align a duplicated movie clip

    i'm a beginner at scripting and i'd love some help. i have a movie clip called "box" which i want to duplicate into 30 instances arranged in a 5x6 column. how do i do these (duplicate AND arrange)? please please please help. thanks!

  2. #2
    Senior Member
    Join Date
    Oct 2003
    Posts
    1,354
    Create 2 'for loops', one inside the other.
    'i' for rows, 'x' for columns, 'z' will increment from 1 to 30:

    code:

    for (i=1;i<6;i++) {
    for(x=1;x<7;x++){
    z=z+1;
    duplicateMovieClip (_root.box, "box"+z, z);
    _root["box"+z]._x = i*50; // where 50 is the horizontal spacing you want
    _root["box"+z]._y = x*50; // where 50 is the vertical spacing you want
    }
    }

    Last edited by DallasNYC; 04-02-2004 at 11:02 PM.

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here's the method I was writing simultaneously as the above post. Essentially the same, except I'm putting some stuff in constants at the beginning, and I'm assuming the clip to duplicate is already in the correct position, and can be used to determine the grid size.

    Code:
    // adjust numbers to your taste
    NBR_COLUMNS = 5;
    NBR_ROWS = 6;
    var clipToDupe = _root.myClip;
    
    COL_WIDTH = clipToDupe._width;
    ROW_HEIGHT = clipToDupe._height;
    LEFT_MARGIN = clipToDupe._x;
    TOP_MARGIN = clipToDupe._y;
    
    FIRST_LAYER = 10;
    
    
    var n = 1;
    for (var y = 0; y < NBR_ROWS; ++y) 
    {
      for (var x = 0; x < NBR_COLS; ++x) 
      {
        if (y == 0 && x == 0) {
          // first tile is already attached
          continue;
        }
        duplicateMovieClip(clipToDupe, "mc"+n, n+FIRST_LAYER);
        var mc = _root["mc"+n];
        mc._x = LEFT_MARGIN+x*COL_WIDTH;
        mc._y = TOP_MARGIN+y*ROW_HEIGHT;
      }
    }
    I should point out that I prefer to use attachMovie instead of duplicate clip, keeping the original clip in my library instead of on the stage.

    This way, I don't have to 'skip' the first tile, as I had to above. In order for this to work, the clip in the library needs to be assigned an export name, such as 'rootClip', which I use below:

    Code:
    // adjust numbers to your taste
    NBR_COLUMNS = 5;
    NBR_ROWS = 6;
    COL_WIDTH = 100;
    ROW_HEIGHT = 100;
    LEFT_MARGIN = 50;
    TOP_MARGIN = 50;
    FIRST_LAYER = 10;
    
    var n = 1;
    for (var y = 0; y < NBR_ROWS; ++y) 
    {
      for (var x = 0; x < NBR_COLS; ++x) 
      {
        var mc = _root.attachMovie("rootClip", "mc"+n, n+FIRST_LAYER);
        mc._x = LEFT_MARGIN+x*COL_WIDTH;
        mc._y = TOP_MARGIN+y*ROW_HEIGHT;
      }
    }
    - Jim
    Last edited by jbum; 04-02-2004 at 11:05 PM.

  4. #4
    Junior Member
    Join Date
    Apr 2004
    Posts
    23
    thanks for the help. now i have a variable called "x" that is a random whole number between 1 and 30. if variable x matches the number for one of the boxes, the visitor is shown a msg. i have a feeling that this can be done with an if loop but i'm not sure of the details. can anyone help?

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    You didn't specify how you intend to assign numbers to the boxes, but yes, you can do that.

    Code:
    var n = 1;
    for (n = 1; n <= NBR_ROWS*NBR_COLUMNS; ++n) 
    {
      var mc = _root["mc"+n];
      if (x == mc.number) {
         trace("show message here");
         break;
      }
    }

  6. #6
    Junior Member
    Join Date
    Apr 2004
    Posts
    27

    any to randomise this?

    this is kinda what im looking for im looking for a random appearance of each of the movieclips

    any ideas?

    thanks

    ds

  7. #7
    Junior Member
    Join Date
    Apr 2004
    Posts
    27
    am i missing something?

    cant get it to work, have a circle in a mc that i want to place in a grid to it randomly forms one circle at a time but not in an ordered fashion.

    the circle is set to be exported as the name circle

    have change the code to chose this rather than the default rootClip

    the code is on the first frame of the movie, not sure where to put it

    but nothing happens

    any clues?

    i am using Flash MX is that helps, pref not to use 2004, am happy with MX for my use

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center