I’ve always had to come up with hacked math using additional variables in order to tile movieclips properly. By tiling I mean, positioning movieclips so their are a certain number in each line with each additional line being placed below the previous one, etc. So I finally decided to spend the time to figure out how to do it properly.
Here was my scenario… I had 8 boxes that needed to be created and positioned. They needed to be positioned next to each other from left to right with only 4 boxes in each row. Each new row needed to be just below the top row. I hope that makes sense. I knew the calculations had to include the % modulo operator in order to know when the next line was to come. For each iteration I wanted to come up with the following: for the ‘x’ coordinate the numbers should be [0,1,2,3,0,1,2,3] and for the ‘y’ coordinate the numbers should be [0,0,0,0,1,1,1,1]. I would multiply those numbers by the spacing amount and it should work. This is what I have…
var xspace:Number = 100; var yspace:Number = 50; var rowCount:Number = 4; for(var i = 0; i < len; i++) { var mc:MovieClip = this.createEmptyMovieClip("thisClip" + i + "_mc", i); mc._x = i%rowCount * xspace; mc._y = (i - i%rowCount) / rowCount * yspace; }
The ‘x’ coordinate is easy to come up with using the counter to start over every 4th iteration. The tricky part was the ‘y’ position, but it makes perfect sense if you think about it. Just subtract the modulo result from the counter and divide that by every 4th iteration. Anyways, I hope that helps someone out their. I can just simplify your code and make your life a little easier when you come across a movieclip tiling issue like I seem to always have.
January 15th, 2007 at 1:25 am
Nice work! len is undefined
And if you wanted to tighten it up just a wee bit more you could make a var out of “i%rowCount” one less calculation in each iteration. Your CPU will thank you!
var mod:Number = i%rowCount;
var mc:MovieClip = this.createEmptyMovieClip(â€thisClip†+ i + “_mcâ€, i);
mc._x = mod * xspace;
mc._y = (i – mod) / rowCount * yspace;
September 21st, 2007 at 7:02 pm
I have no idea what you just said! But I’m guessing it’s great! Nice work