12 Replies Latest reply on Nov 12, 2007 7:51 AM by joeflashTO

    Parent/Child communication in CS3

    dtmoore Level 1
      Hello -

      I have a movie setup that imports another external swf file. I am trying to get the two movieclips to communicate with eachother. I am able to call a function in the child movie from the parent movie, but I cannot get the child movie to call a function in the parent movie.

      parent.functionName(); results with an error #1061 call to a possibly undefined method.

      I'm really banging my head on my desk with this one, it seems that this should be very simple (it was in AS2 anyway) but I just cannot get it to work. Any help would be greatly appreciated.

      thanks
      ~Dave
        • 1. Re: Parent/Child communication in CS3
          clbeech Level 3
          try root, or level0, instead of parent.
          • 2. Re: Parent/Child communication in CS3
            kglad Adobe Community Professional & MVP
            the parent of your child is probably the loader. if the loader is a child of the timeline that contains your function use parent.parent. and you will probably need to type that object as a movieclip and you'll probably need to use a try..catch statement to prevent the compiler from complaining when you publish the external swf.
            • 3. Re: Parent/Child communication in CS3
              joeflashTO
              Make sure you get a load completion event (if using the MovieClipLoader) before calling your loaded clip's child method. Either place the function call in your onLoadComplete or onLoadInit event handler. if you're using the old getBytesLoaded==getBytesTotal (pseudocode) technique, make sure, again, that your asset has finished loading before you call its method.
              • 4. Re: Parent/Child communication in CS3
                dtmoore Level 1
                I've tried all those ideas, and can't seem to get any of them to work. I posted a quick example of what I'm trying to do at : http://www.verge-technology.com/parentTest.rar

                the code in my parent page is:

                //imports
                import flash.display.MovieClip;
                import flash.events.MouseEvent;

                var thisMovieClip:MovieClip;

                var thisLoader:Loader = new Loader();
                thisLoader.contentLoaderInfo.addEventListener(Event.INIT, doneLoading);

                thisLoader.load( new URLRequest("childClip.swf") );

                function doneLoading(evt:Event):void {
                thisMovieClip = MovieClip(thisLoader.content);
                stage.addChild(thisMovieClip);
                thisMovieClip.stop();
                }


                //button event listener
                parentButton.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);

                function onPressHandler(myEvent:MouseEvent){
                thisMovieClip.toggleChildBox();
                }

                function toggleParentBox(){
                if(parentBox.visible == true)
                {parentBox.visible = false}
                else{parentBox.visible = true}
                }



                and in the child movieclip:

                import flash.display.MovieClip;
                import flash.events.MouseEvent;

                childButton.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);

                function onPressHandler(myEvent:MouseEvent){
                try{
                MovieClip(parent).toggleParentBox();
                }
                catch(error:Error){
                trace(error);
                }
                }

                function toggleChildBox(){
                if(childBox.visible == true)
                {childBox.visible = false}
                else{childBox.visible = true}
                }



                I have tried using parent.parent, level0, root, and It always throws back an error. It doesn't seem to like the cast to the movieclip type very much either. Again, any help would be greatly appreciated. I'm fairly new to AS3 and it's a bit overwhelming at first.

                cheers
                ~Dave
                • 5. Re: Parent/Child communication in CS3
                  joeflashTO Level 1
                  Ah well. AS3 is another kettle of fish.

                  In your doneLoading event handler, access the loader instance method with

                  • 6. Re: Parent/Child communication in CS3
                    dtmoore Level 1
                    I added the code you suggested into the doneLoading event and I get the following error:

                    1061: Call to a possibly undefined method toggleParentBox through a reference with static type flash.display:Loader.

                    • 7. Re: Parent/Child communication in CS3
                      kglad Adobe Community Professional & MVP
                      add your loader's conent to the main timeline (not the stage) and continue to use parent in the child.

                      or, i think it may be preferrable to, add your loader to the main timeline, and use parent.parent in the child.
                      • 8. Parent/Child communication in CS3
                        dtmoore Level 1
                        to put it on the main timeline as opposed to the stage would I just use "root.addChild(thisMovieClip);" When I try this I get an error 1061 Call to a possibly undefined method addChild thorugh a reference with static type flash.display:DisplayObject.

                        Part of me thinks I tried to dive into this as3 a little too early, I program c# all the time but there is just so much new stuff going on here. Anyway appreciate the help guys, hopefully I'll get this figured out soon.

                        cheers
                        ~David

                        • 9. Re: Parent/Child communication in CS3
                          joeflashTO Level 1
                          Where is the toggleParentBox declared, and have you made sure to declare it as public (if it's in a class or MXML file)? This will only work if the method was declared in your Document class, your main timeline, or your Application MXML tag.
                          • 11. Re: Parent/Child communication in CS3
                            dtmoore Level 1
                            awesome, that code worked kglad, really appreciate all of the help guys. For anyone that runs into this later on my final code was:


                            //imports
                            import flash.display.MovieClip;
                            import flash.events.MouseEvent;

                            var thisMovieClip:MovieClip;

                            var thisLoader:Loader = new Loader();
                            thisLoader.contentLoaderInfo.addEventListener(Event.INIT, doneLoading);

                            thisLoader.load( new URLRequest("childClip.swf") );

                            var tl:MovieClip=this;
                            function doneLoading(evt:Event):void {
                            thisMovieClip = MovieClip(thisLoader.content);
                            tl.addChild(thisMovieClip);
                            thisMovieClip.stop();
                            }


                            //button event listener
                            parentButton.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);

                            function onPressHandler(myEvent:MouseEvent){
                            thisMovieClip.toggleChildBox();
                            }

                            function toggleParentBox(){
                            if(parentBox.visible == true)
                            {parentBox.visible = false}
                            else{parentBox.visible = true}
                            }



                            and


                            import flash.display.MovieClip;
                            import flash.events.MouseEvent;

                            childButton.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);

                            function onPressHandler(myEvent:MouseEvent){
                            try{
                            MovieClip(parent).toggleParentBox();
                            }
                            catch(error:Error){
                            trace(error);
                            }
                            }

                            function toggleChildBox(){
                            if(childBox.visible == true)
                            {childBox.visible = false}
                            else{childBox.visible = true}
                            }

                            • 12. Re: Parent/Child communication in CS3
                              joeflashTO Level 1
                              quote:

                              Originally posted by: joeflashTO
                              Where is the toggleParentBox declared, and have you made sure to declare it as public (if it's in a class or MXML file)? This will only work if the method was declared in your Document class, your main timeline, or your Application MXML tag.

                              LOL, duh, didn't notice toggleParentBox was declared in your code. All this time I thought you were trying to reference a method in your newly loaded SWF. :P