Arranging dynamic buttons on stage

I’m creating buttons dynamically depending on the number I get from a file. So far I can align the buttons either straight up and down having 1 per row or all across. I want to be able to add as many as I want per row. Here is the code Im using.


buttonNames = fmenuitems.split(",");
buttonHeight = 40;
//trace(fmenuitems)
i = 0;
while (i < menutxtcount) {
	
//	trace(buttonNames*)
	this.attachMovie("button", ("button" + i), i);
//	trace(i)
	this[("button" + i)]._x = 0;
this[("button" + i)]._y = i * buttonHeight + buttonWidth;

	this[("button" + i)].text = buttonNames*;
	this[("button" + i)].frame = i + 46;
//	trace(this[("button" + i)].frame = i + 45)
	
	i++;
}
stop();