Ultrashock Forums > Flash > Flash Professional
Re arrange duplicated movie clips

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes
1|2|3|> Page 1 of 3

#1
Bookmark and Share!
Re arrange duplicated movie clips
Old 2005-09-29

I am duplicating a moviclip x times determined by a variable called "total" then i arrange them to have a distance of 30 pixels between each other, the thing is that i whant them to arrange depending of the Stage.width. For example:
I have 12 duplicated movieclips but only 4 clips can fit on the Stage.width so i whant the others to place themselfs in the Stage.height to creat a grid, i hope i make myself clear, ill put my fla so you can take a look.

FLA IS HERE

Thanks.
www.rum-i.com
postbit arrow 92 comments | 4208 views postbit arrow Reply: with Quote   
Super Moderator
Anik is offline Super Moderator
seperator
Posts: 8,023
2004-07-21
Age: 26
Anik lives in Argentina
27
Anik's Avatar
seperator

Ultrashock Member Comments:
Ben Ben is offline Ben lives in United States 2005-09-29 #2 Old  
.. Like.

multiline Word Wrapping ?
But with movieclips ?
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2005-09-29 #3 Old  
my idea is like this.
for example:
i have 11 duplicated movieclips.
if my Stage.width is 550 then the movieclips arrange like this

if my Stage.width is 650 then the movieclips arrange like this


it has to calculate the Stage.width and calculate how many clips can fit in it perfectly if they cant fit then it re arranges them.
Reply With Quote  
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2005-09-29 #4 Old  
Here ye go

ActionScript Code:
  1. // duplicate given clips, and use that clip as offset to create a grid with
  2. function addClips(movieclip:MovieClip, nr:Number) {
  3.     // get the width, but keep 30 pixels distance in total
  4.     var mc_width:Number = movieclip._width + 15;
  5.     var mc_height:Number = movieclip._height + 15;
  6.     // calculate how many mc's fit over the total width
  7.     var columns:Number = Math.floor(Stage.width / mc_width);
  8.    
  9.     // add the clips
  10.     for (var i = 1; i < nr; i++) {
  11.         var depth:Number = movieclip._parent.getNextHighestDepth();
  12.         var mc:MovieClip = movieclip.duplicateMovieClip(movieclip._name + i, depth);
  13.  
  14.         var column:Number = i % columns;
  15.         var row:Number = (i - column) / columns;
  16.        
  17.         // use movieclip._x and y as offset and add the calculated values
  18.         mc._x = movieclip._x + column * mc_width;
  19.         mc._y = movieclip._y + row * mc_height;
  20.     }
  21. }
  22.  
  23. // add 12 duplicates of thumbs
  24. addClips(thumbs, 12);
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2005-09-29 #5 Old  
thanks Codemonkey ill give it a try !
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2005-09-29 #6 Old  
it works great ! thanks man, one more thing, how can i make it to rearrange each time the Stage.width changes ?
Reply With Quote  
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2005-09-29 #7 Old  
I didn't know you could even do that, but here's the script anyway.

ActionScript Code:
  1. // duplicate the clips, then snap them to the grid
  2. function addClips(movieclip:MovieClip, nr:Number) {
  3.     for (var i = 1; i < nr; i++) {
  4.         var depth:Number = movieclip._parent.getNextHighestDepth();
  5.         movieclip.duplicateMovieClip(movieclip._name + i, depth);
  6.     }
  7.     // create the grid
  8.     arrangeClips(movieclip, nr);
  9. }
  10.  
  11. // create a grid, using given clip as offset
  12. function arrangeClips(movieclip:MovieClip, nr:Number) {
  13.     // get the width, but keep 30 pixels distance in total
  14.     var mc_width:Number = movieclip._width + 5;
  15.     var mc_height:Number = movieclip._height + 5;
  16.     // calculate how many mc's fit over the total width
  17.     var columns:Number = Math.floor(Stage.width / mc_width);
  18.    
  19.     // add the clips
  20.     for (var i = 1; i < nr; i++) {
  21.         var mc:MovieClip = eval(movieclip._name + i);
  22.  
  23.         var column:Number = i % columns;
  24.         var row:Number = (i - column) / columns;
  25.        
  26.         // use movieclip._x and y as offset and add the calculated values
  27.         mc._x = movieclip._x + column * mc_width;
  28.         mc._y = movieclip._y + row * mc_height;
  29.     }
  30. }
  31. //
  32. addClips(thumbs, 12);
