I'm not that new to using Flash, but I mostly used it for making animations and I don't really use actionscript much but this time I wanted to try making layouts by code.
I'm trying to position square movieclips to form a grid like menu. I positioned the each of the movieclips all in code but I think perhaps there's a better and more efficient way of doing this.
The code is very basic but I'll post it anyway so you have an image of what I'm trying to make it look like. I'm not really good at explaining so, I apologize.
//1st row//
var btn1:MovieClip = new dBtn();
btn1.x = -210;
btn1.y = -90;
addChild(btn1);
var btn2:MovieClip = new dBtn();
btn2.x = btn1.x+70;
btn2.y = btn1.y;
addChild(btn2);
var btn3:MovieClip = new dBtn();
btn3.x = btn2.x+70;
btn3.y = btn2.y;
addChild(btn3);
//2nd row//
var btn4:MovieClip = new dBtn();
btn4.x = btn1.x;
btn4.y = btn1.y+70;
addChild(btn4);
var btn5:MovieClip = new dBtn();
btn5.x = btn4.x+70;
btn5.y = btn4.y;
addChild(btn5);
var btn6:MovieClip = new dBtn();
btn6.x = btn5.x+70;
btn6.y = btn5.y;
addChild(btn6);
//3rd row//
var btn7:MovieClip = new dBtn();
btn7.x = btn4.x;
btn7.y = btn4.y+70;
addChild(btn7);
var btn8:MovieClip = new dBtn();
btn8.x = btn7.x+70;
btn8.y = btn7.y;
addChild(btn8);
var btn9:MovieClip = new dBtn();
btn9.x = btn8.x+70;
btn9.y = btn8.y;
addChild(btn9);
Loops + simple math.
var buttonsList:Array = new Array;
for (var i:int = 0; i < 9; i++)
{
// You can omit () with "new" operator if there are no arguments.
var aBut:MovieClip = new dBtn;
// Value of i % 3 goes 0 1 2 0 1 2 0 1 2.
aBut.x = -210 + 70 * (i % 3);
// Value of int(i / 3) goes 0 0 0 1 1 1 2 2 2.
aBut.y = -90 + 70 * int(i / 3);
addChild(aBut);
buttonsList[i] = aBut;
}
Then to address each one of them you can use their indices from 0 to 8 respectively:
// Make the central one semi-transparent.
buttonsList[4].alpha = 0.5;