PDA

View Full Version : How to reference to dynamically created sprites?



sumo
September 27th, 2007, 02:58 AM
Hello.

I have a problem with creating dynamically some sprites that act as buttons and then referencing to them later on. Basically I just create three instances of my own buttonClass that has some rollOvers and outs. I use a for loop for that. Later on I want to change let's say the second and third instances y coordinate. How do I reference to those instances? Here is my code:

private function createMainButtons()
{
var btnTextArrayMain:Array = new Array("first", "second", "third");
for(var i:int = 0; i < 3; i++)
{
btn = new buttonClass();
btn.name = "mainBtn" + String(i);
btn.txt_mc.teksti.text = btnTextArrayMain[i];
btn.addEventListener(MouseEvent.CLICK, bclick);
btn.x = 60;
btn.y = 150 + i * 20;
addChild(btn);
}
}

I tried to use mainBtn1.y = 100; as I set the name property for each btn "mainBtn" + String(i) but it seems that I can't use the name property for referencing to that instance.

Sumo

Dazzer
September 27th, 2007, 04:00 AM
Well, you must keep the references to the objects in some way.

The other way would be to get a reference to the parent object (the container you added the buttons to) and use getChildByName().

sumo
September 27th, 2007, 04:20 AM
Thank you! You saved my day!!! :D