Just call arrangeClips everytime you want to update the grid (supply the duplicated clip and the same number each time).
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2005-09-29 #8 Old  
you'r the man Codemonkey ! it works just had to add a this.onEnterFrame, thanks alot !
Reply With Quote  
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2005-09-29 #9 Old  
always a pleasure!
Reply With Quote  
Ben Ben is offline Ben lives in United States 2005-09-30 #10 Old  
Thanks, monkey.
Excellent.
Reply With Quote  
InfinityI's Avatar InfinityI InfinityI is offline InfinityI lives in United Kingdom 2005-09-30 #11 Old  
Nice code codemonkey, to improve performance instead of using an onEnterFrame you could add a listener to the Stage broadcaster and listen for the onResize event, as follows:

ActionScript Code:
  1. var stage_lis = new Object();
  2. Stage.addListener(stage_lis);
  3. stage_lis.onResize = function() {
  4. arrangeClips(movieclip, nr);
  5. }
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2005-09-30 #12 Old  
InfinityI i tryed what you sayd and added the stage listener, but it seems like the for(); on the arrangeClips function is not being executed, because it doesnt trace, dont know whats the problem

here is the code

ActionScript Code:
  1. // Stage aligh
  2. Stage.align = "LT";
  3. Stage.scaleMode = "noScale";
  4. var stage_lis = new Object();
  5. Stage.addListener(stage_lis);
  6. stage_lis.onResize = function() {
  7.     trace("resize");
  8.     arrangeClips(movieclip, nr);
  9. };
  10. // duplicate the clips, then snap them to the grid
  11. function addClips(movieclip:MovieClip, nr:Number) {
  12.     for (var i = 0; i<nr; i++) {
  13.         var depth:Number = movieclip._parent.getNextHighestDepth();
  14.         movieclip.duplicateMovieClip(movieclip._name+i, depth);
  15.         var mc:MovieClip = eval(movieclip._name+i);
  16.         mc.f = i;
  17.     }
  18.     // create the grid
  19.     arrangeClips(movieclip, nr);
  20. }
  21. // create a grid, using given clip as offset
  22. function arrangeClips(movieclip:MovieClip, nr:Number) {
  23.     // get the width, but keep 30 pixels distance in total
  24.     var mc_width:Number = movieclip._width+15;
  25.     var mc_height:Number = movieclip._height+15;
  26.     // calculate how many mc's fit over the total width
  27.     var columns:Number = Math.floor(Stage.width/mc_width);
  28.     // add the clips
  29.     for (var i = 0; i<nr; i++) {
  30.         var mc:MovieClip = eval(movieclip._name+i);
  31.         //
  32.         var column:Number = i%columns;
  33.         var row:Number = (i-column)/columns;
  34.         // use movieclip._x and y as offset and add the calculated values
  35.         mc._x = movieclip._x+column*mc_width;
  36.         mc._y = movieclip._y+row*mc_height;
  37.         trace("arrange");
  38.     }
  39. }
  40. //
  41. addClips(my_clip, 12);
Reply With Quote  
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2005-09-30 #13 Old  
hmm, it's working fine here?
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2005-09-30 #14 Old  
the clips arrange perfectly at first but they dont re-arrange when the stage resizes... dont know whats happening...
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2005-09-30 #15 Old  
i got it going, thanks again codemonkey
Reply With Quote  
tribalab tribalab is offline 2006-04-17 #16 Old  
Originally posted by Codemonkey
I didn't know you could even do that, but here's the script anyway.
LOL!

U2 are the coolest, CODE - thank you for your help on this and the backend stuff too!

aaron
Reply With Quote  
nelson714 nelson714 is offline 2006-04-17 #17 Old  
how can make it change dynamiclly? when change the borswer size. the stage layout will change dynamiclly.

i saw FWA web site have this feature.
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2006-04-17 #18 Old  
look at the code CodeMonkey posted all you need to know its there
Reply With Quote  
tribalab tribalab is offline 2006-04-17 #19 Old  
ActionScript Code:
  1. // config
  2. Stage.align = "LT";
  3. Stage.scaleMode = "noScale";
  4.  
  5. // listener object
  6. var stage_lis = new Object();
  7. stage_lis.onResize = function ()
  8. {
  9. trace("new width: "+Stage.width);
  10. }
  11.  
  12. // add the listener
  13. Stage.addListener(stage_lis);

