Figured I might make it easier to understand what I am trying to accomplish…
I have 3 sections in my flash file. Only 1 section shows at a time.
Via AS3 I am creating CheckBox objects, and placing them on individual sections. Instead of creating each checkbox individually, I want to be able to create a function, tell it how many checkboxes to create, and also what name to give each checkbox.
Actually looking at my code, I think I have something similar to what I am trying to achieve, but not sure if it is the right way to go about it…
For example I am using this code:
var tab1_gp2_sub1_Options:int = 10;
for (i=0; i<=tab1_gp2_sub1_Options; i++)
{
{
//Option Creation Loop
tab1_gp2_sub1_op = new CheckBox();
tab1_gp2_sub1_op.name = "tab1_gp2_sub1_op" + i;
addChild(tab1_gp2_sub1_op);
//Add Properties for Options
tab1_gp2_sub1_op.label = tab1_gp2_sub1_op_Labels[i];
tab1_gp2_sub1_op.width = 200;
trace(tab1_gp2_sub1_op.name);
}
};
What the above will do is create objects tab1_gp2_sub1_op0 through tab1_gp2_sub1_op9 and assign the above values to each one.
So Maybe I can do something like:
createVariables("tab1_gp2_sub1_op", 10);
function createVariables(InstanceName:String, vCount:Number):void{
for (i=0; i<=vCount; i++)
{
{
//Option Creation Loop
var newCheckbox:CheckBox = new CheckBox();
newCheckbox.name = InstanceName + i;
addChild(newCheckbox);
//Add Properties for Options
newCheckbox.label = "Checkbox: " + i;
newCheckbox.width = 200;
trace(newCheckbox.name);
}
};
}
Would the above function do the same job as the first For Loop I posted, but create a checkbox with the InstanceName I passed to the function?
If you’re going to be making a collection of values, you should use an Array (or better yet, a Vector).
So, for example, if you needed to make 10 check boxes, something like this would work.
// Somewhere that creates the variable in a place the function can find.
// Alternatively you could pass the variable to the function.
var checkboxes:Vector.<CheckBox> = new Vector.<CheckBox>();
function makeCheckBoxes() {
for(var i:int = 0; i < 10; i++) {
var newCheckbox:CheckBox = new CheckBox();
// Add it to the vector.
checkboxes.push(newCheckbox);
}
}
// access it somewhere
checkboxes[0].name = "hello";
If you aren’t familiar with Vectors, they’re essentially the same as Arrays, except they can only hold a single filetype (which you specify in the <>). Doing this allows them to have a pretty substantial performance boost. They have all the same functions otherwise as an array.