<?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://benwatts.ca/tag/actionscript3/feed/" rel="self" type="application/rss+xml" />
	<link>http://benwatts.ca</link>
	<description>Designer and Frontend Developer in Ottawa, Canada</description>
	<lastBuildDate>Fri, 22 Jul 2011 19:00:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating Instances from the Library with Strings</title>
		<link>http://benwatts.ca/2009/creating-instances-from-the-library-with-strings/</link>
		<comments>http://benwatts.ca/2009/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 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.*;</span><br />
<br />
<span style="color: #3f5fbf;">/** <br />
&nbsp;* Takes a string classname, returns an instance of that class, if it exists. <br />
&nbsp;*/</span><br />
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> newInstanceFromString<span style="color: #000000;">&#40;</span>className<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:*</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">try</span><span style="color: #000000;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> classRef<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Class</span> = <span style="color: #004993;">getDefinitionByName</span><span style="color: #000000;">&#40;</span>className<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Class</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> classRef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span> e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ReferenceError</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'Error: there was a problem creating an instance of the &quot;'</span><span style="color: #000066; font-weight: bold;">+</span>className<span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">'&quot; class. Check your library and linkage to ensure it<span style="">\'</span>s there.'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#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://benwatts.ca/2009/creating-instances-from-the-library-with-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

