View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2006-11-04 #3 Old  
002 - Aligning Movie Clips
 
The following function will allow you to align movie clips horizontally or vertically. You can also set the amount of spacing between each movie clip.

ActionScript Code:
  1. function alignClips(clips:Array, alignment:String, spacing:Number):Void {
  2.     if (spacing == null) {
  3.         spacing = 0;
  4.     }
  5.     var hori:Boolean  = alignment == "horizontal";
  6.     var propA:String  = hori ? "_x" : "_y";
  7.     var propB:String  = hori ? "_y" : "_x";
  8.     var length:String = hori ? "_width" : "_height";
  9.     var value:Number  = clips[0][propA];
  10.     var i:Number      = 0;
  11.     var n:Number      = clips.length;
  12.    
  13.     while (i < n) {
  14.         clips[i][propA] = value;
  15.         if (i) {
  16.             clips[i][propB] = clips[i-1][propB];
  17.         }
  18.         value += clips[i++][length] + spacing;
  19.     }
  20. }
  21.  
  22. // Create an array and populate it with the movie clips
  23. // that we want to align.
  24. var myClips:Array = [mc1, mc2, mc3, mc4];
  25.  
  26. // Align the movie clips horizontally with 10px spacing.
  27. alignClips(myClips, "horizontal", 10);
  28.  
  29. // or.. Align the movie clips vertically with 2px spacing.
  30. // alignClips(myClips, "vertical", 5);
  31.