Welcome, Guest. Please login or register.
Did you miss your activation email?
05/22/12, 06:18
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  General
| |-+  Flash and AS 3 (Moderators: papachan, kofi addaquay)
| | |-+  accessing child trouble
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 Print
Author Topic: accessing child trouble  (Read 3851 times)
Pluda
Senior Programmer
****
Posts: 270


View Profile
« on: 11/11/08, 09:13 »

Hello,

simplifiing this, I've one sprite called mc and I'm attaching one textfield to it like this

Code:
var i:Number = 0;
while (i< arr.length) {
var mc:Sprite = new Sprite();
mc.name = arr[i];
//
var ta:TextField = new TextField();
ta.type = "input";
ta.text = arr[i];
ta.name = arr[i];
ta.border = true;
ta.borderColor = 0xcccccc;
ta.x = 1;
ta.y = 1;
ta.width = 100;
ta.height = 22;
mc.addChild(ta);
}

then I add a button and I need to be able of making an array with all the text user has inputed in the textfileds

how do I access those texts?

I'm tring like this, but it doesn't work...

Code:
var bt:Button = new Button();
bt.name = "inserir";
bt.setSize(150, 30);
bt.label = "inserir";
bt.x = 0;
bt.y = 200;
bt.addEventListener(MouseEvent.CLICK, criar_variaveis);
//
function criar_variaveis(e:Event) :void {
//trace(mc.numChildren);
//
for (var ii:Number = 0; ii < mc.numChildren; ii ++) {
trace(mc.getChildAt(ii).name);
//var nome:String = mc.getChildAt(ii).name;
trace(mc.getChildByName(arr[ii]));
// var target:TextField = TextField(mc.getChildByName(arr[ii])).text;
//trace(target);
}
}

thanks in advance

Pluda
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #1 on: 11/12/08, 11:21 »

Have mc some value? get you something in the trace?

Jorge
Logged

Pluda
Senior Programmer
****
Posts: 270


View Profile
« Reply #2 on: 11/12/08, 11:28 »

Hello,

mc has a pre defined name assigned by mc.name = arr;

var arr:Array = ["a", "b", "c", "d"];
var i:Number = 0;
while (i< arr.length) {
   var mc:Sprite = new Sprite();
   mc.name = arr;
}

Thanks
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #3 on: 11/12/08, 11:41 »

So inside the button callback, mc is the last one or even undefined

Jorge
Logged

Pluda
Senior Programmer
****
Posts: 270


View Profile
« Reply #4 on: 11/12/08, 11:54 »

sorry?, haven't untherstand your comment

mc is a sprite created inside a loop. each mc has as children one textfield

button is added to stage outside the loop, and I need to retrieve each mc -> textfield text
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #5 on: 11/12/08, 12:09 »

Quote
get you something in the trace?

Jorge
Logged

Pluda
Senior Programmer
****
Posts: 270


View Profile
« Reply #6 on: 11/12/08, 12:12 »

yes, i got this

d
null
conteudo_d
null


since my tes array is ["a", "b", "c", "d"] it seems the loop isn't working...
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #7 on: 11/12/08, 13:47 »

The creation loops finish with mc beeing a pointer to d
So the next time you refers mc, is pointing to d
To get a valid reference, you should loop trough the arr array to get the first reference

mc = getChildAtByName(arr[ii])

And the refer to the textfield inside each mc

Is just a matter to think about

Jorge
Logged

Pluda
Senior Programmer
****
Posts: 270


View Profile
« Reply #8 on: 11/13/08, 05:49 »

Hello Jorge,

but I get this ugly error when trying to do what you said.

Code:
function criar_variaveis(e:Event) :void {
//trace(mc.numChildren);
//
for (var ii:Number = 0; ii < mc.numChildren; ii ++) {
trace(mc.getChildAt(ii).name);
//var nome:String = mc.getChildAt(ii).name;
trace(mc.getChildByName(arr[ii]));
// var target:TextField = TextField(mc.getChildByName(arr[ii])).text;
//trace(target);
}

while (i < mc.numChildren) {
mc = getChildByName(arr[ii]);
trace(mc);
trace(mc.getChildAt(i).name);
i++;
}

//trace("texto do campo "+e.target);
}

1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:Sprite.
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #9 on: 11/13/08, 06:08 »

Since is an Sprite, cast it

mc = Sprite(getChildByName(arr[ii]));

Jorge
Logged

Pluda
Senior Programmer
****
Posts: 270


View Profile
« Reply #10 on: 11/13/08, 07:17 »

oh boy... I'm hatting as3

now it doesn't trace anything

Code:
for (var ii:Number = 0; ii < mc.numChildren; ii ++) {
//trace(mc.getChildAt(ii).name);
while (i < mc.numChildren) {
mc = Sprite(getChildByName(arr[i]));
// mc = getChildByName(arr[ii]);
trace(mc);
// trace(mc.getChildAt(i).name);
i++;
}
}
Logged
Pluda
Senior Programmer
****
Posts: 270


View Profile
« Reply #11 on: 11/13/08, 16:28 »

jorge, sorry for asking this,

can you just code 3 or 4 lines to example me a way of making this?

one sprite with 2 textfields inside it and an outside button retrieving each textField.text

how do we output one hierarquical (this is wrong english, I'm sure) structure of the sprite content? is that even possible?

Thanks!
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #12 on: 11/14/08, 03:10 »

I thing you have 3 main problems:

- Understand better DisplayList mechanism
- Clear idea of scope
- Clearer data structure

I encourage to read more about DisplayList, consider that is the basement for all AS3 stuff: http://www.flash-db.com/Board/index.php?topic=18754.msg79173#new

Jorge
Logged

Pluda
Senior Programmer
****
Posts: 270


View Profile
« Reply #13 on: 11/14/08, 05:36 »

Thanks for the file, I will studdy :-)

I don't know where you guys live, I'm in Portugal and here it is very difficult to find good technical books. I search and search and abaut Flash just find simple things like how to do an motion tween.

Thanks
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14600


View Profile
« Reply #14 on: 11/14/08, 05:55 »

Currently on Spain, but usually in Argentina. I just buy on Amazon and usually in english (spanish translations sucks, and probably the same for portuguese)

Jorge
Logged

Pages: [1] 2 Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!