All you need to do is create an object, and give it a function for the onResize event, so when the stage changes size, your function is called. Then you can access the new size with the Stage.width, and Stage.height properties. Also dont forget to set the scaleMode and stageAlign
Reply With Quote  
nelson714 nelson714 is offline 2006-04-17 #20 Old  
still not work for me..
i have manuelly refresh the page then the layouit will change
Reply With Quote  
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2006-04-17 #21 Old  
I think you're missing something nelson. Put up some code and we'll take a look at it
Reply With Quote  
nelson714 nelson714 is offline 2006-04-17 #22 Old  
ActionScript Code:
  1. // config
  2. Stage.align = "LT";
  3. Stage.scaleMode = "noScale";
  4. // listener object
  5. var stage_lis = new Object();
  6. stage_lis.onResize = function() {
  7.     trace("new width: "+Stage.width);
  8.     trace("new height: "+Stage.height);
  9. };
  10. // add the listener
  11. Stage.addListener(stage_lis);
  12.  
  13. // duplicate the clips, then snap them to the grid
  14. function addClips(movieclip:MovieClip, nr:Number) {
  15.     for (var i = 1; i<nr; i++) {
  16.         var depth:Number = movieclip._parent.getNextHighestDepth();
  17.         movieclip.duplicateMovieClip(movieclip._name+i, depth);
  18.     }
  19.     // create the grid
  20.     arrangeClips(movieclip, nr);
  21. }
  22. // create a grid, using given clip as offset
  23. function arrangeClips(movieclip:MovieClip, nr:Number) {
  24.     // get the width, but keep 30 pixels distance in total
  25.     var mc_width:Number = movieclip._width+5;
  26.     var mc_height:Number = movieclip._height+5;
  27.     // calculate how many mc's fit over the total width
  28.     var columns:Number = Math.floor(Stage.width/mc_width);
  29.     // add the clips
  30.     for (var i = 1; i<nr; i++) {
  31.         var mc:MovieClip = eval(movieclip._name+i);
  32.         var column:Number = i%columns;
  33.         var row:Number = (i-column)/columns;
  34.         // use movieclip._x and y as offset and add the calculated values
  35.         mc._x = movieclip._x+column*mc_width;
  36.         mc._y = movieclip._y+row*mc_height;
  37.     }
  38. }
  39. //
  40. addClips(thumbs, 15);

anything wrong for my code

testing

when i change the borswer size. the stage layout never change unless i do the refresh manuelly.
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2006-04-17 #23 Old  
you need to add this
ActionScript Code:
  1. stage_lis.onResize = function() {
  2. arrangeClips(movieclip, nr);
  3.         trace("new width: "+Stage.width);
  4.         trace("new height: "+Stage.height);
  5. };
Reply With Quote  
tribalab tribalab is offline 2006-04-17 #24 Old  
i don't see the variable "movieclip", or "nr" being declared outside the arrangeClips function parameters?
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2006-04-17 #25 Old  
its declared when you create the grid
ActionScript Code:
  1. addClips(thumbs, 15);
Reply With Quote  
nelson714 nelson714 is offline 2006-04-17 #26 Old  
i'm added "arrangeClips(movieclip, nr)" on stage_lis.onResize
but still the same. why ?????????????????
Reply With Quote  
tribalab tribalab is offline 2006-04-17 #27 Old  
they are instance variables local to the function correct? That means that they would not be accessible outside the function, unless they are set into vars that exist outside the function..

try this:

ActionScript Code:
  1. // config
  2. Stage.align = "LT";
  3. Stage.scaleMode = "noScale";
  4. // listener object
  5. var stage_lis = new Object();
  6. stage_lis.onResize = function() {
  7.         trace("new width: "+Stage.width);
  8.         trace("new height: "+Stage.height);
  9. };
  10. // add the listener
  11. Stage.addListener(stage_lis);
  12.  
  13. // duplicate the clips, then snap them to the grid
  14. function addClips(movieclip:MovieClip, nr:Number) {
  15.         for (var i = 1; i<nr; i++) {
  16.                 var depth:Number = movieclip._parent.getNextHighestDepth();
  17.                 movieclip.duplicateMovieClip(movieclip._name+i, depth);
  18.         }
  19.         // create the grid
  20.         arrangeClips(movieclip, nr);
  21. }
  22. // create a grid, using given clip as offset
  23. function arrangeClips(movieclip:MovieClip, nr:Number) {
  24.         // get the width, but keep 30 pixels distance in total
  25.         var mc_width:Number = movieclip._width+5;
  26.         var mc_height:Number = movieclip._height+5;
  27.         // calculate how many mc's fit over the total width
  28.         var columns:Number = Math.floor(Stage.width/mc_width);
  29.         // add the clips
  30.         for (var i = 1; i<nr; i++) {
  31.                 var mc:MovieClip = eval(movieclip._name+i);
  32.                 var column:Number = i%columns;
  33.                 var row:Number = (i-column)/columns;
  34.                 // use movieclip._x and y as offset and add the calculated values
  35.                 mc._x = movieclip._x+column*mc_width;
  36.                 mc._y = movieclip._y+row*mc_height;
  37.         }
  38. }
  39. //
  40. var target = thumbs;
  41. var num = 15;
  42. addClips(target, num);
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2006-04-17 #28 Old  
if you created the clips like this
ActionScript Code:
  1. addClips(thumbs, 15);
