|
-
Senior Member
This code example uses duplicateMovieClip to make rows and columns of buttons, from a single offscreen button, named 'myButton'.
code:
maxButtons = 11;
nbrColumns = 3;
leftMargin = 10;
topMargin = 50;
cellWidth = 150;
cellHeight = 50;
for (var i = 0; i < maxButtons; ++i)
{
duplicateMovieClip(myButton, "button_"+i,i+1);
var mc = eval("button_" + i);
var cellX = i % nbrColumns;
var cellY = Math.floor(i / nbrColumns);
mc._x = leftMargin + cellX * cellWidth;
mc._y = topMargin + cellY * cellHeight;
}
The lines of most interest are these two, which generate cell coordinates, from the single looping i variable.
var cellX = i % nbrColumns;
var cellY = Math.floor(i / nbrColumns);
We use the mod (%) operator to create a cycling X (column) variable that goes 0,1,2, 0,1,2, 0,1,2 etc.
Then we use integer division to determine the y (row) number.
Once you've determined the cell coordinates (x and y) then you can manipulate those to position your buttons into a grid formation, using cellWidth, cellHeight and the left and top margin values.
Last edited by jbum; 08-02-2004 at 06:48 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|