11 Replies Latest reply on Aug 2, 2007 11:09 PM by Marsoups

    AS 3.0 -> communicating between classes

    Marsoups
      Hi all,

      Here's a good question, that nobody seems to have any answers for... I've searched the net and there really isn't that much info at all, so I'm assuming that us AS 3.0 folks are doing a bit of 'trail blazing' , so to speak.

      Here's my problem :

      I would like to have a particularly modular interface, such that, the main movieclip can load in modular parts, and insert these into a frame and have this functioning as a 'window' within the flash. So the loader for the movieclip has to be dynamic. I will refrain from complicating matters any more and get to the point of my question :

      Let's say, you've created a class for a 'circle' movieclip, which is created in its very own .fla file, with its own class file (circle.as) that extends the movieclip class. Now let's consider this circle is one of the modules in the base movieclip which will dynamically load it.

      The circle movieclip loads, but now you want to communicate with the circle movieclip, and for example, query it and ask it what colour its circle is. We would surely create a function within the circle class, something like the following :
      public function getColor():Color{
      return circlecolor;
      };

      On the main movieclip, we would query this file that was loaded, and one might imagine the code to look like the following :
      public function objectLoaded(event:Event):void {
      trace("circleloaded! And Flash coders rule!");
      trace("The color of this new object is " + event.content.getColor());
      };

      Now my problem is, that I cannot get these to communicate at all!! From testing and debugging, it appears the circle movie won't even look at the stage as the root timeline (it will look at the loader as its personal root, I think!). Does anybody have any ideas of how one can get some sort of a communication channel running between movieclip classes that are loaded dynamically in this fashion ?


        • 1. Re: AS 3.0 -> communicating between classes
          Marsoups Level 1
          Still no replies on this....

          Does this mean it is not possible to load another movieclip with the loader and to be able to communicate with classes on this movieclip from the main timeline ?

          Can't be... there has to be an answer... Anybody had any thoughts that this isn't possible... Help point me in the right direction..

          Thanks.
          • 2. Re: AS 3.0 -> communicating between classes
            Marsoups Level 1
            Still no answers...

            This is not possible ?
            • 3. Re: AS 3.0 -> communicating between classes
              Marsoups Level 1
              I have changed the topic summary as it appears that this must be a bug with the system...

              Flash 2.0 there was never a problem talking to the functions of the file that loaded it.. So therefore can we honestly say that it's not worthwhile getting too deep into AS 3.0 before holes like these are ironed out ??

              Would appreciate some comments here...
              • 4. AS 3.0 -> communicating between classes
                Marsoups Level 1
                *bumps* ....

                Can anybody get this to work and tell me I've made a mistake.. How is it possible for classes to communicate when they are loaded in from another movie ?

                Anybody from Adobe with knowledge on this ?
                • 5. Re: AS 3.0 -> communicating between classes
                  Juankpro
                  Weird, I tried a very basic example and it worked
                  • 6. Re: AS 3.0 -> communicating between classes
                    SymTsb Level 2
                    where are you adding the event listener. when using the Loader class, there is a huge difference between...

                    EXAMPLE 1
                    var _ldr:Loader = new Loader();
                    _ldr.addEventListener( Event.COMPLETE, functionName );

                    and....

                    EXAMPLE 2
                    var _ldr:Loader = new Loader();
                    _ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, functionName );

                    when using the loader class, contentLoaderInfo is the object that receives the events not the loader itself. There are two different loading classes.... Loader and URLLoader each with its own benefits. To communicate with the content loaded into either and to add listeners properly, you have to use a different method. Example 1 works for URLLoader and example 2 works for Loader. I hope this helps. My guess is that these are probably the issues you're having but without code beyond your theoreticals, it's hard to say for sure.
                    • 7. Re: AS 3.0 -> communicating between classes
                      Marsoups Level 1
                      Thanks SysTsb , I actually do have the contentLoaderInfo in my code...

                      If I try to call a function in the class of one of the movieclips that is being loaded in, then I get the message
                      "1061: Call to a possibly undefined method dotest through a reference with static type flash.display:DisplayObject.."

                      So it's not what the event listener is attached to that is the problem......

                      • 8. AS 3.0 -> communicating between classes
                        Marsoups Level 1
                        quote:

                        Originally posted by: juankpro
                        Weird, I tried a very basic example and it worked


                        And you used the loader to load this file ?

                        Could you share the source with me on how you achieved this ( that would help me heaps - I've been struggling with this for days now....)

                        Remember this is loading an external .swf , it's not just referencing a movieclip that's placed on the stage at compile time or something..
                        • 9. Re: AS 3.0 -> communicating between classes
                          SymTsb Level 2
                          are you type casting the content being loaded. Many times (almost always), Flash has some serious issues when you don't type cast the return type you are expecting.
                          • 10. Re: AS 3.0 -> communicating between classes
                            Juankpro Level 2
                            Yep sorry I forgot:
                            you must cast the object to a MovieClip for it to work. Complete code:

                            the class Circle.as

                            package {
                            import flash.display.MovieClip;

                            public class Circle extends MovieClip{
                            var circlecolor:Number = 0xFF0000;

                            public function getColor():Number{
                            return circlecolor;
                            }
                            }
                            }

                            This class must be attached to the document (in my case loaded.swf), use the property inspector.

                            The following code goes inthe loader swf:

                            import flash.events.*;
                            import flash.display.*;
                            import flash.net.*;

                            var ldr:Loader = new Loader();
                            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onCircleLoaded);
                            ldr.load(new URLRequest("loaded.swf"));
                            addChild(ldr);

                            function onCircleLoaded(event:Event):void{
                            trace("The color is: " + MovieClip(event.target.content).getColor());
                            }

                            That's it
                            • 11. Re: AS 3.0 -> communicating between classes
                              Marsoups Level 1
                              HEy juankpro , champion....

                              Thanks so much for that, I wouldn't have worked this out on my own ....

                              I'm surprised you managed to pull that one off!! The MovieClip reference makes sense on reflection... Trouble is, it's not documented anywhere!!

                              Cheers again....