Convert an XML node to a MovieClip
I have an xml document that I would like to use to show MovieClips in my .fla:
<linkedMovieClip>TestClip</linkedMovieClip>
In my .fla, I created a symbol called TestClip and select Linkage > Export for Actionscript and named it TestClip.
My code in my Document class traces the xml:
var t:*= getDefinitionByName(String(slideItem.linkedMovieClip)) as Class;
var linked:MovieClip = new t();
trace("linked is..."+ linked); // outputs [Object TestClip];
开发者_Go百科However, when I compile, I am getting an Error #1065.
ReferenceError: Error #1065: Variable is not defined.
at global/flash.utils::getDefinitionByName()
I searched around and many sites recommended including the following imports:
import flash.utils.getDefinitionByName;
import TestClip;
And I included the following dummy variable:
public var _dummyClip:TestClip;
However, I am still getting an error message. When I check the debugger it's from this line:
var t:*= getDefinitionByName(String(slideItem.linkedMovieClip)) as Class;
Can anyone advise?
Your code looks okay. Try creating an instance of TestClip
in your program to make sure that it is compiled into the SWF. Trace out the fully qualified name and make sure it is indeed TestClip
var _dummyClip:TestClip = new TestClip();
trace(flash.getQualifiedClassName(_dummyClip));//what does it trace?
With the answer above, I see my problem now.
My XML looked like:
<items>
<item><linkedMovieClip>TestClip</linkedMovieClip></item>
<item><linkedMovieClip>TestClip</linkedMovieClip></item>
<item><linkedMovieClip>TestClip</linkedMovieClip></item>
<item></item>
<item><linkedMovieClip>TestClip</linkedMovieClip></item>
<item></item>
</items>
i.e. some items had nodes, some did not.
When I use a for each (var item:XML in itemList)
loop to iterate through my XML nodes () the nodes that do not contain the node fail and return the Reference Error (since there is no node called "linkedMovieClip".
If I use a if statement to check for the existence of the node, it works:
if (xmlListname.linkedMovieClip != undefined) {
trace(String(xmlListname.linkedMovieClip));
var c:* = getDefinitionByName(String(xmlListname.linkedMovieClip)) as Class;
var mc:MovieClip = new c();
trace(mc);
}
精彩评论