<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>benwatts.ca &#187; actionscript3</title>
	<atom:link href="http://www.benwatts.ca/tag/actionscript3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benwatts.ca</link>
	<description>The crazy ramblings of designer/web developer Ben Watts.</description>
	<lastBuildDate>Sat, 02 Jan 2010 22:31:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>Creating Instances from the Library with Strings</title>
		<link>http://www.benwatts.ca/2009/12/20/creating-instances-from-the-library-with-strings/</link>
		<comments>http://www.benwatts.ca/2009/12/20/creating-instances-from-the-library-with-strings/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 18:52:25 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[actionscript3]]></category>

		<guid isPermaLink="false">http://www.benwatts.ca/?p=531</guid>
		<description><![CDATA[Problem: while working on an AS3 document you uncover an unusual situation in which you have a string class name that corresponds to something in the library. How does one do it? I&#8217;ve come across this a number of times and found myself dropboxing this snippet so I would always have it with me &#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: while working on an AS3 document you uncover an unusual situation in which you have a string class name that corresponds to something in the library. How does one do it? </p>
<p>I&#8217;ve come across this a number of times and found myself dropboxing this snippet so I would always have it with me &#8230;</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:540px"><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">import</span> <span class="kw6">flash.utils</span>.<span class="sy0">*</span>;<br />
<br />
<span class="coMULTI">/** <br />
&nbsp;* Takes a string classname, returns an instance of that class, if it exists. <br />
&nbsp;*/</span><br />
<span class="kw1">private</span> <span class="kw3">function</span> newInstanceFromString<span class="br0">&#40;</span>className<span class="sy0">:</span><span class="kw5">String</span><span class="br0">&#41;</span><span class="sy0">:*</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">try</span><span class="br0">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> classRef<span class="sy0">:</span><span class="kw5">Class</span> = <span class="kw7">getDefinitionByName</span><span class="br0">&#40;</span>className<span class="br0">&#41;</span> <span class="kw1">as</span> <span class="kw5">Class</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">new</span> classRef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span><span class="br0">&#40;</span> e<span class="sy0">:</span><span class="kw5">ReferenceError</span> <span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw7">trace</span><span class="br0">&#40;</span><span class="st0">'Error: there was a problem creating an instance of the &quot;'</span><span class="sy0">+</span>className<span class="sy0">+</span><span class="st0">'&quot; class. Check your library and linkage to ensure it<span class="es0">\'</span>s there.'</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw7">trace</span><span class="br0">&#40;</span>e<span class="sy0">+</span><span class="st0">&quot;<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>; <br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></div>
<p>See the AS3 livedocs for more information about <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName()">getDefinitionByName</a>, the super helpful method that makes it all work. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.benwatts.ca/2009/12/20/creating-instances-from-the-library-with-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