then use the same parameters
ActionScript Code:
  1. stage_lis.onResize = function() {
  2.         arrangeClips(thumbs, 15);
  3. };
Reply With Quote  
nelson714 nelson714 is offline 2006-04-17 #29 Old  
finally...done !!
thanks anik
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2006-04-17 #30 Old  
glad you got it working !
Reply With Quote  
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2006-04-17 #31 Old  
I'm going to turn it into a neat class somewhere this week.
Reply With Quote  
tribalab tribalab is offline 2006-04-17 #32 Old  
yeah! let me know if i can do anything to help, I would love to give it an official test run
Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2006-04-18 #33 Old  
Originally posted by Codemonkey
I'm going to turn it into a neat class somewhere this week.
NICE !!
Reply With Quote  
HonestWeb's Avatar HonestWeb HonestWeb is offline HonestWeb lives in China 2006-12-18 #34 Old  
got it working here. it's great.

just wondering whether it can really work like theFWA, you know the gap between each thumb is adjustable according to the resized stage.width. And our gap between each thumb is set to a fixed number, is there a work around that? when the stage is resized, the gap between thumbs will also adjust themselves to make the thumbs on teh stage perfectly fit teh width of stage?

hope I got myself clear.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2006-12-18 #35 Old  
This should do the trick. It will only work if you are using the entire stage for your thumbnails though:

ActionScript Code:
  1. // number of tile columns
  2. var nX:Number = 4;
  3. // tile width
  4. var tW:Number = 200;
  5. // the space between tiles
  6. var spaceX:Number = (Stage.width - (nX * tW)) / (nX + 1) >> 0;
  7.  
  8. // number of tile rows
  9. var nY:Number = 5;
  10. // tile height
  11. var tH:Number = 200;
  12. // the space between tiles
  13. var spaceY:Number = (Stage.height - (nY * tH)) / (nY + 1) >> 0;
spaceX is the distance you need to space your tiles out horizontally, spaceY is the distance you need to space you tiles out vertically.

I haven't looked at all of the code in this thread but I imagine that you can work that^ code into your own.

Reply With Quote  
Anik's Avatar Anik Anik is offline Super Moderator Anik lives in Argentina 27 Creative Assets 2006-12-18 #36 Old  
Originally posted by Codemonkey
I'm going to turn it into a neat class somewhere this week.
we are still waiting
Reply With Quote  
HonestWeb's Avatar HonestWeb HonestWeb is offline HonestWeb lives in China 2006-12-19 #37 Old  
hi guys, found this on gotoAndlearn forum.

http://gotoandlearn.com/forum/viewto...9f2afec549f2b4
Reply With Quote  
emix2006 emix2006 is offline emix2006 lives in Uruguay 2006-12-19 #38 Old  
:)
that code is great!, once i needed something like that and i had to write it by myself, here is the code if you wanna take a look, is diferent from yours:
ActionScript Code:
  1. //number of columns
  2. var numcol = 4;
  3. //columns spacing
  4. var colspace = base_mc._width+20;
  5. //rows spacing
  6. var filaspace = base_mc._height+20;
  7. //number of elements
  8. var cant = 23 //XMLobject.childNodes[0].childNodes.length;
  9. for (var i = 0; i<cant; i++) {
  10.     base_mc.duplicateMovieClip("base_mc"+i, i);
  11.     this["base_mc"+i]._y = base_mc._y+Math.floor(i/numcol)*filaspace;
  12.     this["base_mc"+i].cuales = i+1
  13. }
  14. base_mc._visible = false;
  15. var xxx = Math.floor(i/numcol)+1;
  16. for (var r = 0; r<=xxx; r++) {
  17.     for (var i = r*numcol; i<numcol*numcol+cant; i++) {
  18.         this["base_mc"+i]._x = base_mc._x+i*colspace-colspace*numcol*r;
  19.     }
  20. }
