13 Replies Latest reply on Aug 2, 2009 10:41 AM by kglad

    Accessing components nested inside a movie clip instantiated at runtime

    oneWonders

      I am having this problem with actionscript 2:

       

      I have a class that instantiates a movieclip from the library, eg:

      myBar:MovieClip = level0.AttachMovie("debugbar", "debugbar_mc", 999);
      

       

      myBar is an instance variable in the class.

       

      Now the important part - debugbar in the library is a movieclip that contains some components - buttons and textInputs. The problem comes when trying to access these components.

       

      I would have thought that this would work:

      myBar.input_txt.text = "hello";

       

      in order to set the text of "input_txt", the instance name of a TextInput component on the timeline of the movie clip (frame 1 of a 1 frame mc).

       

      This does not work. Infact, I cannot access any specific "component" properties - they come back undefined. I cannot added event handlers for the component events either.

       

      I can however set and retrieve MovieClip properties for the "input_txt", such as _x. However there is one added strange thing with this too - setting _visible to false doesn't seem to work (however perhaps a component by default overrides this).

       

      I tried casting it to a component, such as:

      var temp:TextInput = TextInput(myBar.input_txt);
      trace(temp);
      

       

      which gave "temp" as null. Without casting, it gives the path to correctly.

       

      It almost seems like the components are somehow broken when trying to access them this way - or that they cannot be accessed this way?

       

      The thing is, I was able to access all of this before, when the code to do it was placed on the timeline (frame 1, the only frame) of the debugbar itself, where the components were placed at authortime.

       

      I need to have it in a class though, as I need to pass in certain objects that need to be accessed by the mc. I am rather baffled as to why all this doesn't work.

       

      Thanks for your time, cheers

        • 1. Re: Accessing components nested inside a movie clip instantiated at runtime
          kglad Adobe Community Professional & MVP

          components need some time to initialize.  there's an event that fires when they're ready but i don't remember what it is.

           

          you can use setTimeout() to reference your component(s) after 1 or 2 hundred ms and that should work, too.  i'll think about the event and let you know if i can recall it.

           

          rothrock pointed it out to me 1 or 2 weeks ago but i don't remember which thread that's in.

          1 person found this helpful
          • 2. Re: Accessing components nested inside a movie clip instantiated at runtime
            oneWonders Level 1

            Ah thanks, that confirms the problem If you could find out abut the event that would be really good - i had a little search but couldn't find anything.

             

            I wonder why it would work without having to "wait for the components to initialise" if you put the code on the timeline of the container mc (debug_bar)? - It's only 1 frame :S but probably it was loop playing that movie clip cos i didn't put a stop action at the top of the code, and then redoing parts of the code every time?? To be honest I don't really know how timeline code works haha.

             

            It turns out that you have to do: _global.setTimeout in classes, because setTimeout isn't in the intrinsic class definition files or something - whatever that means.

             

            Thanks for the help - if you could find the event that would be great as knowing by evnts is what I would like to do - otherwise it's just guesswork.

            • 3. Re: Accessing components nested inside a movie clip instantiated at runtime
              kglad Adobe Community Professional & MVP

              the solution i saw must have been for as3.

               

              for as2, i think you can use an onLoad() method:

               

              yourMC.yourcomponent.onLoad=function(){

              // do whatever.  your component is ready.

              }

              • 4. Re: Accessing components nested inside a movie clip instantiated at runtime
                oneWonders Level 1

                Ah yes I was just about to say the same thing - the Movie Clip onLoad thing seems to work, to tell you when it's done. Strange, because I would have thought it would have been the: UIObject.load event that would have done it - but that doesn't seem to get fired!.

                It seems I (probably by accident) tried the Movie Clip onLoad event by mistake - and with lots of delegating I managed it.

                 

                So that's nice, it all works now, thank you very much

                 

                What would you do to do the same thing but loading from an swf - if i right clicked on the debug_bar in the library and exported to swf. I tried loading it with mc loader and waiting for it to load and then tried to do things with it's components but it all got a bit strange and messy.

                Just that this would mean that people wouldn't have to have the debug_bar or the components in their library and would just need the swf I exported.


                Any ideas?

                 

                Thanks for you time

                • 5. Re: Accessing components nested inside a movie clip instantiated at runtime
                  kglad Adobe Community Professional & MVP

                  to determine when a loaded swf is ready to be reference (and the objects defined on its first frame are ready) use preloader code or the onLoadInit() method of the moviecliploader class.

                  1 person found this helpful
                  • 6. Re: Accessing components nested inside a movie clip instantiated at runtime
                    oneWonders Level 1

                    As yes sorry, I didnt make myself clear - by "and wait for it to load" i meant"onLoadInit". Once that has happened though, would the components inside be loaded? Or would I then have to set onLoad handlers to them? hmmm - i tired both approaches but neither worked - although I may have messed things up with all the even handlers and delegates being thrown around.

                     

                    Mind you, I am not really sure of the "movie clip" tree of the swf - I generated it by right clicking on the mc i was using originally and clicking "export swf".

                    I was assuming that that mc becomes _level0 and that inside that swf you would just reference the components directly?

                     

                    Thanks

                    • 7. Re: Accessing components nested inside a movie clip instantiated at runtime
                      kglad Adobe Community Professional & MVP

                      onLoadInit() is called when the first frame of the main timeline of the loaded swf is initialized and the first frame objects are ready for use.

                       

                      i'm not sure what you're trying to do when you export a movieclip as a swf.

                      1 person found this helpful
                      • 8. Re: Accessing components nested inside a movie clip instantiated at runtime
                        oneWonders Level 1

                        I was just thinking that it would be better as an swf, as then whoever was making a flash document could use it without having to import it and all the components it uses into their library.

                         

                        So from what you are saying, the components inside it should have initialised when onLoadInit for the main thing is fired

                         

                        Say a movie clip, the_mc has this structure:

                         

                        the_mc ---- input_txt (TextInput)

                                   ----- the_btn (Button)

                                   ----- bg (MovieClip)

                         

                        Where input_txt, the_btn and bg are all instance names of the children within the symbol identifier the_mc in the library.

                         

                        We right click on the_mc in the library and export it to an swf. We then remove the_mc and all the children symbols / components it uses from the library.

                        Then, we load the swf we created and attach it to a level or mc. Now, in order to reference the children again, would we do this:

                         

                        placeAttached.the_mc.input_txt

                        (so if attached to level1, then level1.the_mc.input_txt)

                         

                        or would we do this:

                         

                        placeAttached.input_txt

                        (so if attached to level1, then level1.input_txt)

                         

                        I am assuming it is the latter - though I am pretty sure I tried it and it didn't work - I will try again though

                         

                        Thanks again

                        • 9. Re: Accessing components nested inside a movie clip instantiated at runtime
                          kglad Adobe Community Professional & MVP

                          when onLoadInit() executes, the main timeline and the objects on the main timeline's first frame will be ready for use/initialized.  BUT nested objects, whose parents are initialized, are NOT ready for use/initialized.

                          1 person found this helpful
                          • 10. Re: Accessing components nested inside a movie clip instantiated at runtime
                            oneWonders Level 1

                            Hmm, but because the movie clip has been made into an swf, then surely it's components are now "things on the first frame of it's main timeline"?


                            At least, how do I find out the structure of an swf in terms of mc's and nestings?

                             

                            edit----

                             

                            ok, i did a for(attr in placeToAttach) loop to try and find out what there was. Turned out there was an "instance2" inside that and then inside that was my correctly named instances of all the objects. How odd!

                             

                            So it was like this:

                             

                            _level0.debug_bar_mc.instance2.input_txt

                             

                            so that corresponds to:

                             

                            placeAttached.the_mc.input_txt

                             

                            as _level0.debug_bar_mc is where it is attached and "instance2" appears to be the random instance name that flash has assigned the thing when I exported it to swf.

                            • 11. Re: Accessing components nested inside a movie clip instantiated at runtime
                              kglad Adobe Community Professional & MVP

                              when you export that movieclip flash creates and names a main timeline for it.

                              • 12. Re: Accessing components nested inside a movie clip instantiated at runtime
                                oneWonders Level 1

                                Yes, it appears so.

                                 

                                I would have preferred it itself to be it's own "main timeline" when exported - at least that makes more sense to me? Then you wouldn't have any of this "instance2" ambiguity.

                                 

                                I guess to do that I would have to place it on stage with nothing else and then "break it apart", set the stage size to the size of it and publish the movie. A bit of a faff.

                                • 13. Re: Accessing components nested inside a movie clip instantiated at runtime
                                  kglad Adobe Community Professional & MVP

                                  while there's some potential benefit to exporting a movieclip as a precompiled swf, it's so small i've never felt it was worth the trouble.