I've had to do this so many times and seen this question asked so many times as well...
This is an easy way of creating some cool effects, like this one for example.
Here's a simple script to create a grid (without the color effects):
-
var x0:Number = 0; // x position (upper left corner)
-
var y0:Number = 0; // y position (upper left corner)
-
var nr:Number = 3; // number rows
-
var nc:Number = 10; // number colums
-
var vd:Number = 50; // vertical distance between the mcs
-
var hd:Number = 50; // horizontal distance between the mcs
-
-
for (var i = 1; i <= nr; i++) {
-
for (var j = 1; j <= nc; j++) {
-
// names are assigned like positions in a matrix: square(i,j)
-
var mc = this.attachMovie("square", "square" + i + j, i + "" + j);
-
mc._x = x0 + hd * (j - 1);
-
mc._y = y0 + vd * (i - 1);
-
}
-
}
This piece of code creates a rectangular grid, with nr rows and nc colums, i.e., with nr*nc squares.
You can think of this grid like a nr*nc matrix.
download .fla
This is achieved using a nested for() loop inside another for(),
In this .fla you'll also find code that creates exactly the same grid, but assigns different instance names to the squares. This can be important, depending on the way you want to interact with the attached movie clips.
-
var x0:Number = 0; // x position (upper left corner)
-
var y0:Number = 0; // y position (upper left corner)
-
var nr:Number = 3; // number rows
-
var nc:Number = 10; // number colums
-
var vd:Number = 50; // vertical distance between the mcs
-
var hd:Number = 50; // horizontal distance between the mcs
-
var s:Number = 0; // variable to hold names and depths
-
-
for (var i = 1; i <= nr; i++) {
-
for (var j = 1; j <= nc; j++) {
-
s++;
-
// names are sequential: left to right, top to bottom
-
var mc = this.attachMovie("square", "square" + s, s);
-
mc._x = x0 + hd * (j - 1);
-
mc._y = y0 + vd * (i - 1);
-
}
-
}
In case you don't want to have nr*nc squares, or just want to see things done in a different way:
The second example uses the modulo operator - % - but places the squares in a strange order.
download .fla
The third example does the same without the modulo operator, and places the squares sequentially on the stage.
download .fla
In these last two examples you specify both the number of movie clips and the number of rows:
-
var x0:Number = 0; // x position (upper left corner)
-
var y0:Number = 0; // y position (upper left corner)
-
var nt:Number = 12; // total number of movie clips
-
var nc = 5; // number of columns
-
var vd:Number = 50; // vertical distance between the mcs
-
var hd:Number = 50; // horizontal distance between the mcs
-
-
for (var i = 1; i <= nt; i++) {
-
var mc = this.attachMovie("square", "square" + i, i);
-
var aprox = Math.floor((i - 1) / nc);
-
mc._x = x0 + hd * ((i - aprox * nc) - 1);
-
mc._y = y0 + aprox * vd;
-
}
In this example you obtain only 2 movie clips in the last row.
Here's a simple example interacting with the squares: fading them in and out:
-
var x0:Number = 0;
-
var y0:Number = 0;
-
var nt:Number = 12;
-
var nc = 5;
-
var vd:Number = 50;
-
var hd:Number = 50;
-
for (var i = 1; i <= nt; i++) {
-
var mc = this.attachMovie("square", "square" + i, i);
-
var aprox = Math.floor((i - 1) / nc);
-
mc._x = x0 + hd * ((i - aprox * nc) - 1);
-
mc._y = y0 + aprox * vd;
-
mc.useHandCursor = false;
-
// fade in
-
mc.onRollOver = function()
-
{
-
this.onEnterFrame = function()
-
{
-
if (this._alpha> 0) {
-
this._alpha -= 10;
-
} else {
-
this._alpha = 0;
-
delete this.onEnterFrame;
-
}
-
};
-
};
-
// fade out
-
mc.onRollOut = function()
-
{
-
this.onEnterFrame = function()
-
{
-
if (this._alpha <100) {
-
this._alpha += 10;
-
} else {
-
this._alpha = 100;
-
delete this.onEnterFrame;
-
}
-
};
-
};
-
}
I’ve been looking for an example of how to construct nested ‘for’ loops which position clips in a grid, this is just the primer I needed – thank you for sharing Nuno!! Excellent stuff. I’ve downloaded your examples and will go and have a play about – cheers!!