views:

181

answers:

1

I'm adding an array of sprites, each with an associated textfield.

When the sprite is clicked (or the textfield -- either one, though I want the cursor to be a hand) all I want to do is to grab the text. (e.g. "One" in the example below).

It ought to be possible to do it with dot notation, using the sprite's name, but that doesn't work. That is, Sprite_1.textField_1.text doesn't work.

When I click the sprite, I can add an event listener, but the target is then the sprite, and the sprite object does not contain the textfield object.

Extremely frustrating and ought to be simple... anyone know how to do this?

for (var i : int = 0;i < 5; i++) 
{
 var myText:TextField = new TextField();
 myText.text = someText;
   //say "One" first time through, then "Two" second time thru, etc.  
 myText.name = "textField_" + i;
 mySprite.addEventListener(MouseEvent.CLICK, grabText); 

 var mySprite:Sprite = new Sprite();
 mySprite.graphics.lineStyle(2,0x000000);
 mySprite.graphics.beginFill(0xff0000, 1);
 mySprite.graphics.drawRect(0, 0, myText.width, myText.height);
 mySprite.graphics.endFill();
 mySprite.useHandCursor = true;
 mySprite.mouseChildren = false;
 mySprite.buttonMode = true;
 mySprite.name = "Sprite_" + i;
 mySprite.addEventListener(MouseEvent.CLICK, grabText);

Thanks!

A: 

You haven't given the full code and it's not clear if you are addChilding textfield to mySprite or mySprite to this. Assuming that you are doing it, you can use getChildByName method to access them (I would rather store them in arrays and access from there - but that might take a lot of redesigning). Be warned that getChildByName returns the first child with a matching name and hence fails for cases where there are multiple children with the same name.

var sprite:Sprite = Sprite(this.getChildByName("Sprite_1"));
var tf:TextField = TextField(sprite.getChildByName("textField_1"));
tf.text = "some other text";
Amarghosh