Actionscript 3 Acceso hijo de hijo pero por índice

So I gave a movieclip in my library which I have added to stage and it contains more components and I have been able to get the information about these using :

try
{
    for (i = 0; i < musicControls.numChildren; i++){
        trace ('\t|\t ' +i+'.\t name:' +  musicControls.getChildAt(i).name + '\t type:' + typeof ( musicControls.getChildAt(i))+ '\t' +  musicControls.getChildAt(i));      
    }
}
catch (error:Error)
{
    trace("Failed:", error.message);
}

The script above returned the infomation below. I want to ask is there a way of now accessing these children by index. for example one of the components within the movieclip is a progress bar how would I reference it? I tried using getChildByName but its not working and when looking for help everyone says using it is slow anyway. I am sorry if this is a basiuc question im not very good at as3.

|    0.  name:plyControl     type:object    [object Button]
|    1.  name:stopControl    type:object    [object Button]
|    2.  name:nextControl    type:object    [object Button]
|    3.  name:prevControl    type:object    [object Button]
|    4.  name:progressControl    type:object    [object ProgressBar]
|    5.  name:volControl     type:object    [object Slider]
|    6.  name:__id11_    type:object    [object MovieClip]
|    7.  name:__id12_    type:object    [object TLFTextField]
|    8.  name:instance71     type:object    [object MovieClip]
|    9.  name:vizControl     type:object    [object ComboBox]
|    10.     name:__id13_    type:object    [object MovieClip]
|    11.     name:__id14_    type:object    [object TLFTextField]
|    12.     name:instance74     type:object    [object MovieClip]
|    13.     name:__id15_    type:object    [object MovieClip]
|    14.     name:timeControl    type:object    [object TLFTextField]
|    15.     name:instance75     type:object    [object MovieClip]

Edit: or very good at grammer and spelling by the looks of it....

preguntado el 03 de mayo de 12 a las 16:05

You should rarely need to get a child by index or name you should hold a ref to the object when you create it. -

Well what I did was make a movieclip in the library that has all my music controls in it instead of having to individually add these to stage and then go to the troble of positioning each one and stying it etc, this just seems the quicker option to me. The only reason im adding them in the first place like this is because when I change frames if you don't remove childs when you leave the frame they stay there otherwise I would of just placed all my components manually on the stage. -

2 Respuestas

Is the following code not working?

var progressBar:ProgressBar = musicControls.getChildByName("progressControl") as ProgressBar;
if(progressBar)
    //  doStuff

You said getChildByName is not working what error message are you getting?

contestado el 03 de mayo de 12 a las 17:05

This is what i get when compiling: Scene 1, Layer 'Functions', Frame 1, Line 137 1046: Type was not found or was not a compile-time constant: ProgressBar. - Alguien

Looks like the definition of ProgressBar is missing. Is it a custom type? Flash control? Make sure you import ProgressBar. - Barış Uşaklı

Ok its working YAY! thanks man and you Atriace for explaining things about indexs. - Alguien

You're heading in the right direction, and in fact have proven you can by your demonstration. Any object instantiated on the stage from a library asset will always have the same index structure (it's basically a proxy to the original after all). Therefore, if you know what layer your progress bar is on, your reference would look like this:

myPointer = musicControls.getChildAt(4);

If you want to get the child of a child by index you could do:

musicControls.getChildAt(4).getChildAt(0);

And you can mix and match your operations in the dot.notation syntax, because (at every step ) the code is resolved from a function call and into the object it returns.

this.getChildByName("musicControls").progressControl.getChildAt(0)

I hope that clarifies things.

contestado el 03 de mayo de 12 a las 18:05

you know you have the myPointer set above is this somthing you refer to later when setting the progress bar so like using it: progressBar.setProgress(playbackPercent.value, 100); or am I not understanding? - Alguien

I think its somthing I am trying to problem because this: (musicControls.getChildAt(14).text); will not work but this: (musicControls.getChildAt(14).name); will so its because im am trying to access somthing not directly accesible the way I am trying? - Alguien

musicControls.getChildAt(14) will return the object at layer 14. Therefore, musicControls.getChildAt(14) se convierte en [object TLFTextField]. The TextField object has lots of properties, one of which is a name property, and it's name value is "timeControl". If you're trying to set the text value of that textfield, doing musicControls.getChildAt(14).text = "Hello World!"; will work, however, if all you want is that TextField, all you need is musicControls.getChildAt(14) - Atriace

En cuanto a los myPointer, as Neil pointed out, it's usually easier to use a reference than typing out the full path. So, for example, setting 10 different properties on the progress bar (ie., alpha, x, y, name, etc...) is a lot easier to do with a small pointer than with this.getChildByName("musicControls").progressControl.getChildAt(0) or whatever complex path you might need to use to get a reference to the thing you want to change. - Atriace

Thanks again but I just used the other answer I appreciate the time you took to explain this to me and its helpful just found it easier to declare varibles when I add it to stage than this way its nothing personal. - Alguien

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.