Skip navigation
buzzafetta
Currently Being Moderated

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

Dec 5, 2012 8:16 AM

Tags: #as3 #xml #movieclips

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>

 
Replies
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    Dec 5, 2012 8:56 AM   in reply to buzzafetta

    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();

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 5, 2012 9:41 AM   in reply to buzzafetta

    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;

              }

         }

    }

     
    |
    Mark as:
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    Dec 7, 2012 12:59 PM   in reply to buzzafetta

    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.

     
    |
    Mark as:
  • kglad
    72,260 posts
    Jul 21, 2002
    Currently Being Moderated
    Dec 7, 2012 5:00 PM   in reply to kglad

    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();

    }

     

     

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points