make comments about it!

you can also download it here
Reply With Quote  
HonestWeb's Avatar HonestWeb HonestWeb is offline HonestWeb lives in China 2006-12-20 #39 Old  
I was busy last week, just got back on this code today. Splat, I must let you down, 'cause I didn't manage to work your code out into my own. AS is indeed hard for my brain.

What I want to archieve is this. The gap between thumbs are flexible as user resize the stage. And when the total extra gapx is greater than thumbs._width plus the minimal gapx (which I set to 15), we want add one column, otherwise if it's less than that we want to decrease one column.

So as a result, when users drag to resize and stage you can see that the gap between thumbs are changing. And as they are increasing or decreasing to certain level we will add or remove one column from the stage.

Hope I made myself clear.

I came up with this code. Again, it's now working. Somebody help me have a look?
ActionScript Code:
  1. Stage.align = "LT";
  2. Stage.scaleMode = "noScale";
  3.  
  4. var totalgapx:Number;
  5. var nowgapx:Number;
  6. var mingapx:Number = 15;
  7. var totalgapy:Number;
  8. var nowgapy:Number;
  9. var mingapy:Number = 15;
  10. var column:Number;
  11. var row:Number;
  12. var exgapx:Number;
  13. var exgapy:Number;
  14.  
  15.  
  16. //make some mcs to line up the stage
  17. function addClip(movieclip:MovieClip)
  18. {
  19.     for (var i=1; i<column * row; i++)
  20.     {
  21.         movieclip.duplicateMovieClip("movieclip" + i, _root.getNextHighestDepth());
  22.     }
  23.     //create the grid, yea
  24.     arrangeClip(mvoieclip, nr);
  25. }
  26.  
  27. //now the grid function
  28. function arrangeClip(movieclip:MovieClip)
  29. {
  30.     var mc_width:Number = movieclip._width + nowgapx;
  31.     var mc_height:Number = movieclip._height + nowgapy;
  32.     column = Math.floor(Stage.width / mc_width);
  33.     row = Math.floor(Stage.height / mc_height);
  34.     nowgapx = Math.floor((Stage.width - (movieclip._width * column)) / (column - 1));
  35.     nowgapy = Math.floor((Stage.height - (movieclip._height * row)) / (row - 1));
  36.    
  37.     totalgapx = nowgapx * (column - 1);
  38.     totalgapy = nowgapy * (row - 1);
  39.     exgapx = totalgapx - ((movieclip._width + mingapx) * (column - 1));
  40.     exgapy = totalgapy - ((movieclip._height + mingapy) * (row - 1));
  41.  
  42.     //add one column if the extra x gap is greater than movieclip._width plus mingapx
  43.     if(exgapx > (movieclip._width + mingapx))
  44.     {
  45.         column += 1;
  46.     }
  47.    
  48.     //decrease one column if the extra x gap is less than movieclip._width plus mingapx
  49.     if(exgapx < (movieclip._width + mingapx))
  50.     {
  51.         column -= 1;
  52.     }
  53.    
  54.     //add one row if the extra y gap is greater than movieclip._height plus mingapy
  55.     if(exgapy > (movieclip._height + mingapy))
  56.     {
  57.         row += 1;
  58.     }
  59.    
  60.     //decrease one row if the extra y gap is less than movieclip._height plus mingapy
  61.     if(exgapy < (movieclip._height + mingapy))
  62.     {
  63.         row -= 1;
  64.     }
  65.    
  66.     for (var i = 1; i<(column * row); i++)
  67.     {
  68.         var mc:MovieClip = eval("movieclip" + i);
  69.         // use movieclip._x and y as offset and add the calculated values
  70.         mc._x = movieclip._x + column * mc_width;
  71.         mc._y = movieclip._y + row * mc_height;
  72.     }
  73. }
  74.  
  75.  
  76. addClip(thumbs);
  77.  
  78. // listener object
  79. var stage_lis = new Object();
  80. stage_lis.onResize = function()
  81. {
  82.     arrangeClip(thumbs);
  83. }
  84. // add the listener
  85. Stage.addListener(stage_lis);
Reply With Quote  
HonestWeb's Avatar HonestWeb HonestWeb is offline HonestWeb lives in China 2006-12-25 #40 Old  
i know you guys are Christmasing
Reply With Quote  
1|2|3|> Page 1 of 3
Thread Tools
Display Modes Rate This Thread
Rate This Thread: