3 Replies Latest reply: Apr 15, 2012 7:11 AM by kglad RSS

    add event listeners to child's child

    clammyclam

      Hi!

       

      I have this little class where I load up a loader and turn it into a movieclip (along with some other stuff).

      I also have this bigger class where I have a bunch of the little classes.  And in trying to lay them out properly, I use their width and height and stuff.

      But I guess since it takes time for the little classes to load up and convert to movieclip along with the other stuff,  when I access the width and height of them in the big class, they're all zero.

      What I'm doing now is kind of ugly where after I convert/add the movieclip,  I dispatch bubbling event in the smaller class, and literally add them up in the parent class until they're all done.

      What I tried doing was adding Event.ADDED to movieclip variable in the little classes, from the big class.

      like:

      littleclass1.mc.addEventListener(Event.ADDED,blah);

      littleclass2.mc.""

      But that didn't work and was just as ugly!

       

      So basically my question is... what do I do in either the child class or the parent class so that... I can just create it, add it, and have all the objects width and height and stuff ready right off the bat(no matter now many movieclips, loaders, sounds, sprites in the smaller class)?

       

      Like a dispatch "ok everything is all done here, now you can access my width and height accurately"

        • 1. Re: add event listeners to child's child
          kglad CommunityMVP

          you must wait until loading is complete to access width/height properties of the loaded content.  ie, use an Event.COMPLETE listener.

          • 2. Re: add event listeners to child's child
            clammyclam Community Member

            Thanks for the reply! Event.COMPLETE works for loading up the loader in the little class, but it wouldn't work for when adding the little class to the big class.  Using Event.ADDED kind of works but it will fire like 9 times
            Little Class - inbox.as

            package laptop
            {
                      import flash.display.Loader;
                      import flash.display.Sprite;
                      import flash.events.Event;
                      import flash.system.LoaderContext;
                      import flash.display.MovieClip;
            
                      public class inbox extends Sprite 
                      {
                                [Embed(source = '../assets/Inbox Button.swf', mimeType = 'application/octet-stream')]
                                public var inboxButtonClass:Class;
                                public var box:MovieClip = new MovieClip();
            
                                public function inbox(title:String) { 
                                          if (stage) init();
                                          else addEventListener(Event.ADDED_TO_STAGE, init);
                                }
            
                                private function init(e:Event = null):void 
                                {
                                          var load:Loader = new Loader();
                                          load.loadBytes(new inboxButtonClass(), new LoaderContext());
                                          load.contentLoaderInfo.addEventListener(Event.COMPLETE, loadDone);
                                }
                                private function loadDone(e:Event):void {
                                          box = MovieClip(e.currentTarget.content);
                                          addChild(box);
                                              dispatchEvent(new Event("boxDone", true));
            
                                }
              }
             }
            

             

             

            Big Class - Email.as

             

             

            package laptop
            {
                      import flash.display.Loader;
                      import flash.display.Sprite;
                      import flash.events.Event;
                      import flash.system.LoaderContext;
                      import flash.display.MovieClip;
            
                      public class email extends Sprite 
                      {
                                [Embed(source = '../assets/Email Interface P1.swf', mimeType = 'application/octet-stream')]
                                private var bgClass:Class;
                                private var bg:Loader = new Loader();
                                public var ans1:laptop.inbox = new laptop.inbox("1");
                                public var ans2:laptop.inbox = new laptop.inbox("2");
                                public var ans3:laptop.inbox = new laptop.inbox("3");
                                private var cnt:int = 0;
            
                                public function email() {
                                          if (stage) init();
                                          else addEventListener(Event.ADDED_TO_STAGE, init);
                                }
            
                                private function init(e:Event = null):void 
                                {
                                          bg.loadBytes(new bgClass(), new LoaderContext());
                                          bg.contentLoaderInfo.addEventListener(Event.COMPLETE, bgDone);
                                }
            
                                private function bgDone(e:Event):void {
                                          addChild(bg);
                                          bg.x = stage.stageWidth / 2 - bg.width / 2;
                                          bg.y = stage.stageHeight / 2 - bg.height / 2;
                                          addChild(ans1); addChild(ans2); addChild(ans3);
            
                                          //DOESNT WORK
                                          ans1.addEventListener(Event.COMPLETE, ansCnt);
                                          ans2.addEventListener(Event.COMPLETE, ansCnt);
                                          ans3.addEventListener(Event.COMPLETE, ansCnt);
            
                                          //KIND OF WORKS SOMETIMES IF I SET CNT >= 3 but not ==3
                                          ans1.addEventListener(Event.ADDED, ansCnt);
                                          ans2.addEventListener(Event.ADDED, ansCnt);
                                          ans3.addEventListener(Event.ADDED, ansCnt);
            
                                          //DOESNT WORK
                                          ans1.box.addEventListener(Event.COMPLETE, ansCnt);
                                          ans2.box.addEventListener(Event.COMPLETE, ansCnt);
                                          ans3.box.addEventListener(Event.COMPLETE, ansCnt);
            
                                          //DOESNT WORK
                                          ans1.box.addEventListener(Event.ADDED, ansCnt);
                                          ans2.box.addEventListener(Event.ADDED, ansCnt);
                                          ans3.box.addEventListener(Event.ADDED, ansCnt);
            
            
                                               //WORKS
            
                                               addEventListener("boxDone", ansCnt);
            
                                }
            
                                private function ansCnt(e:Event):void {
                                          cnt++; if (cnt >= 3) { doAns();}
                                }
                                private function doAns():void {
                                          ans3.x =ans2.x=ans1.x= bg.x+bg.width / 2 - ans3.width / 2;
                                          ans3.y = bg.y + bg.height - ans3.height;
                                          ans2.y = ans3.y - ans2.height;
                                          ans1.y = ans2.y - ans1.height;
                                }
                      }
            
            }
            
            

             

            I comment the listeners out I don't have em all sitting there at same time.  I also tried addChild(ans123) after adding listeners.  The only one that works remotely is Event.ADDED but it will fire way more than 3 times.  Even if I create 3 different functions for the listeners like ansCnt1, ansCnt2, ansCnt3 and then do ans1.removeEventListener(Event.ADDED,ansCnt1), ect.  Sometimes it will work and then other times it will say the count is 9.  It makes no sense >.<  Is there something I can do where I don't have to count at all?  What am I doing wrong for Event.COMPLETE?  Why does Event.ADDED fire so many times?Is there some way where I don't have to use dispatchEvent, I'm still kind of noobish and think I rely on it wayyy too much.

            • 3. Re: add event listeners to child's child
              kglad CommunityMVP

              what is new laptop.inbox("1")?

               

              if that's a library symbol that contains a component, then use the render event, or that contains shapes in a frame other than 1 that contribute to the width/height you're trying to access, then dispatch an event from a frame other than one, use a callback function.

               

              if that's a class in which you construct objects, then dispatch and event or use a callback function, when the width and height are ready to be accessed.