6 Replies Latest reply: Dec 7, 2012 5:00 PM by kglad RSS

    Is it possible to call a movieClip from the Flash library using XML?

    buzzafetta

      Instead of using "test_1.swf" or "test_2.jpg" in the <IMAGE> tags shown below, we are wanting to call a movieClip from the same library as the .FLA via XML.

       

      Is this possible using XML and AS3?

       

       

       

      < ?xml version="1.0" encoding="UTF-8"?>

       

      < all>

       

           < GROUP>

       

                <IMAGE>test_1.swf</IMAGE>

                <QUESTION>Question example #1</QUESTION>

                <OPTION1>Option A example</OPTION1>

                <OPTION2>Option B example/OPTION2>

                <OPTION3>Option C example</OPTION3>

       

                <IMAGE>test_2.jpg</IMAGE> 

                <QUESTION>Question example #2</QUESTION>

                <OPTION1>Option A example</OPTION1>

                <OPTION2>Option B example/OPTION2>

                <OPTION3>Option C example</OPTION3>

       

           < /GROUP>

       

      < /all>

        • 1. Re: Is it possible to call a movieClip from the Flash library using XML?
          kglad CommunityMVP

          yes, you can pass the class name as a string and then use:

           

          function stringToClassInstanceF(s:String):*{

          var C:Class=Class(getDefinitionByName(s));

          return new C();

          }

          • 2. Re: Is it possible to call a movieClip from the Flash library using XML?
            sinious Community Member

            Just a note on complex projects with SWCs containing the clips you want to load. I've found I cannot getDefinitionByName() to dynamically find a class in a SWC unless the compiler knew about it ahead of time. This was a huge pain in the butt. The only tedious workaround I found was to declare an instance of that type in the Main document class of that library linkage type. From then on, any other classes no matter how deep could see that class via getDefinitionByName().

             

            You didn't need to actually instantiate it, just mark a variable of that type. Otherwise the compiler does not compile that class properly and gDBN returns undefined. This is an annoying bug, really difficult to track down.. AAMOF I might open a bug request if I can reproduce this in the latest FP/AIR.

             

            e.g. Main class Main.as:

             

            package

            {

                 public class Main

                 {

                      public function Main()

                      {

                           // uselessly pre-declare all linkage IDs I'll need later so the

                           // compiler will know they exist

                           _initRefs();

                      }

             

                      protected function _initRefs():void

                      {

                          // good enough, the compiler knows it must link this class from

                           // whatever SWC has it.. without this any other class can't find

                           // it via getDefinitionByName

                           var blah:SomeLinkageID;

                      }

                 }

            }

            • 3. Re: Is it possible to call a movieClip from the Flash library using XML?
              buzzafetta Community Member

              To clarify we are looking for a way to load a specified symbol from a library, be it .fla, .swf or .swc using xml.

               

              test_1.swc - symbol_01

              test_1.swc - symbol_02 etc.

              • 4. Re: Is it possible to call a movieClip from the Flash library using XML?
                kglad CommunityMVP

                again (per 1), use:

                 

                function stringToClassInstanceF(s:String):*{

                var C:Class=Class(getDefinitionByName(s));

                return new C();

                }

                 

                and again (per 2), if you need to instantiate a class from a swc, also declare a class member in the fla.

                • 5. Re: Is it possible to call a movieClip from the Flash library using XML?
                  buzzafetta Community Member

                  Could you elaborate, maybe with links to examples?

                  • 6. Re: Is it possible to call a movieClip from the Flash library using XML?
                    kglad CommunityMVP

                    data.xml:

                     

                    < ?xml version="1.0" encoding="UTF-8"?>

                     

                    < all>

                     

                         < GROUP>

                     

                              <IMAGE>MC1</IMAGE>

                              <QUESTION>Question example #1</QUESTION>

                              <OPTION1>Option A example</OPTION1>

                              <OPTION2>Option B example/OPTION2>

                              <OPTION3>Option C example</OPTION3>

                     

                              <IMAGE>MC2</IMAGE> 

                              <QUESTION>Question example #2</QUESTION>

                              <OPTION1>Option A example</OPTION1>

                              <OPTION2>Option B example/OPTION2>

                              <OPTION3>Option C example</OPTION3>

                     

                         < /GROUP>

                     

                    < /all>

                     

                    // a.s.

                     

                    var urlLoader:URLLoader=new URLLoader();

                    urlLoader.addEventListener(Event.COMPLETE,completeF);

                    urlLoader.load(new URLRequest("data.xml"));

                     

                    function completeF(e:Event):void{

                    var xml:XML=XML(e.data);

                    var instance:*=stringToClassInstanceF(xml.GROUP.IMAGE[0]);

                    addChild(instance);

                    }

                     

                     

                    function stringToClassInstanceF(s:String):*{

                    var C:Class=Class(getDefinitionByName(s));

                    return new C();

                    }