Reputation: 95
Hello stackoverflow users, i have a question at witch i cant find answer and i really need to solve this small problem, the idea is with the same button im creating some circles, but right now i need each of them to have their own properties so during the program i can edit them, and i think if it is possible to make and Array with the properties of the Child, in my case the colour of the circle, and the text what is in it.
the code how i create the circles is :
if (i<9 && mouseX<400 && mouseY<350 && mouseX>15 && mouseY>15 && event.target.name!=add_s )
{
i++;
q=i;
var btn:Sprite = new Sprite();
btn.graphics.beginFill(0x0099FF, 1);
btn.graphics.drawCircle(mouseX, mouseY, 15);
btn.graphics.endFill();
cordX[i]=mouseX;
cordY[i]=mouseY;
btn.mouseEnabled=true;
var s:String = String(q);
btn.name=s;
var textField = new TextField();
textField.mouseEnabled=false;
textField.text = i;
textField.width = 10;
textField.height = 17;
textField.x = mouseX-5; // center it horizontally
textField.y = mouseY-8; // center it vertically
btn.addChild(textField);
this.addChild(btn);
}
My question is: Is it possible to make an array of Childs, so i can acces each circles parameters. Please help
For example instead of ---> btn.graphics.beginFill(0x0099FF,1); to be btn[1].graphics.beginFill(0x0099FF,1); where btn[1] is the first circle and in future so i can edit this parameters...
Upvotes: 0
Views: 55
Reputation: 1505
// create var for number of buttons you want to create
var totalNumberOfButtons:Number = 10;
// create array to store buttons
var buttonArray:Array = new Array();
// loop through array and create button params
for( var i:int = 0; i < totalNumberOfButtons; i++ )
{
q=i;
var btn:Sprite = new Sprite();
btn.graphics.beginFill(0x0099FF, 1);
btn.graphics.drawCircle(mouseX, mouseY, 15);
btn.graphics.endFill();
cordX[i]=mouseX;
cordY[i]=mouseY;
btn.mouseEnabled=true;
// you don't really need this name now since you'll be referencing your buttons though an array now
//var s:String = String(q);
//btn.name=s;
var textField = new TextField();
// you'll want to give your Textfield a name so you can reference it later
textField.name = 'tf_' + q;
textField.mouseEnabled=false;
textField.text = i;
textField.width = 10;
textField.height = 17;
textField.x = mouseX-5; // center it horizontally
textField.y = mouseY-8; // center it vertically
btn.addChild(textField);
// add created button to buttonArray
buttonArray[ i ].push( btn );
this.addChild(btn);
}
access your buttons in buttonArray:
var currentIndex:Number = 0; // use this variable to keep track of which index you'll need at any given time
var currentButton:Sprite = buttonArray[ currentIndex ];
you can get the text of your button textfields like:
var currentButtonText:String = buttonArray[ currentIndex ]['tf_' + currentIndex].text;
and set the text of your button textfields like:
buttonArray[ currentIndex ]['tf_' + currentIndex].text = 'hello world';
Upvotes: 1