<?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>Flash Cove</title>
	<atom:link href="http://flashcove.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://flashcove.net</link>
	<description>Quality AS2 and AS3 Flash Tutorials!</description>
	<lastBuildDate>Thu, 23 Jun 2011 21:06:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Flash Historical Stock Chart Tutorial (Yahoo Data)</title>
		<link>http://flashcove.net/834/flash-historical-stock-chart-tutorial-yahoo-data/</link>
		<comments>http://flashcove.net/834/flash-historical-stock-chart-tutorial-yahoo-data/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 21:06:19 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[finance]]></category>
		<category><![CDATA[stock]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=834</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/834/flash-historical-stock-chart-tutorial-yahoo-data/">Flash Historical Stock Chart Tutorial (Yahoo Data)</a></p><p>&#160; I like investing in and following the stock market. I also like developing in Adobe Flash. What a great flash tutorial idea to develop a stock application! What I...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/834/flash-historical-stock-chart-tutorial-yahoo-data/">Flash Historical Stock Chart Tutorial (Yahoo Data)</a></p><p>&nbsp;</p>
<p>I like investing in and following the stock market.  I also like developing in Adobe Flash.  What a great flash tutorial idea to develop a stock application!  What I have got for everyone here today is an <strong>interactive chart in Flash that dynamically grabs stock prices and other data from the Yahoo finance feed</strong><em> </em>, parses, and then displays historical prices for any stock from it&#8217;s IPO (initial public offering) to present day.  Additionally, because of sandbox security restrictions, your flash player cannot access external sites such as yahoo.com, only items on the same hosting server. To bypass this security restriction, a couple of <strong>php proxys</strong> were also written and stored on my server, which act as middlemen for the flash application to grab ticker information from Yahoo icharts.</p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-9768577137874485";
/* FlashCove Text Banner */
google_ad_slot = "4850902026";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>The entire application is a couple hundred lines of code, but on the positive side, this is a pure actionscript 3 application, so no actual Flash IDE functionality,  external movieclips, etc.. was needed! Directly below is the source code for the tutorial in addition to a<strong> LIVE, WORKING demo </strong>of the stock chart! Enjoy this great Flash and Actionscript 3 tutorial, from the one and only, flash cove tutorial site!</p>
<blockquote><p><strong>Place both the flash file and two php scripts (BE SURE TO CHANGE FORMATTING FROM .TXT TO .PHP!) in the same directory/folder, and then run the &#8220;stock_chart.fla&#8221;</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/06/stock_chart.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/06/stock_chart.fla'>stock_chart.fla</a></div></div><div class='clear'></div><br />
<div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/06/get_stock.txt'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/txt-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/06/get_stock.txt'>get_stock.txt</a></div></div><div class='clear'></div><br />
<div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/06/get_stock_name.txt'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/txt-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/06/get_stock_name.txt'>get_stock_name.txt</a></div></div><div class='clear'></div></p>
</blockquote>
<p><strong>Here&#8217;s the final yahoo finance based stock graph, in a compiled flash program, for your enjoyment:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 600px;"><div id="efe-swf-1" class="efe-flash"><!-- --></div></div>

 You can trace the historical stock prices anywhere on the flash chart by tracing
 along with your mouse. I defaulted with the big blue chip Microsoft (MSFT),
 but you can search any stock listed on the market by typing it up in the
 top right corner, such as AAPL for Apple Inc, CSCO for Cisco, CMG for Chipotle,
 F for FORD, YHOO for Yahoo, ONTY for Oncothyreon (which, with Stimuvax, is
 a great stock right now), etc..., and then pressing enter.  Enjoy!
</pre>
<p>With that, lets get to work on the actual flash tutorial for this sweet stock application.</p>
<p>&nbsp;</p>
<p>Since there is a lot of code for this flash stock chart tutorial, I am only going to go over the actionscript source code in high detail. It is up to you to decipher the smaller facets within. I&#8217;ve lined the code with a few comments to help you identify where certain objects are laid out and created, but feel free to leave a comment below if you need any help or want to post some suggestions to me or others on the flash cove.</p>
<p>With that, let&#8217;s get to the actual development for this flash tutorial:</p>
<p><strong><em>INITIALIZATION</em></strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> arr<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">max</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> max_volume<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> increment_level<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> titleLoader<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=urlloader%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloader.html"><span style="color: #004993;">URLLoader</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> input_field<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> stock_line<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> volume_array<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> year_array<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> top_left<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">15</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> dimensions<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">800</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">300</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> displayed_data<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>This first part is pretty easy, we are just initializing some global variables in flash for our stock chart to use.</p>
<ul>
<li>An array to stock price numbers and data</li>
<li>variables for max stock price and maximum volume</li>
<li>for every pixel on the chart, how many days we advance in the price history (that&#8217;s our increment_level)</li>
<li><strong>titleLoader</strong> is our download wrapper for obtaining the tickers company name</li>
<li><strong>input_field</strong> is where we can type in new stock tickers to search</li>
<li><strong>stock_line</strong> is the actual stock price line on the screen, a flash sprite</li>
<li><strong>volume_array</strong> is the series of lines at the bottom of the stock chart indicating volume; all sprite lines.</li>
<li><strong>year_array</strong> are all the little text values indicating years on the bottom of the stock price chart (from IPO to current)</li>
<li><strong>top_left</strong> is a point marking where our chart begins on the screen</li>
<li><strong>dimensions</strong> obviously mark the width and height we want for the chart. This is scalable to whatever you like</li>
<li><strong>displayed_data</strong> is storage for our formatting setups</li>
</ul>
<p>And with that, let&#8217;s get to a little bit more initialization. Some actual sprites and graphics on the flash screen.</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">//initialization of all the little labels and formatting for the stock chart</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> current_stock<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a> = <span style="color: #990000;">&quot;MSFT&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> square<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>square<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">,</span>0x333333<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x111111<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_MOVE</span><span style="color: #000066; font-weight: bold;">,</span> show_popup<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> volume_square<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>volume_square<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
volume_square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">,</span>0x333333<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
volume_square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x111111<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
volume_square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">22</span><span style="color: #000066; font-weight: bold;">,</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
volume_square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_MOVE</span><span style="color: #000066; font-weight: bold;">,</span> show_popup<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> tracer<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>0x222222<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0xffcc00<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>tracer<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> tracer_line<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>0xFFE87C<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">+</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>tracer_line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> tracer_price<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>0x817339<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x111111<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">80</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> price_field<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">type</span> = <a href="http://www.google.com/search?q=textfieldtype%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfieldtype.html"><span style="color: #004993;">TextFieldType</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">DYNAMIC</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">border</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">selectable</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">80</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">multiline</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">wordWrap</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">background</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;MSFT&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> price_format<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = 0xFDD017<span style="color: #000066; font-weight: bold;">;</span> <br />
price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">size</span> = <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">font</span> = <span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">align</span> = <a href="http://www.google.com/search?q=textformatalign%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformatalign.html"><span style="color: #004993;">TextFormatAlign</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CENTER</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>price_field<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setTextFormat</span><span style="color: #000000;">&#40;</span>price_format<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>tracer_price<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> tracer_date<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>0x817339<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x111111<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> date_field<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">type</span> = <a href="http://www.google.com/search?q=textfieldtype%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfieldtype.html"><span style="color: #004993;">TextFieldType</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">DYNAMIC</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">border</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">8</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">selectable</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">80</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">multiline</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">wordWrap</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">background</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;MSFT&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> date_format<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = 0xFDD017<span style="color: #000066; font-weight: bold;">;</span> <br />
date_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">size</span> = <span style="color: #000000; font-weight:bold;">12</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">font</span> = <span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">align</span> = <a href="http://www.google.com/search?q=textformatalign%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformatalign.html"><span style="color: #004993;">TextFormatAlign</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CENTER</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>date_field<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setTextFormat</span><span style="color: #000000;">&#40;</span>date_format<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>tracer_date<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>All this scary looking stuff is just the box where our stock line goes, the box where the volume lines go down below, and then the vertical tracer line that  shows a specific stock price and the date it is recorded for.  We initialize colors, sizes, and positions, and then add the necessary event listeners that go along with each element in the flash tutorial.</p>
<p>The next part of learning for this flash stock tutorial, is the event listener for ourpopup / tracer linethat we initialized in the previous chunk of actionscript code above.</p>
<p><strong>POP-UP/TRACER EVENT LISTENER:<br />
</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> show_popup<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//these are the two little ovals that follow our vertical tracer line</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">&lt;</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> <span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">&gt;</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> possib_y<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">-</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>increment_level<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">max</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;$&quot;</span><span style="color: #000066; font-weight: bold;">+</span>arr<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">-</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>increment_level<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = arr<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">-</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>increment_level<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> price_format<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = 0xFDD017<span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">size</span> = <span style="color: #000000; font-weight:bold;">12</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">font</span> = <span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; price_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">align</span> = <a href="http://www.google.com/search?q=textformatalign%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformatalign.html"><span style="color: #004993;">TextFormatAlign</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CENTER</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer_price<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>price_field<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; price_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setTextFormat</span><span style="color: #000000;">&#40;</span>price_format<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; date_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setTextFormat</span><span style="color: #000000;">&#40;</span>price_format<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; tracer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = possib_y<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>If the mouse position is within our specified stock chart range, update the positioning for our tracer popup and all related data. Every time the tracer is updated, we also need to reflect that in the text formatting for the stock price.  That&#8217;s the code you see at the bottom of this function.</p>
<p>The next chunk of actionscript code, which is our largest and most complex, is the initial construction of the stock chart and graphs. We make a request to my php script on the flashcove.net server, it in turn makes a request to the yahoo finance servers, gets a file back called &#8220;<strong>table.csv</strong>&#8220;, and then that is fed back in to flash via a return variable to the <strong>URLLoader</strong> function.  Once that data is safe in our hands, we parse through, find price and volume maximums, break the dates in to meaningful chunks, and display on-screen.<br />
<strong><br />
CHART CONSTRUCTION:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> csvLoader<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=urlloader%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloader.html"><span style="color: #004993;">URLLoader</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=urlloader%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloader.html"><span style="color: #004993;">URLLoader</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">dataFormat</span> = <a href="http://www.google.com/search?q=urlloaderdataformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloaderdataformat.html"><span style="color: #004993;">URLLoaderDataFormat</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TEXT</span><span style="color: #000066; font-weight: bold;">;</span><br />
csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=ioerrorevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:ioerrorevent.html"><span style="color: #004993;">IOErrorEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">IO_ERROR</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=ioerrorevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:ioerrorevent.html"><span style="color: #004993;">IOErrorEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span> <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//error listening, just in case</span><br />
csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=urlrequest%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlrequest.html"><span style="color: #004993;">URLRequest</span></a><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://flashcove.net/wp-content/get_stock.php?url=&quot;</span><span style="color: #000066; font-weight: bold;">+</span>current_stock<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">COMPLETE</span><span style="color: #000066; font-weight: bold;">,</span>CSVLoaded<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> CSVLoaded<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a> = csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span> <span style="color: #0033ff; font-weight: bold;">as</span> <a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; arr = str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</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> <span style="color: #009900; font-style: italic;">//break it up</span><br />
&nbsp; &nbsp; arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">','</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">max</span> = <a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; max_volume = <a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//find our max volume and price to set a perspective scale</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">&lt;</span>arr<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">','</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #004993;">max</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #004993;">max</span> = <a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span> max_volume<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> max_volume = <a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//get the line ready!</span><br />
&nbsp; &nbsp; stock_line = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; stock_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseEnabled</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>stock_line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; stock_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">,</span>0x817339<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//secondary lining for the stock price, makes it look cool</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> dropShadow<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=dropshadowfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:dropshadowfilter.html"><span style="color: #004993;">DropShadowFilter</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=dropshadowfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:dropshadowfilter.html"><span style="color: #004993;">DropShadowFilter</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">distance</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">angle</span> = <span style="color: #000000; font-weight:bold;">45</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = 0xFFE87C<span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">1.0</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurX</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurY</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">strength</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">quality</span> = <span style="color: #000000; font-weight:bold;">15</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">inner</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">knockout</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">hideObject</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp; <br />
&nbsp; &nbsp; stock_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span>dropShadow<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; stock_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">max</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; volume_array = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; year_array = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; increment_level = <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>arr<span style="color: #000066; font-weight: bold;">.</span>length<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">/</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> prev_item<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> first_year<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span>arr<span style="color: #000066; font-weight: bold;">.</span>length<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> k<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span> k <span style="color: #000066; font-weight: bold;">&lt;</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span> k<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>k<span style="color: #000066; font-weight: bold;">*</span>increment_level<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;</span> prev_item<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900; font-style: italic;">// &amp;&amp; int(arr[Math.floor(k*increment_level)][0].substring(0,4)) &gt; first_year){</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_text<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;<span style="">\'</span>&quot;</span><span style="color: #000066; font-weight: bold;">+</span>prev_item<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> k <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">,</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//year line</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> year_line<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; year_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseEnabled</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>year_line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; year_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>0xECD872<span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; year_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> k<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; year_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> k<span style="color: #000066; font-weight: bold;">,</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; year_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>year_line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; prev_item = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>k<span style="color: #000066; font-weight: bold;">*</span>increment_level<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; stock_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> k<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>k<span style="color: #000066; font-weight: bold;">*</span>increment_level<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">max</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//volume</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> volume_line<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=sprite%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:sprite.html"><span style="color: #004993;">Sprite</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; volume_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseEnabled</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; volume_square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>volume_line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; volume_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>0x817339<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; volume_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>k<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">22</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; volume_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>k<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">22</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>k<span style="color: #000066; font-weight: bold;">*</span>increment_level<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">/</span> max_volume<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; volume_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>volume_line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//High-Low-Current prices</span><br />
&nbsp; &nbsp; add_text<span style="color: #000000;">&#40;</span><span style="color: #004993;">max</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">,</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; add_text<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;0&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">,</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span>y<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; add_text<span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">max</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Input Box!</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">type</span> = <a href="http://www.google.com/search?q=textfieldtype%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfieldtype.html"><span style="color: #004993;">TextFieldType</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">INPUT</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">border</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">228</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">350</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">26</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">88</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">maxChars</span> = <span style="color: #000000; font-weight:bold;">6</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scrollH</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">multiline</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">wordWrap</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">background</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">backgroundColor</span> = 0x111111<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">borderColor</span> = 0x333333<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = current_stock<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=keyboardevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:keyboardevent.html"><span style="color: #004993;">KeyboardEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_DOWN</span><span style="color: #000066; font-weight: bold;">,</span>new_stock_handler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> input_format<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = 0xFDD017<span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; input_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">size</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; input_format<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">font</span> = <span style="color: #990000;">&quot;Silkscreen&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>input_field<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setTextFormat</span><span style="color: #000000;">&#40;</span>input_format<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">embedFonts</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">antiAliasType</span> = <span style="color: #990000;">&quot;advanced&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; add_text<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Search New Ticker Symbol: &quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> dimensions<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">350</span><span style="color: #000066; font-weight: bold;">,</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">swapChildren</span><span style="color: #000000;">&#40;</span>tracer<span style="color: #000066; font-weight: bold;">,</span>stock_line<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Plop on the company name / title</span><br />
&nbsp; &nbsp; titleLoader = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=urlloader%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloader.html"><span style="color: #004993;">URLLoader</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; titleLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">dataFormat</span> = <a href="http://www.google.com/search?q=urlloaderdataformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloaderdataformat.html"><span style="color: #004993;">URLLoaderDataFormat</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TEXT</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; titleLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=urlrequest%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlrequest.html"><span style="color: #004993;">URLRequest</span></a><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://flashcove.net/wp-content/get_stock_name.php?url=&quot;</span><span style="color: #000066; font-weight: bold;">+</span>current_stock<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; titleLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">COMPLETE</span><span style="color: #000066; font-weight: bold;">,</span>grab_company_name<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #000000;">&#125;</span></div></div>
<p>The first for loop iterates over the received data, and stores it in to an array. We then setup our line display values (such as the drop shadow and thickness), and move the line sprite into starting position. Inside the second for loop, we actually draw a line segment between the current stock price and the next, creating the graph as we go along. If we see the beginning of a new year in the data, write down the date at the bottom of the graph and draw a thin gray line to represent it. Additionally, each time we add a little chunk to the stock history graph, we also draw a volume line down in the tiny box on the bottom to show volume history.</p>
<p>Just a few more bits to go, first we display the high, low, and current stock prices on the right-hand side of the stock chart, which you see changes every time I search a new stock on the flash demo.  We also create the input box so that our users can search for more stock symbols. And finally, make a request to the yahoo finance servers again to grab the company&#8217;s name given that we already know the stock ticker (Either the initial &#8220;MSFT&#8221; or whatever a user types in the search bar)!</p>
<p>Three more functions to go!</p>
<p><strong>COMPANY NAME FUNCTION:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> grab_company_name<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a> = titleLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span> <span style="color: #0033ff; font-weight: bold;">as</span> <a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> company<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</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; company<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = company<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">','</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; company<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = company<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> company<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>length<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; add_text<span style="color: #000000;">&#40;</span>company<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span>top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> top_left<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Very similar to the function we used to grab stock price history before.  Split the input file on newlines, and then grab the company name off the first data entry in the 2D array. Call &#8220;add_text&#8221; to create the actual instance on-screen in flash.</p>
<p>Next function, <strong>ADD TEXT HELPER:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> add_text<span style="color: #000000;">&#40;</span>mess<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a><span style="color: #000066; font-weight: bold;">,</span> wid<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> xpos<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> ypos<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> font_size<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">//Creating the textfield object and naming it &quot;myTextField&quot;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> myTextField<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//Here we add the new textfield instance to the stage with addchild()</span><br />
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>myTextField<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//content, width, x, and y coordinates.</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = mess<span style="color: #000066; font-weight: bold;">;</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = wid<span style="color: #000066; font-weight: bold;">;</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = xpos<span style="color: #000066; font-weight: bold;">;</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = ypos <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//Here are some great properties to define, first one is to make sure the text is not selectable, and also disabling the border.</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">selectable</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">border</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//textfield autosizes with the text, aligning to the left.</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">autoSize</span> = <a href="http://www.google.com/search?q=textfieldautosize%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfieldautosize.html"><span style="color: #004993;">TextFieldAutoSize</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">LEFT</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// create a TextFormat instance</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> myFormat<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textformat.html"><span style="color: #004993;">TextFormat</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//format a hex decimal color code</span><br />
myFormat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = 0xECD872<span style="color: #000066; font-weight: bold;">;</span> <br />
<br />
<span style="color: #009900; font-style: italic;">//set the text size</span><br />
myFormat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">size</span> = font_size<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//set font.</span><br />
myFormat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">font</span> = <span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//the MOST important thing, actually apply the formatting</span><br />
myTextField<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setTextFormat</span><span style="color: #000000;">&#40;</span>myFormat<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
displayed_data<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>myTextField<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>This is a helper function we&#8217;ve used several times throughout the program.  Rather than creating a full instance and construction for each and every text entity on screen, we can just make this one helper function and call IT repeatedly instead!  It takes, as input, the text, width, x and y positioning, and font size.  We could have made it a little more dynamic with font colors and such, but that wasn&#8217;t really necessary for this simple flash tutorial.</p>
<p>The only real peculiar thing here is the <strong>displayed_data.push(myTextField);</strong>. That line exists, because, when we switch to a new stock chart, all the old information needs to be removed from the previous stock. Rather than blindly search for the instances on the screen, it is super easy to just iterate through the <strong>displayed_data</strong> array, remove from screen, and then delete the data!</p>
<p>Which is exactly what&#8217;s shown in the last and final actionscript function.<br />
<strong><br />
NEW STOCK HANDLER:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// get key press only when the stock textfield is being edited</span><br />
<span style="color: #339966; font-weight: bold;">function</span> new_stock_handler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=keyboardevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:keyboardevent.html"><span style="color: #004993;">KeyboardEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #009900; font-style: italic;">// if the key is ENTER</span><br />
&nbsp; &nbsp;<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">charCode</span> == <span style="color: #000000; font-weight:bold;">13</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">//clear out the stock price line</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;stock_line<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> ff<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> ff <span style="color: #000066; font-weight: bold;">&lt;</span> displayed_data<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> ff<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>displayed_data<span style="color: #000000;">&#91;</span>ff<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;displayed_data<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>displayed_data<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">//clear out all the volume bars</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> gg<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> gg <span style="color: #000066; font-weight: bold;">&lt;</span> volume_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> gg<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;volume_square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>volume_array<span style="color: #000000;">&#91;</span>gg<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;volume_array<span style="color: #000000;">&#91;</span>gg<span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;volume_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>volume_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">//clear out all the year bars</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> hh<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> hh <span style="color: #000066; font-weight: bold;">&lt;</span> year_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> hh<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;square<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>year_array<span style="color: #000000;">&#91;</span>hh<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;year_array<span style="color: #000000;">&#91;</span>hh<span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;year_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>year_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;current_stock = input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; csvLoader = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=urlloader%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloader.html"><span style="color: #004993;">URLLoader</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">dataFormat</span> = <a href="http://www.google.com/search?q=urlloaderdataformat%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlloaderdataformat.html"><span style="color: #004993;">URLLoaderDataFormat</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TEXT</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=urlrequest%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:urlrequest.html"><span style="color: #004993;">URLRequest</span></a><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://flashcove.net/wp-content/get_stock.php?url=&quot;</span><span style="color: #000066; font-weight: bold;">+</span>input_field<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; csvLoader<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">COMPLETE</span><span style="color: #000066; font-weight: bold;">,</span>CSVLoaded<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>This function checks for the <strong>&#8220;ENTER</strong>&#8221; strike form the input text box, and if it&#8217;s there, deletes everything currently onscreen, and restarts the stock chart initialization function with a new request for data from the Yahoo finance servers!</p>
<p><HR></p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-9768577137874485";
/* FlashCove Text Banner */
google_ad_slot = "4850902026";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>And that&#8217;s it for the actionscript code!  Of course it&#8217;s a couple hundred lines long, and in some cases confusing and unclear, but if you need any help, first look at the actual compiled code in your own flash IDE, and then follow up with any questions in the comments section if you need clarification.</p>
<p>All that&#8217;s left now is the two PHP files we accessed in the actionscript code above, at the web addresses:</p>
<p><strong>
<ul>
<li>&#8220;http://flashcove.net/wp-content/get_stock.php?url=&#8221;+input_field.text</li>
<li>&#8220;http://flashcove.net/wp-content/get_stock_name.php?url=&#8221;+current_stock</li>
<li>&#8220;http://flashcove.net/wp-content/get_stock.php?url=&#8221;+current_stock</li>
</ul>
<p></strong></p>
<p>What exactly are these PHP files and what are they doing? It&#8217;s not voodoo magic, but at first it may be hard to understand, especially if you don&#8217;t have a lot of web experience.</p>
<p>There&#8217;s certain security restrictions within flash, and to prevent harmful things from happening to your computer, eternal data access can only happen on the same local PC as where the flash file resides. Since the stock information is hosted on yahoo.com servers, and not on my flashcove.net server, security restrictions prevent me from accessing stock information online.</p>
<p>HOWEVER, if I instead didn&#8217;t use flash to request yahoo&#8217;s finance data, and rather a PHP file on my own server, there would be no such restriction.  This is exactly what I did.  The <strong>get_stock.php</strong> file will grab stock information, and temporarily cache it on my flashcove.net server. Flash can then access that cached info locally, and use it to display some charts.</p>
<p>When we call the PHP file from inside flash, we can dynamically pass parameters via appending <em>?variable1=variabledata1&#038;variable2=variabledata2&#038;variable3=variabledata3&#038;&#8230;</em> to the PHP file call.<br />
All we append though in this flash tutorial is the ticker title for the specific stock.</p>
<p>With that, let&#8217;s look at the <strong>get_stock.php</strong> contents:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> &nbsp;<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;url&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://ichart.finance.yahoo.com/table.csv?s=<span style="color: #006699; font-weight: bold;">$filename</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp;<a href="http://www.php.net/header"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: text/csv; Content-Disposition:attachment; filename=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$filename</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp;<a href="http://www.php.net/readfile"><span style="color: #990000;">readfile</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>We grab the passed in ticker symbol from the &#8220;url&#8221; parameter, append it to the yahoo finance webserver call through the $filename variable, and then actually call <strong>readfile</strong> to execute the file transfer.  This will automatically feed the response back in to the URLLoader event listener in flash, which can then be parsed for the graph.</p>
<p>The other PHP file we use, <strong>get_stock_name.php</strong>, is just a call to acquire the company name for display above the graph. It&#8217;s file structure is very similar to the <strong>get_stock.php </strong>setup.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> &nbsp;<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;url&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://finance.yahoo.com/d/quotes.csv?s=<span style="color: #006699; font-weight: bold;">$filename</span>&amp;f=n&quot;</span><span style="color: #339933;">;</span><br />
&nbsp;<a href="http://www.php.net/header"><span style="color: #990000;">header</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: text/csv; Content-Disposition:attachment; filename=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$filename</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; <a href="http://www.php.net/readfile"><span style="color: #990000;">readfile</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>The only thing that changed was the yahoo request URL, and we also added <strong>&#8220;&#038;f=n&#8221; </strong> to specify we want a <em>COMPANY NAME</em> returned (Hence the &#8220;<strong>n</strong>&#8220;).</p>
<p>You now have everything you need to turn this stock flash tutorial into your very own dream stock application. My specific flash demo is riddled with bugs, doesn&#8217;t handle a ticker that doesn&#8217;t exist, and also has many other small, annoying, issues.  You could really make this a professional application just like the one Google or Yahoo Finance uses for their charts. With selectable date ranges, variation on sizes, zoom, intraday stock prices, EVERYTHING.  I&#8217;ll probably come around again sometime and make a more polished version of this tutorial, but for now this is plenty of ammunition for your own projects.</p>
<p>Just remember that, if you&#8217;re going to create your own online or commercial version of this flash stock chart application, to update the URLs of the PHP files! You don&#8217;t want to be pinging mine, but rather your own. It&#8217;ll be faster and you won&#8217;t have a dependency on someone elses servers, only your own.</p>
<p>Thanks everyone for tuning in, and I wish you the best of luck on your own projects and endeavors in flash and the stock and finance world!</p>
<p>- Dave at the Flash Cove (http://flashcove.net)</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/834/flash-historical-stock-chart-tutorial-yahoo-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cubic Spline Generation in AS3 &#8211; Catmull Rom Curves</title>
		<link>http://flashcove.net/795/cubic-spline-generation-in-as3-catmull-rom-curves/</link>
		<comments>http://flashcove.net/795/cubic-spline-generation-in-as3-catmull-rom-curves/#comments</comments>
		<pubDate>Thu, 26 May 2011 16:20:38 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[actionscript3]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[catmull]]></category>
		<category><![CDATA[cubic]]></category>
		<category><![CDATA[curve]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[splines]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=795</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/795/cubic-spline-generation-in-as3-catmull-rom-curves/">Cubic Spline Generation in AS3 &#8211; Catmull Rom Curves</a></p><p>&#160;&#160; While mathematically complex ( at least compared to all the other tutorials on this site ), curve generation techniques such as these catmull rom splines are absolutely critical in...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/795/cubic-spline-generation-in-as3-catmull-rom-curves/">Cubic Spline Generation in AS3 &#8211; Catmull Rom Curves</a></p><p>&nbsp;&nbsp;<br />
While mathematically complex ( at least compared to all the other tutorials on this site ), curve generation techniques such as these catmull rom splines are absolutely critical in the design of games, movies, and media. This specific flash tutorial on spline curve generation only covers 2D creations, but can easily be mapped to a 3D environment, which I will show in another, later tutorial. There is so much power in the knowledge of how to create these splines; in fact, the Pen tool itself in Adobe flash uses similar spline curve creation on it&#8217;s back-end implementation (Bezier, not catmull).  Here&#8217;s a list of some of the practical applications for curve creation using catmull rom splines.</p>
<ul>
<li>General Path interpolation (both 2D and 3D curve mapping)</li>
<li>Most blockbuster animated movies ( Pixar, Dreamworks, etc.. ) use catmull rom splines for camera path animations</li>
<li>Surface clamping, ex: map a roller-coaster to a track, car to a sloped road, &#8230;</li>
<li>Combined with a frenet frame, we can get interpolation across a position AND orientation ( something like a full roller coaster simulation, where the camera follows orientation of the track too ).</li>
<li>3D graphics rendering, still-frame and animated</li>
<li>Design of a car body; engineers use splines to model and create the smooth shape of a commercial car&#8217;s body (Honda, Ford, Toyota cars, etc..)</li>
</ul>
<p>I hope you can see this has a wide application of uses. So, with that, here&#8217;s our source code and demo!</p>
<blockquote><p><strong>Place the flash file somewhere on the hard drive, and then run &#8220;catmull.fla&#8221; using CTRL+ENTER inside the Flash IDE.</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/catmull.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/catmull.fla'>catmull.fla</a></div></div><div class='clear'></div></p></blockquote>
<p><strong>Here&#8217;s the final flash catmull rom dynamic curve example, for your enjoyment:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-2" class="efe-flash"><!-- --></div></div>

Click and drag the white circles to observe the dynamic curves in flash,
they will move and bend at the user's command!
</pre>
<p>So lets start development on this curve generating flash tutorial!</p>
<ol>
<li>Create a new flash document, standard size, 550 pixels by 400 pixels, actionscript 3 (AS3). Set the background color of the stage to a dark color ( mine is 0&#215;003333 ).</li>
<li>Head up to the top, <strong>Insert -&gt; New Symbol</strong>, Movieclip, name it &#8220;<strong>ball</strong>&#8220;, and check the <strong>&#8220;Export for Actionscript&#8221;</strong> box. Make sure the class name is &#8220;<strong>Ball</strong>&#8221; with a capital <em>B</em>.  Once inside, draw a simple circle to represent your markers that are going to be used for the drag and drop anchors in flash. Make sure they are centered about the orientation cross!<a href="http://flashcove.net/wp-content/uploads/2011/05/ball_draw.png"><img class="aligncenter size-full wp-image-812" title="ball_draw" src="http://flashcove.net/wp-content/uploads/2011/05/ball_draw.png" alt="" width="189" height="140" /></a></li>
<li>Now that we&#8217;ve got the ball in place, all we need to do is write up some quality actionscript 3 code ( about 120 lines! ) But before we do this in Flash, let&#8217;s get a quick explanation on how catmull rom actually works..</li>
</ol>
<p><a href="../wp-content/uploads/2011/05/catmull_pic.gif"><img class="alignright" title="catmull_pic" src="http://flashcove.net/wp-content/uploads/2011/05/catmull_pic.gif" alt="" width="280" height="167" /></a>Using a series of control points, we can procedurally generate spline segments between the points.  As seen in the picture on the right, we use 4 control points and a desired <em>t</em> along the curve to create a new point. We can do this for many different values between t = 0 and t  = 1.  In this flash tutorial, we build 200 segments between each pair of control points, by starting t = 0, and incrementing each time by 0.005 in a for loop, up to t = 1.  The easiest way to ensure we have four control points at all times is to loop back from the last to first control point at the end. Sometimes, developers will either fabricate additional control points that are not visible, or use a &#8220;Blended Parabolas&#8221; technique to tie off the ends of the interpolation path.</p>
<p style="text-align: center;">The equation we are using to find sub spline points between P1 and P2 is this:</p>
<p style="text-align: center;"><a href="http://flashcove.net/wp-content/uploads/2011/05/catmull_equation.png"><img class="size-full wp-image-817 aligncenter" title="catmull_equation" src="http://flashcove.net/wp-content/uploads/2011/05/catmull_equation.png" alt="" width="348" height="86" /></a></p>
<p style="text-align: left;">Where <strong>y</strong> is the result vector, t is the parameter (between 0 and 1) and the <strong>x&#8217;s</strong> are the grid vectors. In other words, we have 4 points, <strong>x1&#8230;x4</strong> and we&#8217;re trying to interpolate smoothly between the middle two, <strong>x2</strong> and <strong>x3</strong>. At t=0, the above equation evaluates to <strong>y</strong> = <strong>x2</strong>, and at t=1, it evaluates to <strong>x3</strong>. Intermediate values give the points in-between we are trying to find.</p>
<p style="text-align: left;">With that, let&#8217;s get to the actionscript 3 development for this spline curve flash tutorial!</p>
<p style="text-align: left;"><strong>Flash initialization and the Catmull Rom actionscript 3 function:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> lineDrawing<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> Spline_Points<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> Spline_Lengths<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> Interpolation_Points<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">450</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">250</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">300</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//CATMULL ROM FUNCTIONALITY</span><br />
<span style="color: #339966; font-weight: bold;">function</span> calculate_catmull<span style="color: #000000;">&#40;</span>t<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> p0<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> p1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> p2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> p3<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> t2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = t<span style="color: #000066; font-weight: bold;">*</span>t<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> t3<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = t2 <span style="color: #000066; font-weight: bold;">*</span> t<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// P(t)</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> firstn<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.5</span> <span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> p1<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>p0 <span style="color: #000066; font-weight: bold;">+</span> p2<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> t <span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">*</span>p0 <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">*</span>p1 <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">*</span>p2 <span style="color: #000066; font-weight: bold;">-</span> p3<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> t2 <span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>p0 <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">*</span>p1<span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">*</span>p2 <span style="color: #000066; font-weight: bold;">+</span> p3<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> t3<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// P'(t)</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> secondn<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0.5</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>p0 <span style="color: #000066; font-weight: bold;">+</span> p2<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> t <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">*</span>p0 <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">*</span>p1 <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">*</span>p2 <span style="color: #000066; font-weight: bold;">-</span> p3<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> t2 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">1.5</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>p0 <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">*</span>p1 <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">*</span>p2 <span style="color: #000066; font-weight: bold;">+</span> p3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// P''(t)</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> thirdn<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">*</span>p0 <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">*</span>p1 <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">*</span>p2 <span style="color: #000066; font-weight: bold;">-</span> p3<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> t <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">3.0</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>p0 <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">*</span>p1 <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">*</span>p2 <span style="color: #000066; font-weight: bold;">+</span> p3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> arr<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span>firstn<span style="color: #000066; font-weight: bold;">,</span> secondn<span style="color: #000066; font-weight: bold;">,</span> thirdn<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> arr<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #009900; font-style: italic;">//END CATMULL ROM FUNCTIONALITY</span></div></div>
<p>We create an &#8220;Interpolation_Points&#8221; array, and initialize some points we can to draw.  The calculate catmull function does exactly what the mathematical equation above represents. Multiply out the matrix against the control points, get our primary function, the first and second derivatives, and return.</p>
<p>Next piece of the code, <strong>Drawing the Markers:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> draw_anchors<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> k<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> k <span style="color: #000066; font-weight: bold;">&lt;</span> Interpolation_Points<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> k<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//loop through all the interpolation points, and render anchors</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> marker<span style="color: #000066; font-weight: bold;">:</span>Ball = <span style="color: #0033ff; font-weight: bold;">new</span> Ball<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = Interpolation_Points<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = Interpolation_Points<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> = <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> = <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker<span style="color: #000066; font-weight: bold;">.</span>id = k<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_DOWN</span><span style="color: #000066; font-weight: bold;">,</span>start_dragging<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; marker<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span>stop_dragging<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>marker<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>Pretty simple actionscript? We iterate over the  Interpolation_Points array, create a new <strong>Ball</strong> for each, set it&#8217;s position, add visibility by plopping it on the stage, and then attach the drag and drop functions.</p>
<p>Next chunk of actionscript code, <strong>Drag and Drop Functionality in Flash:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> start_dragging<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_MOVE</span><span style="color: #000066; font-weight: bold;">,</span> update_dragging<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">startDrag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> stop_dragging<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_MOVE</span><span style="color: #000066; font-weight: bold;">,</span> update_dragging<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stopDrag</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> update_dragging<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Interpolation_Points<span style="color: #000000;">&#91;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; Interpolation_Points<span style="color: #000000;">&#91;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; lineDrawing<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; lineDrawing = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; Spline_Points = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; init_track<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Once we click down on a marker, add continuous updating so our spline curve in the flash tutorial will update real-time. When our mouse button is raised ( no longer holding down ), remove the continuous updating and remove the drag functionality.</p>
<p>Inside update_dragging, we modify the Interpolation_Points position to match the ball&#8217;s, clear the splines onscreen, reset the array, and draw again using the init_track actionscript function, which is up next.</p>
<p><strong>Spline Curve Rendering:</strong></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> init_track<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> k<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> k <span style="color: #000066; font-weight: bold;">&lt;</span> Interpolation_Points<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> k<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//loop through all the interpolation points, and build splines in between</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> point0<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = Interpolation_Points<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> point1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = Interpolation_Points<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#40;</span>k<span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">%</span> Interpolation_Points<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> point2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = Interpolation_Points<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#40;</span>k<span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">%</span> Interpolation_Points<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> point3<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = Interpolation_Points<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#40;</span>k<span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">%</span> Interpolation_Points<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> g<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000066; font-weight: bold;">;</span> g <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">1.0</span><span style="color: #000066; font-weight: bold;">;</span> g <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">0.005</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//for each segment, calculate 200 spine points and store onto vector</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> setX<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = calculate_catmull<span style="color: #000000;">&#40;</span>g<span style="color: #000066; font-weight: bold;">,</span> point0<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> point1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> point2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> point3<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> setY<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = calculate_catmull<span style="color: #000000;">&#40;</span>g<span style="color: #000066; font-weight: bold;">,</span> point0<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> point1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> point2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> point3<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// .x/.y/.z labelled incorrectly</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> spline_spot<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span>setX<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span>setY<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> spline_spot_first<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span>setX<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span>setY<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> spline_spot_second<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span>setX<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span>setY<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//end incorrect labelling</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spline_Points<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>spline_spot<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; lineDrawing = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><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: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>lineDrawing<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; lineDrawing<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>0xFFFF00<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; lineDrawing<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span>Spline_Points<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>Spline_Points<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">///This is where we start drawing</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> iter<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> iter <span style="color: #000066; font-weight: bold;">&lt;</span> Spline_Points<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> iter<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lineDrawing<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span>Spline_Points<span style="color: #000000;">&#91;</span>iter<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> Spline_Points<span style="color: #000000;">&#91;</span>iter<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</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 />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> filtersArray<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><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: #6699cc; font-weight: bold;">var</span> color_array<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span>0xFF0000<span style="color: #000066; font-weight: bold;">,</span> 0x00FF00<span style="color: #000066; font-weight: bold;">,</span> 0x0000FF<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> j<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> j <span style="color: #000066; font-weight: bold;">&lt;</span> color_array<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> j<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> dropShadow<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=dropshadowfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:dropshadowfilter.html"><span style="color: #004993;">DropShadowFilter</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=dropshadowfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:dropshadowfilter.html"><span style="color: #004993;">DropShadowFilter</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = color_array<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurX</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurY</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">angle</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dropShadow<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">distance</span> = <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; filtersArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>dropShadow<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">setChildIndex</span><span style="color: #000000;">&#40;</span>lineDrawing<span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; lineDrawing<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = filtersArray<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Here&#8217;s where the rest of the magic happens in this flash and actionscript tutorial. Loop through all of the interpolation points, grabbing every combination of 4 points in series. For each set of four control points, iterate over <em>t</em> from 0 to 1, for 200 separate sub-sections ( as we explained above with the mathematical equation! ). After calculating the catmull, we then push the new point onto our iteration array, to be drawn after ( spline_spot_first and spline_spot_second aren&#8217;t actually used in this tutorial, but they&#8217;re the first and second derivatives we would need if we were worried about orientation! ). Once we have an array of all the sub segments of the curve we wanted, it is time to actually draw them!  </p>
<p>Dynamically create a new line drawing, and iteratively call <strong>moveTo</strong> for the line to update, position by position, where our curve is moving to. At the end of that, we add a series of drop shadows to give a 3D effect on the line, which actually looks pretty cool! I&#8217;ll explain in detail how dynamic filters work at a later time!  We also call setChildIndex(lineDrawing, 0) to push the spline curves down to the bottom, below the markers, so they&#8217;re still easy to grab and see by the user.</p>
<p>Only thing after all this code, is actually calling the functions themselves!</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">draw_anchors<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
init_track<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>At that, we are done with the flash tutorial and actionscript 3 development! Press Ctrl+Enter to fire it up and see your masterpiece!  I plan on adding easing, animation, and eventually 3D maneuverability to catmull rom&#8217;s so please stay tuned and be on the lookout  for more, great flash cove tutorials here on the site!</p>
<p>-Dave at the Flash Cove (http://flashcove.net)</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/795/cubic-spline-generation-in-as3-catmull-rom-curves/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Part 2: 3D Perspective Shooter in Flash &#8211; Paint Splatter Effect</title>
		<link>http://flashcove.net/732/part-2-3d-perspective-shooter-in-flash-paint-splatter-effect/</link>
		<comments>http://flashcove.net/732/part-2-3d-perspective-shooter-in-flash-paint-splatter-effect/#comments</comments>
		<pubDate>Sat, 21 May 2011 18:57:07 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[cannon]]></category>
		<category><![CDATA[masking]]></category>
		<category><![CDATA[paint]]></category>
		<category><![CDATA[sound]]></category>
		<category><![CDATA[special effect]]></category>
		<category><![CDATA[splatter]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=732</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/732/part-2-3d-perspective-shooter-in-flash-paint-splatter-effect/">Part 2: 3D Perspective Shooter in Flash &#8211; Paint Splatter Effect</a></p><p>&#160;This is PART 2 of the flash shooter tutorial, where we learned the basics of creating a 3D perspective cannon in flash, paint splattering effects, projectile paths in AS3, sound...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/732/part-2-3d-perspective-shooter-in-flash-paint-splatter-effect/">Part 2: 3D Perspective Shooter in Flash &#8211; Paint Splatter Effect</a></p><p>&nbsp;This is PART 2 of the flash shooter tutorial, where we learned the basics of creating a 3D perspective cannon in flash, paint splattering effects, projectile paths in AS3, sound effect imports, and mouse-controlled user input.</p>
<p>Again, all actionscript / AS3 source code and artistic effects are free to download, so go ahead and use these code segments and flash game styles in your own project.</p>
<p>What&#8217;s left to cover is:</p>
<ul>
<li> the coloring of our cannonballs</li>
<li> colored splatter animations (both the random colors and actual masking)</li>
<li> sound effects for a realistic feel</li>
</ul>
<p>Before we begin, as in the previous part of the tutorial, here is the <strong>source code and example flash demo</strong> of what we are trying to achieve:</p>
<blockquote><p><strong>Place both the actionscript and flash files in the same directory/folder, IN ADDITION TO THE CAURINA TWEENER PACKAGE, and then run the &#8220;cannon_final.fla&#8221;</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/cannon_final.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/cannon_final.fla'>cannon_final.fla</a></div></div><div class='clear'></div></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/04/cannonball.as'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/as-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/04/cannonball.as'>cannonball.as</a></div></div><div class='clear'></div></p>
<p>To get the caurina package, either search it on google, or get the reference from my caurina tutorial, over at the flash cove link:</p>
<p><span style="text-decoration: underline;"><a href="http://flashcove.net/163/programatic-tweening-flash/"><strong>http://flashcove.net/163/programatic-tweening-flash/</strong></a></span></p></blockquote>
<p><strong>Here&#8217;s the final flash paint cannon game, for your enjoyment:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-3" class="efe-flash"><!-- --></div></div>

Point and click anywhere on the screen to fire the cannon! A randomly colored ball
will fly out and splatter on the backboard, in that random color, size, and orientation.
This flash game DOES INCLUDE SOUND EFFECTS, so if you'd like to hear
those effects when you click and when the paint hits the wall, turn up the volume!</pre>
<p>So, let&#8217;s wrap this baby up.  Next step in creation for this flash game tutorial, coloring of the cannonballs.</p>
<p>&nbsp;</p>
<p>Head inside the &#8220;cannonball&#8221; MovieClip by double clicking it inside the Library. Select the middle, white filling, copy (Crtl+C), and paste in place (Crtl+Shift+V).  Right click on that new instance of the filling, and select &#8220;<strong>Convert to Symbol&#8230;</strong>&#8220;  Name it &#8220;<strong>front</strong>,&#8221; and name that single instance inside the cannonball &#8220;<strong>front</strong>&#8221; as well.</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/font_piece.png"><img class="aligncenter size-full wp-image-761" title="font_piece" src="http://flashcove.net/wp-content/uploads/2011/05/font_piece.png" alt="" width="526" height="174" /></a></p>
<p>Only other thing left to do for that, is inside the &#8220;front&#8221; MovieClip, set the alpha on each gradient color to 50%.  We want the color tinting in flash to only be partial, so you can see the distinguished curves and shadows on the cannonball, not just a solid boring color.</p>
<p>______________________________________</p>
<p>Now, for the paint effect, we use a masking technique.  I covered what this is in another <a href="http://flashcove.net/309/masking-layers-in-flash-games-zombie-shooter/">masking tutorial</a> on the flashcove site, so get acquainted with it there if you haven&#8217;t heard of the concept.</p>
<p>The hierarchy for our flash paint splatter effect is a triple MovieClip.  We&#8217;ve got a  &#8220;<strong>splat</strong>&#8220;, which, on each frame, has a unique paint splatter picture / art. Color doesn&#8217;t matter, just a unique image on each, centered around the crosshair. I&#8217;ve got 6 in my flash source file, and all I did to find them was search &#8220;paint splatter&#8221; images online.</p>
<p>Wrapped around this &#8220;<strong>splat</strong>&#8221; MovieClip is a MovieClip appropriately named &#8220;<strong>splat_wrap</strong>&#8220;, all this is, is an instance of a paint splat centered on stage and tinted to a pure, 100% white color. Reason being, we need it white so that when the further outer layer is tinted, we&#8217;re layering on top an impressionable color, and not black or orange like the naturally colored paint splatter effects are on the &#8220;<strong>splat</strong>&#8221; MovieClip. I uncreatively named my instance &#8220;<strong>spla</strong>&#8220;, which will be referenced in some actionscript 3 code later on. The setup will look something like this:</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/white_tint.png"><img class="aligncenter size-full wp-image-765" title="white_tint" src="http://flashcove.net/wp-content/uploads/2011/05/white_tint.png" alt="" width="504" height="399" /></a></p>
<p>The last wrapper layer is a MovieClip called &#8220;<strong>splat_anim</strong>&#8220;.  Inside, it has an instance of a &#8220;splat_wrap&#8221; and an instance of a &#8220;<strong>masking</strong>&#8221; MovieClip.  The masking layer is what we use to give the appearance of paint flying out, as compared to the paint splatter effect image immediately appearing, and looking lame.</p>
<p>Only tricky thing about setting this up is we need two layers. Top layer is the &#8220;<strong>masking</strong>,&#8221; with the layer checked as &#8220;<strong>Mask</strong>&#8220;, and the bottom layer sub indented, with a &#8220;<strong>splat_wrap</strong>&#8221; instance named &#8220;<strong>splatter</strong>&#8221; laying behind.  It will look like this. The yellow blob is our masking.</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/masking_paint.png"><img class="aligncenter size-full wp-image-767" title="masking_paint" src="http://flashcove.net/wp-content/uploads/2011/05/masking_paint.png" alt="" width="449" height="347" /></a></p>
<p>Last part, the creation of our &#8220;<strong>masking</strong>&#8221; MovieClip class!  Remember that what&#8217;s colored is what&#8217;s visible. All I have is three frames, first that small blob, second a larger blob, and third a massive rectangle to make sure everything below is not-masked.  On that third frame, I also put a command on the line to keep our masking from going anywhere.</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #004993;">stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>And that&#8217;s all there is to the art!  Make sure though, that the outermost layer, &#8220;<strong>splat_anim</strong>&#8220;, is checked as &#8220;<strong>Export for Actionscript</strong>,&#8221; keep the name the same. We&#8217;re going to be dynamically creating instances of this for each cannonball.</p>
<p>____________________________________</p>
<p>No actionscript 3 (AS3)  code change is needed in the &#8220;<strong>cannon.fla,</strong>&#8221; only the cannonball actionscript class file. Inside the cannonball constructor class ( the <strong>cannonball()</strong> part ), we need to modify the color before being launched!  So, our new constructor will look something like this:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><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;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>time_to_live <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//we're in action</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= spread<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= accel<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000; font-weight:bold;">0.02</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000; font-weight:bold;">0.02</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; time_to_live<span style="color: #000066; font-weight: bold;">--;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; accel <span style="color: #000066; font-weight: bold;">+</span>= GRAVITY<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//remove from screen</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> splat<span style="color: #000066; font-weight: bold;">:</span>splat_wrap = <span style="color: #0033ff; font-weight: bold;">new</span> splat_wrap<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; splat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; splat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; splat<span style="color: #000066; font-weight: bold;">.</span>splatter<span style="color: #000066; font-weight: bold;">.</span>spla<span style="color: #000066; font-weight: bold;">.</span>id = <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ceil</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; splat<span style="color: #000066; font-weight: bold;">.</span>splatter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span> = <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; splat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> = <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">0.25</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; splat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> = splat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> myColor<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=colortransform%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:colortransform.html"><span style="color: #004993;">ColorTransform</span></a> = splat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">transform</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">colorTransform</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myColor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = the_color<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myColor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alphaOffset</span> = <span style="color: #000000; font-weight:bold;">0.8</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; splat<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">transform</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">colorTransform</span> = myColor<span style="color: #000066; font-weight: bold;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>splat<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">swapChildren</span><span style="color: #000000;">&#40;</span>splat<span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> update<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Only thing the change in the source code is in our <strong>else</strong> bracket. We&#8217;re still removing the cannonball, but we&#8217;re also creating a splatter effect!  Assign it to the same position as the cannonball, pick a random drawing of the paint splatter (one of the 6 for me), randomize the size and rotation, update the color reflection, add it to the stage, and swapDepths to the front!</p>
<p>The only real tricky thing in this code is assigning one of the 6 paint splatter effect drawings. In Flash, some things work on the back-end that we don&#8217;t understand. Since the Masking is changing frames 3 times, a new instance of splat is also being re-rendered, typically resetting the paint splatter drawing back to frame one. To avoid that, we set the &#8220;<strong>.id</strong>&#8221; to the frame we want, and then make a modification inside the &#8220;<strong>splat</strong>&#8221; MovieClip.</p>
<p>Inside the frame 1 source code, add the following lines:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><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;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>This should make the paint splatter effect decision seamless every time.</p>
<p>_________________________________</p>
<p>If you were to compile and run this flash tutorial right now, everything should work flawlessly, except where&#8217;s the sound effects!?</p>
<p>They&#8217;re actually trivially easy to implement. First thing though, is you need to find some free sound effects. Either you can  look  online, or just use the ones I did, </p>
<blockquote><p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/bubble_cannon.mp3'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/mp3-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/bubble_cannon.mp3'>bubble_cannon.mp3</a></div></div><div class='clear'></div><br />
<div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/land.mp3'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/mp3-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/land.mp3'>land.mp3</a></div></div><div class='clear'></div><br />
&nbsp;
</p></blockquote>
<p>You can import these sound effects in to flash by selecting <strong>File in the top left -> Import -> Import to Library..  </strong>. Find the two mp3 files, and Ctrl+Click them onto the list.</p>
<p>Inside the library, find both instances, check the &#8220;Export for Actionscript&#8221; under properties, and name them &#8220;<strong>cannonmp3</strong>&#8221; and &#8220;<strong>thudmp3</strong>&#8221; respectively for Actionscript use.<br />
________________________</p>
<p>Inside the cannonball.as file, right up with all the as3 variable declarations, add the follow line to import sound effects in flash.</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> thud<span style="color: #000066; font-weight: bold;">:</span>thudmp3= <span style="color: #0033ff; font-weight: bold;">new</span> thudmp3<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>And down inside the <strong>update</strong> function, in the else bracket, add one more line:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">thud<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">play</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>We now have the landing / paint splatter sound effect in our flash game tutorial!</p>
<p>Those same two lines also need to be placed inside the <strong>cannon.fla</strong>, except we need <strong>cannonmp3</strong>, not<strong> thudmp3</strong>. See if you can do that one yourself, and if you&#8217;re really stuck, just reference the absolutely free AS3 source code available for download at the top of the page!</p>
<p>Compile and run this flash tutorial game demo via &#8220;Ctrl+Enter&#8221; and you&#8217;ll see the beautiful flash masterpiece you just created!<br />
_______</p>
<p>Thanks for tuning in to another amazingly in-depth and free flash tutorial on paint splatter effects, 3D perspective, and importing sound effects!</p>
<p>-Dave at the FlashCove (http://flashcove.net)</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/732/part-2-3d-perspective-shooter-in-flash-paint-splatter-effect/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
<enclosure url="http://flashcove.net/wp-content/uploads/2011/05/bubble_cannon.mp3" length="8486" type="audio/mpeg" />
<enclosure url="http://flashcove.net/wp-content/uploads/2011/05/land.mp3" length="7232" type="audio/mpeg" />
		</item>
		<item>
		<title>Countdown Timer Animation (Dynamic) in Flash AS3</title>
		<link>http://flashcove.net/693/countdown-timer-animation-dynamic-in-flash-as3/</link>
		<comments>http://flashcove.net/693/countdown-timer-animation-dynamic-in-flash-as3/#comments</comments>
		<pubDate>Tue, 10 May 2011 02:18:07 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[countdown]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[pretty]]></category>
		<category><![CDATA[special effect]]></category>
		<category><![CDATA[timer]]></category>
		<category><![CDATA[tween]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=693</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/693/countdown-timer-animation-dynamic-in-flash-as3/">Countdown Timer Animation (Dynamic) in Flash AS3</a></p><p>&#160; I play a lot of Call of Duty: Black Ops, and every time an online multiplayer game starts up, there are flashing yellow numbers for the countdown before the...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/693/countdown-timer-animation-dynamic-in-flash-as3/">Countdown Timer Animation (Dynamic) in Flash AS3</a></p><p>&nbsp;</p>
<p>I play a lot of Call of Duty: Black Ops, and every time an online multiplayer game starts up, there are flashing yellow numbers for the countdown before the game begins. While they look okay, I figure I could make something look a little cooler in an easy flash tutorial to submit to the flash cove.  This flash tutorials encompasses knowledge about embedding fonts, zoom effects, and  timer functionality within flash and as3. Also, rather than storing each number individually in a seperate image, we dynamically use a countdown, updating a Dynamic text field instead for better efficiency. I hope that after reading through this free flash countdown tutorial, and observing the actionscript source code, you too can create a great countdown timer in flash for your very own flash game or flash application.</p>
<p>The actionscript source code and full source FLA file are available right here, along with an interactive flash demo of what you can achieve after covering this easy countdown timer flash tutorial.</p>
<blockquote><p><strong>Place the actionscript / flash file &#8220;countdown_demo.fla&#8221; in a directory/folder on your PC, and then run it using (Ctrl + Enter)</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/countdown_demo.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/countdown_demo.fla'>countdown_demo.fla</a></div></div><div class='clear'></div></p>
<p>&nbsp;</p></blockquote>
<p><strong>Here&#8217;s the mouse animation in flash we are looking to achieve:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-4" class="efe-flash"><!-- --></div></div>

Press the start button to see our <strong>Call of Duty Black Ops flash countdown</strong> in action!
Once it is done, you can also replay via the button bottom-left in the flash demo.</pre>
<p>So, let&#8217;s get started on the development.</p>
<p style="text-align: center;">Open up a new flash document. Standard 550 pixels by 400 pixels.  Insert a new MovieClip, and call it &#8220;<strong>countdown</strong>&#8220;.  Inside the MovieClip, create two Dynamic text boxes, and make sure they align top-left to the center! This is EXTREMELY IMPORTANT for this tutorial, or else the countdown timer&#8217;s flash font will not zoom properly!  As far as font goes, I used <strong><a href="http://www.dafont.com/joystix.font">joystix</a>, </strong>a really great set available at the link. In this next step I&#8217;ll teach you how to embed this specific font, or any other font at that too in flash.</p>
<p style="text-align: center;"><a href="http://flashcove.net/wp-content/uploads/2011/05/aligned_countdown_font.png"><img class="size-full wp-image-701 aligncenter" title="aligned_countdown_font" src="http://flashcove.net/wp-content/uploads/2011/05/aligned_countdown_font.png" alt="" width="267" height="129" /></a></p>
<p>Up on the very top right of your Library in Flash, click the small button, and then select &#8220;<strong>New Font&#8230;</strong>&#8221;</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/new_font_popup.png"><img class="aligncenter size-full wp-image-703" title="new_font_popup" src="http://flashcove.net/wp-content/uploads/2011/05/new_font_popup.png" alt="" width="278" height="268" /></a></p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/new_font_popup.png"></a>Once inside, set the name to be &#8220;<strong>joystix64</strong>&#8221; set the actual font to <strong>Joystix</strong>, and make sure the size is set to <strong>64</strong>, which is the size we will be using in this flash tutorial!</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/font_settings.png"><img class="aligncenter size-full wp-image-704" title="font_settings" src="http://flashcove.net/wp-content/uploads/2011/05/font_settings.png" alt="" width="430" height="233" /></a></p>
<p>For the two sets of text inside our countdown, set both to a &#8220;<strong>Dynamic Text Box</strong>,&#8221; and make sure the font you are using for them is NOT <strong>Joystix</strong>, but actually <strong>joystix64</strong>, which should be somewhere at the top of the font selection list.  Additionally, we need to set the embedded characters for each of the text instances. In the property manager, there&#8217;s a button titled &#8220;<strong>Character Embedding..</strong>&#8221; click that, and then embed the following characters: Numerals (0-9) and the individual letters &#8220;GO!&#8221;  As seen in the picture; make sure the total is 14 characters like mine!</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/character_embed.png"><img class="aligncenter size-full wp-image-707" title="character_embed" src="http://flashcove.net/wp-content/uploads/2011/05/character_embed.png" alt="" width="320" height="425" /></a></p>
<p>Once this is all complete, make sure you actually give both countdown text instances a label! I made mine &#8220;<strong>text1</strong>&#8221; and &#8220;<strong>text2</strong>&#8220;  Once you&#8217;ve done that, the drawing up of our fonts in this flash tutorial is complete, all that&#8217;s left is some actionscript 3 coding.</p>
<p>&nbsp;</p>
<p>Without further adieu, here is the actionsciprt 3 code for this easy countdown timer (Call of Duty style!) flash tutorial:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> beginning<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>countdown_function<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> countdown_function<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> current<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beginning<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>current <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">11000</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span>text1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;GO!&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span>text2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;GO!&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>countdown_function<span style="color: #000000;">&#41;</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: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>current <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span>text1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;GO!&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span>text2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;GO!&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span>text1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000066; font-weight: bold;">-</span> <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>current <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span>text2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000066; font-weight: bold;">-</span> <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>current <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">1000</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 />
&nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> = <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">100</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>current <span style="color: #000066; font-weight: bold;">%</span> <span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; countdown<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> = countdown<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>The <strong>beginning </strong>timer value just grabs the local value for a start time to reference against. <strong>getTimer()</strong> gives us the number of milliseconds passed so far in the flash game. So, we add an ENTER_FRAME listener, grab the difference between present time and starting time (in milliseconds), and do a check..</p>
<p>If time time is less than 10 seconds (10,000 milliseconds), set the text values for both our instances to the right number of seconds.. AKA</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000066; font-weight: bold;">-</span> <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>current <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>Also, set the scale (size) of our text to be the inverse of the inter-second moment. The closer we are to zero of mod 1000, the larger we are (scale of 2), the closer we are to  999 of mod 1000, the smaller we get (scale of 1). This gives us a zoom-in effect for the countdown timer in the flash tutorial.</p>
<p>If time time is greater than 10 seconds (10,000 milliseconds), but less than 11 seconds (11,000 milliseconds), set the font to &#8220;GO!&#8221; rather than a number, as we&#8217;ve hit zero.</p>
<p>If time is greater than 11 seconds (the first if statement in the actionscript 3 code), remove the frame listener, and leave the text statically as &#8220;GO!&#8221;</p>
<p>That&#8217;s all there is to it for the flash as3 code!  Just be sure, that beforeyou run the flash game, you actually have an instance of the countdown text on the stage, labeled as &#8220;<strong>countdown</strong>&#8221; so the actionscript 3 code has something specific to reference!</p>
<p>I appreciate you guys tuning in to this sweet Call of Duty: Black Ops style countdown timer flash tutorial! As always the as3 source code is free to duplicate, so get developing!</p>
<p>-Dave at the Flash Cove (http://flashcove.net)</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/693/countdown-timer-animation-dynamic-in-flash-as3/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Animated Mouse Effects in Flash</title>
		<link>http://flashcove.net/666/animated-mouse-effects-in-flash/</link>
		<comments>http://flashcove.net/666/animated-mouse-effects-in-flash/#comments</comments>
		<pubDate>Mon, 09 May 2011 02:13:13 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[particle]]></category>
		<category><![CDATA[skull]]></category>
		<category><![CDATA[special effect]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=666</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/666/animated-mouse-effects-in-flash/">Animated Mouse Effects in Flash</a></p><p>&#160; Mouse animations and trailing effects are perfect for flash based websites, online advertisements, and flash video games. Not only that, but mouse animations in flash are extremely easy to...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/666/animated-mouse-effects-in-flash/">Animated Mouse Effects in Flash</a></p><p>&nbsp;</p>
<p>Mouse animations and trailing effects are perfect for flash based websites, online advertisements, and flash video games. Not only that, but mouse animations in flash are extremely easy to customize and edit to your liking, so they can be perfect for your specific web application or flash game.  What we are going to create in this easy flash tutorial is a trailing glow effect that will follow your mouse around, and pulsate when idle.</p>
<p>The actionscript source code and full source FLA file are available right here, along with an interactive flash demo of what you can achieve after covering this easy mouse animation flash tutorial.</p>
<blockquote><p><strong>Place the actionscript / flash file &#8220;mouse_trail.fla&#8221; in a directory/folder on your PC, and then run it using (Ctrl + Enter)</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/mouse_trail.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/mouse_trail.fla'>mouse_trail.fla</a></div></div><div class='clear'></div></p>
<p>&nbsp;</p></blockquote>
<p><strong>Here&#8217;s the mouse animation in flash we are looking to achieve:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-5" class="efe-flash"><!-- --></div></div>

Mouse the mouse around quickly, and watch the glow follow in our flash game demo.
If you keep your cursor still, the glow will pulsate around
the skull for a nice flash effect.</pre>
<ol>
<li>First step, open a new Flash Document, Actionscript 3, standard size of 550 pixels by 400 pixels.</li>
<li>Import some <strong>kool</strong> cursor art for your mouse. I used a skull, which you can grab right here, below. Place it inside a new MovieClip (Ctrl + F8), and then drag a single instance of the new MovieClip on to the main flash stage, and label that specific instance of the MovieClip as &#8220;<strong>skull</strong>&#8220;<a href="http://flashcove.net/wp-content/uploads/2011/05/skull.png"><img class="alignright size-full wp-image-679" style="margin-top: 10px;" title="skull" src="http://flashcove.net/wp-content/uploads/2011/05/skull.png" alt="" width="105" height="155" /></a></li>
<li>Make another new MovieClip (Ctrl + F8), and label it &#8220;<strong>trail</strong>&#8220;. Inside, all you need to draw is a 100 pixel by 100 pixel perfect circle, black, centered around the cross index in the MouseClip drawing plane.  After drawing, exit out to the main Scene 1 frame, <strong>right click</strong> the &#8220;<strong>trail</strong>&#8221; in the library, select <strong>Properites</strong>, and then check the &#8220;<strong>Export for Actionscript</strong>&#8221; box.</li>
<li>Third and final thing to draw in this easy flash tutorial is some gridwork on the main stage. Select the brush tool, a light gray color, and hold down the SHIFT button while clicking and dragging to create some straight lines horizontally and vertically across the stage. They don&#8217;t need to be in any type of MovieClip, on-stage is just fine.</li>
<li>Once all the flash art is drawn (the circle, skull, and criss-cross lines), it&#8217;s time to write the small amount of actionscript 3 code neded to get this easy flash tutorial running, and for the mouse animation effects to get glowing. Right click the first frame on the main stage, and select <strong>Actions </strong>down on the bottom. Enter the following as3 code in to the flash code box..</li>
</ol>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> blur_filter<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=blurfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:blurfilter.html"><span style="color: #004993;">BlurFilter</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=blurfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:blurfilter.html"><span style="color: #004993;">BlurFilter</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <br />
<span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #004993;">stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span> <br />
&nbsp; &nbsp; skull<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">startDrag</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?q=mouse%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouse.html"><span style="color: #004993;">Mouse</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">hide</span><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: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>mm<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #339966; font-weight: bold;">function</span> mm<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0.30</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; createTrailBall<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;<br />
<span style="color: #339966; font-weight: bold;">function</span> createTrailBall <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Create a new circle instance</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> trailBall<span style="color: #000066; font-weight: bold;">:</span>trail=<span style="color: #0033ff; font-weight: bold;">new</span> trail<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; trailBall<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cacheAsBitmap</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Position the trailing circle in the same position where the mouse is</span><br />
&nbsp; &nbsp; trailBall<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; trailBall<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">mouseY</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//add the blur to smooth the edges</span><br />
&nbsp; &nbsp; trailBall<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span>blur_filter<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Add ENTER_FRAME to animate the trailing circle</span><br />
&nbsp; &nbsp; trailBall<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>animateTrailBall<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//add it to the bottom of the stage, so it appears below the grid onscreen.</span><br />
&nbsp; &nbsp; <span style="color: #004993;">addChildAt</span> <span style="color: #000000;">&#40;</span>trailBall<span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;<br />
<span style="color: #339966; font-weight: bold;">function</span> animateTrailBall <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//In each frame, reduce the alpha and the scale of the trail ball.</span><br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000; font-weight:bold;">0.10</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">0.10</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">0.10</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> myColor<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=colortransform%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:colortransform.html"><span style="color: #004993;">ColorTransform</span></a> = e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">transform</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">colorTransform</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; myColor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span>= 0xFFFFFF <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000066; font-weight: bold;">-</span> e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">transform</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">colorTransform</span> = myColor<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//if alpha is less than 0; remove the ball from the stage</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>animateTrailBall<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">removeChild</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000000;">&#41;</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>We have an initialization function which adds the ENTER_FRAME listener. Inside that listener, every frame, we generate a random number to check if we should create a glowy ball in flash. If we do need to, we call <strong>createTrailBall(),</strong> which sets up the x and y positions, caches it for optimization, adds the blur filter (from the initialization at the top), starts the update function, and draws to the screen.  In the update function, <strong>animateTrailBall()</strong>, we blow up in size, fade away, and drift to a white color. Once alpha is zero (we are completely invisible), we promptly remove and delete this instance of the glowy ball.</p>
<p>And that&#8217;s all there is to it!  With that, this easy flash tutorial is complete. Compile and run the flash tutorial and you should get something very similar to the flash demo at the top of the page.</p>
<p>Thanks for tuning in to another great, free, and easy flash game tutorial!</p>
<p>- Dave at the Flash Cove (http://flashcove.net)</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/666/animated-mouse-effects-in-flash/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>3D Perspective Shooter in Flash &#8211; Paint Splatter Effect</title>
		<link>http://flashcove.net/621/3d-shooter-flash-paint-splatter/</link>
		<comments>http://flashcove.net/621/3d-shooter-flash-paint-splatter/#comments</comments>
		<pubDate>Sat, 07 May 2011 18:00:01 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[actionscipt]]></category>
		<category><![CDATA[cannon]]></category>
		<category><![CDATA[paint]]></category>
		<category><![CDATA[special effect]]></category>
		<category><![CDATA[splatter]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=621</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/621/3d-shooter-flash-paint-splatter/">3D Perspective Shooter in Flash &#8211; Paint Splatter Effect</a></p><p>&#160; This large and detailed tutorial will teach you the basics of creating a 3D perspective cannon in flash, paint splattering effects, projectile paths in AS3, sound effect imports, and...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/621/3d-shooter-flash-paint-splatter/">3D Perspective Shooter in Flash &#8211; Paint Splatter Effect</a></p><p>&nbsp;</p>
<p>This large and detailed tutorial will teach you the basics of creating a 3D perspective cannon in flash, paint splattering effects, projectile paths in AS3, sound effect imports, and mouse-controlled user input. All actionscript / AS3 source code and artistic effects are free to download, and I hope using these examples you too can create some pretty awesome flash games, applications, and effects.  There are a couple subtle flash effects in here; you may wish to follow along using my &#8220;cannon.fla&#8221; and &#8220;cannonball.as&#8221; source code I attached below. Playing with the demo at the top of this flash tutorial page will show you how effective such a simple paint effect can be in Flash.. and, at that, I am proud to present the following&#8230;</p>
<p><strong>The final 3D paint cannon source code can be found right here:</strong></p>
<blockquote><p><strong>Place both the actionscript and flash files in the same directory/folder, IN ADDITION TO THE CAURINA TWEENER PACKAGE, and then run the &#8220;cannon.fla&#8221;</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/cannon.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/cannon.fla'>cannon.fla</a></div></div><div class='clear'></div></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/05/cannonball.as'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/as-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/05/cannonball.as'>cannonball.as</a></div></div><div class='clear'></div></p>
<p>&nbsp;</p>
<p>To get the caurina package, either search it on google, or get the reference from my caurina tutorial, over at the flash cove link:</p>
<p><span style="text-decoration: underline;"><a href="http://flashcove.net/163/programatic-tweening-flash/"><strong>http://flashcove.net/163/programatic-tweening-flash/</strong></a></span></p></blockquote>
<p><strong>Here&#8217;s the final flash paint cannon game, for your enjoyment:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-6" class="efe-flash"><!-- --></div></div>

Point and click anywhere on the screen to fire the cannon! A randomly colored ball
will fly out and splatter on the backboard, in that random color, size, and orientation.
This flash game DOES INCLUDE SOUND EFFECTS, so if you'd like to hear
those effects when you click and when the paint hits the wall, turn up the volume!</pre>
<p>So, let&#8217;s start the development..</p>
<p>First thing we need to draw is a cannon (in 3D perspective.) Open up a new MovieClip, and name it &#8220;cannon.&#8221; Inside, draw your art such that the very bottom center base of your cannon art is based at the center index point for the MovieClip.  The indexing should look something like the setup in the picture below; hopefully you can draw a letter better looking cannon too lol.</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/cannon_art.png"><img class="aligncenter size-full wp-image-637" title="cannon_art" src="http://flashcove.net/wp-content/uploads/2011/05/cannon_art.png" alt="" width="366" height="338" /></a></p>
<p>Next step, insert a new layer inside the &#8220;<strong>cannon</strong>&#8221; MovieClip,  and draw a small little red dot. Right Click that red dot, and say &#8220;Convert to MovieClip,&#8221; call it &#8220;<strong>launchspot</strong>&#8220;.  Click back in to the &#8220;cannon&#8221; MoveiClip, select the instance of the &#8220;<strong>launchspot</strong>,&#8221; and name this one specifically &#8220;<strong>launchspot</strong>&#8221; again. When we access from as3, this is the name we are going to call.  The reason though, for this little dot existing, is that when I click to fire cannonballs, I need an exact place to launch to cannonballs from, so it looks realistic.  If this  is located on the cannon at all times, I can use it&#8217;s internal position to index and place my procedurally generated MovieClips from the actionscript / AS3 code.  You should place the instance of the &#8220;<strong>launchspot</strong>&#8221; MovieClip somewhere at the base of the barrel of the cannon, just like in this picture from the flash game tutorial:</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/cannon_index_spot.png"><img class="aligncenter size-full wp-image-639" title="cannon_index_spot" src="http://flashcove.net/wp-content/uploads/2011/05/cannon_index_spot.png" alt="" width="582" height="285" /></a></p>
<p>Once the launchspot is set for the cannon MovieClip in the flash ame, it is time to setup the CANNONBALL. Insert a new MovieClip via <strong>Insert -&gt; New Symbol..</strong>, and name it &#8220;<strong>cannonball</strong>&#8220;.  Inside, using the sphere tool from inside the flash IDE, draw a radial colored circle with a thick (5 pixel or so) black border.  Based the alignment of the game&#8217;s cannonball around the center.  My flash cannonball looks like this:</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/cannonball_art.png"><img class="aligncenter size-full wp-image-641" title="cannonball_art" src="http://flashcove.net/wp-content/uploads/2011/05/cannonball_art.png" alt="" width="384" height="325" /></a></p>
<p>Once the art is done, we actually need to export the &#8220;cannonball&#8221; for use in Actionscript 3 as well. Find the cannonball in the flash library,<strong> Right Click -&gt; Properties -&gt; Check the &#8220;Export for Actionscript,</strong>&#8221; and leave the name as &#8220;<strong>cannonball</strong>&#8220;  We&#8217;re done with this art stuff for now, lets get the the as3 source code!</p>
<p>Go the the main &#8220;Scene 1&#8243; part of yuor flash porgram, and on frame 1 of the file, under the &#8220;Actions&#8221; tab, write in the following as3 code..</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><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> caurina<span style="color: #000066; font-weight: bold;">.</span>transitions<span style="color: #000066; font-weight: bold;">.</span>Tweener<span style="color: #000066; font-weight: bold;">;</span> &nbsp;<span style="color: #009900; font-style: italic;">//import the Tweener class from the Caurina package.</span><br />
<br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_MOVE</span><span style="color: #000066; font-weight: bold;">,</span> mm<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span> mu<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> mm<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
cannon<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">275</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">275</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.05</span><span style="color: #000066; font-weight: bold;">;</span><br />
cannon<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span> = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">275</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">mouseX</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.05</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> mu<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> anglec<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000066; font-weight: bold;">-</span>cannon<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> xposc<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = cannon<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
Tweener<span style="color: #000066; font-weight: bold;">.</span>addTween<span style="color: #000000;">&#40;</span>cannon<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#123;</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">540</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">:</span> xposc <span style="color: #000066; font-weight: bold;">+</span> anglec<span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> &nbsp;<span style="color: #004993;">time</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0.035</span><span style="color: #000066; font-weight: bold;">,</span> transition<span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;linear&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
Tweener<span style="color: #000066; font-weight: bold;">.</span>addTween<span style="color: #000000;">&#40;</span>cannon<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#123;</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">530</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">:</span> xposc<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">time</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0.055</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">delay</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0.035</span><span style="color: #000066; font-weight: bold;">,</span> transition<span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;linear&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">//make cannonball</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> loca<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span>cannon<span style="color: #000066; font-weight: bold;">.</span>launchspot<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>cannon<span style="color: #000066; font-weight: bold;">.</span>launchspot<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> glob<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = cannon<span style="color: #000066; font-weight: bold;">.</span>launchspot<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">localToGlobal</span><span style="color: #000000;">&#40;</span>loca<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
glob<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">275</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>glob<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">275</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.4</span> <span style="color: #000066; font-weight: bold;">-</span> anglec<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> cb<span style="color: #000066; font-weight: bold;">:</span>cannonball = <span style="color: #0033ff; font-weight: bold;">new</span> cannonball<span style="color: #000000;">&#40;</span>glob<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">340</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">-</span>anglec<span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.25</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span>anglec<span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">400</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>cb<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">swapChildren</span><span style="color: #000000;">&#40;</span>cb<span style="color: #000066; font-weight: bold;">,</span>cannon<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>We only need two global listeners on the flash game demo, a <strong>MOUSE_MOVE</strong> and a <strong>MOUSE_UP</strong> (mouse_up is the same as a mouse click, but only fires when we lift the key.)  In the MOUSE_MOVE function, &#8220;<strong>mm</strong>&#8221; all we do is move the cannon left and right, and also give it a little tilt with the <strong>.rotation</strong> to help make the 3D perspective look a little more realistic in our flash game.  The 275 value just represents where our instance of the cannon sits on the stage, dead center on a 550 pixel width scene..</p>
<p>In the <strong>MOUSE_UP</strong> function, we are doing two things.. a kickback, or jiggle, on the cannon when it&#8217;s fired so it looks realistically 3D in flash, and of course, the actual generating of a cannon ball.  The  two Tweener calls, using the caurina add-in package, give us our kickback animation. To learn how to do this, you can visit the Caurina tweening tutorial to get a better understanding at this link, <strong><a href="../163/programatic-tweening-flash/"><strong>http://flashcove.net/163/programatic-tweening-flash/</strong></a></strong>.  The second part, where we make the cannon ball, we have to know the point of launch first, since it is not simply our Mouse Position. To do this, we use the &#8220;<strong>launchspot</strong>&#8221; MovieClip from inside the cannon! The only thing though, is it&#8217;s position is <strong>RELATIVE </strong>to the center of the inside of the MovieClip, not to the global stage coordinates of our flash game.</p>
<p>Thankfully though, there&#8217;s a function called<strong> localToGlobal()</strong>, that can do a conversion for us to the stage coordinates in the flash game tutorial! All we have to do is pass in the local coordinate points (I.E. the cannon.launchspot.x and cannon.launchspot.y in loca) to localToGlobal, and call it with the cannon prefix.</p>
<p>This positioning isn&#8217;t perfect though with the localToGlobal() position, so after a few little test tweaks, we get much more realistic projection points in flash after modifying <strong>glob.x</strong> and messing with the<strong> y input value </strong>for the cannon ball constructor. Speaking of the constructor, it takes in three variables, the x-position, y-position, and velocity.  The code on the backend for this cannon ball is in the actionsciprt 3 / AS3 file <strong>cannonball.as</strong>. We will cover this code in just a second.</p>
<p>The final part, addChild() and swapChildren() flash functions, simply add the cannonball to the screen and then swaps depths with the cannon launcher itself in flash. Reason we want to do this, is because we want the cannon ball to come from BEHIND the cannon, and addChild() automatically adds to the top. swapChildren() easily fixes that problem and makes the cannon ball launch correctly from behind when the flash game tutorial is compiled and run.</p>
<p>____________________________________________________</p>
<p>The main function code is now complete, time for the separate actionscript 3 / AS3 file, <strong>cannonball.as</strong>:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #9900cc; font-weight: bold;">package</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.*;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.*;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span><span style="color: #000066; font-weight: bold;">.*;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.filters</span><span style="color: #000066; font-weight: bold;">.*;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span><span style="color: #000066; font-weight: bold;">.</span><a href="http://www.google.com/search?q=colortransform%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:colortransform.html"><span style="color: #004993;">ColorTransform</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> cannonball <span style="color: #0033ff; font-weight: bold;">extends</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> static const START_DELAY<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//how long our fountain will spurt color</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> static const VELOCITY<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//how fast our particles shoot up</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> static const GRAVITY<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0.75</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//how quickly are the particles pulled down</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> start_delay<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> time_to_live<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">color</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=uint%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:uint.html"><span style="color: #004993;">uint</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> spread<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> accel<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> cannonball<span style="color: #000000;">&#40;</span>xpos<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> ypos<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> spread_in<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> veloc<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = xpos<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = ypos<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; time_to_live = <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> ratio<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000;">&#40;</span>veloc <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; veloc = <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000066; font-weight: bold;">*</span>ratio<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">180</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; accel = <span style="color: #000066; font-weight: bold;">-</span>veloc<span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; spread = spread_in<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> update<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>time_to_live <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//we're in action</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= spread<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= accel<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000; font-weight:bold;">0.02</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000; font-weight:bold;">0.02</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_to_live<span style="color: #000066; font-weight: bold;">--;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accel <span style="color: #000066; font-weight: bold;">+</span>= GRAVITY<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//remove from screen</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> update<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>This  actionscript 3 source code looks surprisingly like the particle effect setup I covered in a previous tutorial, which can be found right here: <a title="http://flashcove.net/353/particle-effects...structure/" href="http://flashcove.net/353/particle-effects-in-flash-using-an-actionscript3-class-structure/">http://flashcove.net/353/particle-effects&#8230;structure/</a>.  In fact, the only real difference in the update portion is that I&#8217;m shrinking the scale (AKA size) of the cannonball so it looks like it is shrinking off in to the distance.</p>
<p>For the initialization in the <strong>cannonball() </strong>constructor function though, we set the X and Y positions to what was given to us in the passed-in parameters.  What is passed in from the <span style="text-decoration: underline;"><strong>veloc</strong></span> parameter is a number representing where the mouse was at on the Y-axis at the time of calling. Since the height of the screen is 400 pixels, I grab a percentage ratio of how far along the mouse position is, multiply that percentage  by the maximum velocity I want (70), and add it to a base value (180o ensure my balls never get shot below the screen height. I decided on these two values, 70 and 180, by using the actionscript <strong>trace</strong> command and some trial and error on velocity values.</p>
<p>Assuming you have everything setup correctly, and both the cannonball.as code and the main scene code written properly, if you compile and run the flash game via Ctrl + Enter, you will get the current state of the 3D perspective cannonball game..</p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-7" class="efe-flash"><!-- --></div></div>

Point and click anywhere on the screen to fire the cannon!</pre>
<p>PART 2 of this flash game tutorial will be put up online soon, so please stay tuned!  In the next part we will cover the sound effects, colorization, and paint splatter effects that you saw in the flash game demo at the top of the page!</p>
<p>Thanks for tuning in to another great, free, flash game tutorial!</p>
<p>- Dave at the Flash Cove (http://flashcove.net)</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/621/3d-shooter-flash-paint-splatter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Part 2: Laser Beams and Smoke Effects in Flash Games</title>
		<link>http://flashcove.net/577/part-2-laser-beams-and-smoke-effects-in-flash-games/</link>
		<comments>http://flashcove.net/577/part-2-laser-beams-and-smoke-effects-in-flash-games/#comments</comments>
		<pubDate>Tue, 03 May 2011 03:00:16 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[collision]]></category>
		<category><![CDATA[particle]]></category>
		<category><![CDATA[smoke effect]]></category>
		<category><![CDATA[special effect]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=577</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/577/part-2-laser-beams-and-smoke-effects-in-flash-games/">Part 2: Laser Beams and Smoke Effects in Flash Games</a></p><p>Continuing from PART 1 of &#8220;Laser Beams and Smoke Effects in Flash Games,&#8221; we now need to integrate the smoke effects and easter egg collision detection in to our flash...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/577/part-2-laser-beams-and-smoke-effects-in-flash-games/">Part 2: Laser Beams and Smoke Effects in Flash Games</a></p><p>Continuing from <a href="http://flashcove.net/496/laser-and-smoke-effects/">PART 1 of &#8220;Laser Beams and Smoke Effects in Flash Games,&#8221;</a> we now need to integrate the smoke effects and easter egg collision detection in to our flash game!</p>
<p><strong>The final laser and smoke source code can be found right here:</strong></p>
<blockquote><p><strong>Open up the &#8220;lasers_and_smoke.fla&#8221;, and then run it by pressing Ctrl + Enter in Flash.</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/04/lasers_and_smoke.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/04/lasers_and_smoke.fla'>lasers_and_smoke.fla</a></div></div><div class='clear'></div></p></blockquote>
<p><strong>And, as a reminder,  here&#8217;s what were going for with this laser and smoke flash game:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-8" class="efe-flash"><!-- --></div></div>

Click down on the mouse to charge your lasers.  If you hold the laser beam over any
of the Easter eggs or the counter top, you will see the smoke start to bellow out!
</pre>
<p>Once we are caught up with PART 1 of the flash game tutorial,it is time to implement the smoke effect!  To begin, let&#8217;s draw the art.</p>
<p>On the top menu, select Insert -&gt; New Symbol (Ctrl + F8), set it to be a type MovieClip and name it &#8220;<strong>smoke</strong>&#8220;. Inside, draw a small circle (50px by 50px), centered around the origin. For the color, make it a two-tone gradient, with a light gray in the center, fading to a darker gray on the outside. The outside, darker gray; give it a low alpha (10%-ish) to help keep focus on the center color. Settings should look somewhat like the picture below in flash.</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/smoke_internal.png"><img class="aligncenter size-full wp-image-580" title="smoke_internal" src="http://flashcove.net/wp-content/uploads/2011/05/smoke_internal.png" alt="" width="384" height="324" /></a></p>
<p>Once the smoky circle is drawn, we&#8217;re going to insert yet another MovieClip. On the top menu, select Insert -&gt; New Symbol (Ctrl + F8), set it to be a type MovieClip and name it &#8220;<strong>smoke_shell</strong>&#8220;.  Once inside this blank MovieClip shell, drag and drop an instance of the  <strong>&#8220;smoke&#8221;</strong> MovieClip from the library in to the <strong>&#8220;smoke_shell,&#8221;</strong> and center it about the axis point.</p>
<p>Next, select that instance of the smoke on the stage, and add a <strong>Blur Filter</strong> using the &#8220;<strong>Filters</strong>&#8221; submenu (mine&#8217;s on the left-hand side for the Designer Flash CS4 Layout). Set the blur intensity to be 28 for both BlurX and BlurY. You should get something sleek looking like this smoke particle:</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/smoke_blur.png"><img class="aligncenter size-full wp-image-582" title="smoke_blur" src="http://flashcove.net/wp-content/uploads/2011/05/smoke_blur.png" alt="" width="415" height="162" /></a></p>
<p>&nbsp;</p>
<p>Once that&#8217;s done, we need to export the <strong>&#8220;smoke_shell&#8221;</strong> for actionscript.  Find the MovieClip in the flash&#8217;s library tab, Right Click -&gt; Properties, and then check the <strong>&#8220;Export for Actionscript&#8221;</strong> box. Then we should be good to go; time to draw some easter eggs, and then write some actionscript 3 code (AS3).</p>
<p>________________________________________________________________________________</p>
<p>For the easter eggs in the flash game, we have just a single MovieClip, and several frame inside all with different art. Once a brand new instance of the egg is created, it randomly will chose on of the art sets to display. This is a great way to introduce random objects and detail in to your flash games and applications without too much additional effort needed. Just a couple different sets of art, and you are good to go!</p>
<p>With that, on the top menu, select Insert -&gt; New Symbol (Ctrl + F8), set it to be a type MovieClip and name it &#8220;<strong>egg</strong>&#8220;.  Inside I just drew a nice, oblong, egg looking oval, and copied and pasted it across 10 frames. For each frame, draw a little bit of art on each one, each unique, and different colors!</p>
<p>Then, on the first frame, Right Click -&gt; Actions, and insert this line of code:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ceil</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>What that does is guarantees the egg picks, randomly, one of the 10 drawn art sets, and moves it into position upon creation of the instance on our flash stage.</p>
<p>My egg setup looks something like this, which you can grab in the <strong>lasers_and_smoke.fla</strong> file above if you don&#8217;t want to draw something yourself.  (I&#8217;ve also got the shade and shadow layers to give a dark overtone on the egg, make it fit the flash tutorial scene a little nicer.)</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/05/egg_movieclip.png"><img class="aligncenter size-full wp-image-602" title="egg_movieclip" src="http://flashcove.net/wp-content/uploads/2011/05/egg_movieclip.png" alt="" width="315" height="298" /></a></p>
<p>Once we&#8217;re set on the art, head back to the main &#8220;Scene1&#8243; stage. Drag and drop several instances of the egg (from the flash library) on to the stage, in a nice assortment, just like you see in the demo at the top of the page.  Give each instance of the egg a unique name, as follows:  &#8220;<strong>egg1</strong>,&#8221; &#8220;<strong>egg2</strong>,&#8221; &#8220;<strong>egg3</strong>,&#8221; &#8220;<strong>egg4</strong>,&#8221; etc..  Reason for this, inside our actionscript 3 source code, the laser is going to iterate over potential collisions (eggs) to determine if we should draw a flash smoke effect or not.</p>
<p>(I also made a MovieClip containing the &#8216;countertop&#8217; that the eggs are resting on. Feel free to draw one, and drag a single instance on to the stage and label it <strong>&#8216;counter&#8217;</strong> as I did, so it can be accessible by our actionscript 3 code references.)</p>
<p>&nbsp;</p>
<p>After the smoke particles, eggs, and counter tops are drawn in flash, all that&#8217;s left is a little actionscript 3 (AS3) code!</p>
<p>____________________________________________________________</p>
<p>The only thing that needs to be updated in our preexisting actionscript 3 (AS3) code is the <strong>enter_frame</strong> function. In the <strong>else</strong> bracket, we are going to add a couple lines of as3 code, as follows:</p>
<p>FROM THIS:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><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;">else</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; percent_level = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//if we do, keep it at a 1:1 ratio so it doesn't over shoot</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>TO THIS:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><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;">else</span><span style="color: #000000;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0.25</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> iter<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> iter <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">19</span><span style="color: #000066; font-weight: bold;">;</span> iter<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;e&quot;</span><span style="color: #000066; font-weight: bold;">+</span>iter<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">hitTestPoint</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">mouseY</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; make_smoke<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>counter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">hitTestPoint</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; make_smoke<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; percent_level = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//if we do, keep it at a 1:1 ratio so it doesn't over shoot</span><br />
<span style="color: #000000;">&#125;</span><br />
drawLaser<span style="color: #000000;">&#40;</span>percent_level<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//pass the value in</span></div></div>
<p>&nbsp;</p>
<p>I have 19 eggs on my screen, and I labeled them &#8220;e1,&#8221; &#8220;e2,&#8221; &#8220;e3,&#8221; etc.. So, to check collision with my laser effect (just the mouseX/mouseY positions), I iterate over all possibilities  and do a <strong>HitTestPoint()</strong> with my current mouse positions! Once a collsiion is found to be true, we then call the function <strong>make_smoke()</strong> (to be implemented in just a second).  After that collision is found, we then put the line of code <strong>break;</strong></p>
<p>Once that first collision is found, we don&#8217;t need to keep looking because one egg was already found! So to save computation time, we can just exit the for loop and move on to the next task.</p>
<p>Also appended just below the <em>for loop </em>is a quick check against the countertop to see if we are colliding with that too. Nothing new or special.</p>
<p>_____________________________________________</p>
<p>All that&#8217;s needed now is our make_smoke() function, and it&#8217;s helper/destructor function dissipate(). The as3 code is as follows:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> make_smoke<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> smokie<span style="color: #000066; font-weight: bold;">:</span>smoke_shell = <span style="color: #0033ff; font-weight: bold;">new</span> smoke_shell<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; smokie<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; smokie<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">mouseY</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; smokie<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> = <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; smokie<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> = <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; smokie<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>dissipate<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>smokie<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> dissipate<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span>y<span style="color: #000066; font-weight: bold;">-</span>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000; font-weight:bold;">0.01</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">0.0025</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">0.0025</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> <span style="color: #000066; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> dissipate<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">parent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">delete</span> e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">parent</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>In <strong>make_smoke()</strong>, all we do is create a new smoke movieclip instance, set it&#8217;s position to the current mouse position (in addition to a little jiggle so it looks random), shrink the scale down a bit, add the dissipate ENTER_FRAME event listener, and draw it to the screen.</p>
<p>Inside dissipate, we drift the smoke upwards on the screen, fade it&#8217;s alpha out, and grow it in size to give a &#8220;dissipation&#8221; effect in flash.  Once the smoke particle has completely lost visibility on the stage, remove the ENTER_FRAME event listener, remove it from view, and delete the object.</p>
<p>&nbsp;</p>
<p>That&#8217;s it! Compile and run, and you should get something very similar to the demo shown above at the top of this flash game tutorial.</p>
<p>(TO VIEW THE FIRST PART OF THIS TUTORIAL, GO TO THE LINK DIRECTLY BELOW!)</p>
<blockquote><p><a href="http://flashcove.net/496/laser-and-smoke-effects/">http://flashcove.net/496/laser-and-smoke-effects/</a></p></blockquote>
<p>Once again, enjoy the use of the absolutely free flash and as3 source code, and thanks for tuning in!</p>
<p>-Dave at the Flash Cove (http://flashcove.net)</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/577/part-2-laser-beams-and-smoke-effects-in-flash-games/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Custom Cursors and Crosshairs in Flash</title>
		<link>http://flashcove.net/549/custom-cursors-and-crosshairs-in-flash/</link>
		<comments>http://flashcove.net/549/custom-cursors-and-crosshairs-in-flash/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 21:48:57 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Interface Design]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[crosshairs]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[reticule]]></category>
		<category><![CDATA[startDrag]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=549</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/549/custom-cursors-and-crosshairs-in-flash/">Custom Cursors and Crosshairs in Flash</a></p><p>Unique crosshairs, reticules, and cursors are a staple of any flash game. Having something unique to represent mouse movement other than the typical Mac and Window&#8217;s cursors really helps your...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/549/custom-cursors-and-crosshairs-in-flash/">Custom Cursors and Crosshairs in Flash</a></p><p>Unique crosshairs, reticules, and cursors are a staple of any flash game.  Having something unique to represent mouse movement other than the typical Mac and Window&#8217;s cursors really helps your flash game or application stand out. Thankfully too, it is extremely easy to implement! I&#8217;ll show you guys two simple ways to implement a unique custom flash cursor, and you can decide which you like best!</p>
<ul>
<li>First thing, open up a new flash document using Actionscript3 (AS3). I set the dimensions of mine to 300px by 200px, and the framerate up to 30.  Reason being, both the implementations for our custom flash cursor effect depend on the flash application&#8217;s <strong>framerate</strong>, so the higher a<em> framerate</em>, the <em>smoother an effect you will achieve</em>.<a href="http://flashcove.net/wp-content/uploads/2011/04/flash_doc_setup.png"><img class="aligncenter size-full wp-image-553" title="flash_doc_setup" src="http://flashcove.net/wp-content/uploads/2011/04/flash_doc_setup.png" alt="" width="490" height="283" /></a></li>
<li>Step two, let&#8217;s create the art for our custom movieclip!  On the top menu, select<em> Insert -&gt;New Symbol</em> (or press Ctrl+F8), and call it &#8220;<strong>cursor</strong>&#8221; with a <em>MovieClip</em> type.<a href="http://flashcove.net/wp-content/uploads/2011/04/cursor_movieclip.png"><img class="aligncenter size-full wp-image-555" title="cursor_movieclip" src="http://flashcove.net/wp-content/uploads/2011/04/cursor_movieclip.png" alt="" width="439" height="250" /></a></li>
<li>Inside the movieclip, draw whatever custom art you like, but be sure that the small cross on the screen (you center of reference) stays wherever you want the tip of your mouse point to reference to. Personally, I drew a little sword that looks like the one below.   Black bordering, brown handle, with orange-ish plating, and a two-tone gray gradient for the blade itself. See if you can draw something too (probably much better than my sword lol). Notice how I set up the art so that the tip points at the cross inside the MovieClip for center of reference, this is crucial.</li>
<p>	<a href="http://flashcove.net/wp-content/uploads/2011/04/sword_art.png"><img class="aligncenter size-full wp-image-556" title="sword_art" src="http://flashcove.net/wp-content/uploads/2011/04/sword_art.png" alt="" width="368" height="394" /></a></p>
<li>Once the art is complete, it is time to create an instance of our cursor! Back out of the MovieClip by pressing the tiny breadcrumb button &#8220;Scene1&#8243; above your drawspace. Now, find our &#8220;cursor&#8221; movieclip label in the library, and then click and drag onto the stage to create an instance. In the properties manager, set our label to be &#8220;sword_cursor&#8221; as seen in the image below. We need this reference name inside the actionscript3 code.<a href="http://flashcove.net/wp-content/uploads/2011/04/sword_cursor.png"><img class="aligncenter size-full wp-image-559" title="sword_cursor" src="http://flashcove.net/wp-content/uploads/2011/04/sword_cursor.png" alt="" width="289" height="135" /></a></li>
<li> It&#8217;s now time to plug in a little actionscipt3 (AS3) code on the main stage flash frame.  Right click on frame one, and select &#8220;Actions&#8221; down at the bottom. Enter the following code:</li>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sword_cursor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">startDrag</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<a href="http://www.google.com/search?q=mouse%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouse.html"><span style="color: #004993;">Mouse</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">hide</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<li> You could also write the actionscript3 code like this is you prefer it more:</li>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_MOVE</span><span style="color: #000066; font-weight: bold;">,</span>cursor_update<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<a href="http://www.google.com/search?q=mouse%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouse.html"><span style="color: #004993;">Mouse</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">hide</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> cursor_update<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; sword_cursor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; sword_cursor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">mouseY</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<li>One more thing before compiling, we also want a pretty glow effect. Go click on the instance of the &#8220;sword_cursor&#8221; on the stage, and then down in the FILTERS menu, press the small button on the bottom left that looks like a flipped page. The rollover comment says &#8220;Add filter.&#8221; Press that and then select  &#8220;Glow.&#8221;  I left mine in the default mode with the red glow.<a href="http://flashcove.net/wp-content/uploads/2011/04/glow_filter.png"><img class="aligncenter size-full wp-image-562" title="glow_filter" src="http://flashcove.net/wp-content/uploads/2011/04/glow_filter.png" alt="" width="288" height="273" /></a></li>
<li>Once that is done, compile your code via <strong>Ctrl+Enter</strong>, and Voila! You&#8217;ll get the flash application like below:</li>
</ul>
<p><strong>The demo of this application is here:</strong></p>
<pre><center>
<div style="border: 2px solid #000000; width: 300px;"><div id="efe-swf-9" class="efe-flash"><!-- --></div></div>

</center></pre>
<p>Pretty simple, huh? To get your own art into the cursor, just modify the <strong>&#8220;cursor&#8221;</strong> MovieClip to get something new.</p>
<p>Maybe something like this..</p>
<p> 
<pre><center>
<div style="border: 2px solid #000000; width: 300px;"><div id="efe-swf-10" class="efe-flash"><!-- --></div></div>

</center></pre>
<p>or even this..?</p>
<p> 
<pre><center>
<div style="border: 2px solid #000000; width: 300px;"><div id="efe-swf-11" class="efe-flash"><!-- --></div></div>

</center></pre>
<p>Either way, thanks for tuning in.</p>
<p>Thanks again!</p>
<p>-Dave at the FlashCove (http://flashcove.net)</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/549/custom-cursors-and-crosshairs-in-flash/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Laser Beams and Smoke Effects in Flash Games</title>
		<link>http://flashcove.net/496/laser-and-smoke-effects/</link>
		<comments>http://flashcove.net/496/laser-and-smoke-effects/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 01:48:54 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[particle]]></category>
		<category><![CDATA[smoke effect]]></category>
		<category><![CDATA[special effect]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=496</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/496/laser-and-smoke-effects/">Laser Beams and Smoke Effects in Flash Games</a></p><p>OMG LAZORZ!!! This tutorial covers the development of a simple laser beam effect in Flash.. and along with that, the generation of a smoke particle effect in AS3 to add...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/496/laser-and-smoke-effects/">Laser Beams and Smoke Effects in Flash Games</a></p><p><em><strong>OMG LAZORZ!!!</strong></em> This tutorial covers the development of a simple laser beam effect in Flash.. and along with that, the generation of a smoke particle effect in AS3 to add to the scene&#8217;s mood.  Lasers are a fun feature to add to any video game, and using the knowledge from this flash AS3 tutorial and a little more polish, you will be able to create a truly awesome laser beam flash game.</p>
<p>Not only that.. but since this is so close to the holiday.. IT&#8217;S EASTER THEMED! (Kinda.)</p>
<p><strong>Source code can be found right here:</strong></p>
<blockquote><p><strong>Open up the &#8220;lasers_and_smoke.fla&#8221;, and then run it by pressing Ctrl + Enter in Flash.</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/04/lasers_and_smoke.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/04/lasers_and_smoke.fla'>lasers_and_smoke.fla</a></div></div><div class='clear'></div></p></blockquote>
<p><strong>And a demo of this application is here:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-12" class="efe-flash"><!-- --></div></div>

Click down on the mouse to charge your lasers.  If you hold the laser beam over any
of the Easter eggs or the counter top, you will see the smoke start to bellow out!</pre>
<p>TUTORIAL STEPS:</p>
<ol>
<li>First and foremost, we need something to shoot lasers from. What I used in this tutorial was the drawing of a skull I made. You can either grab that right here, save as, and import into your flash project; or simply use the assets form the completed tutorial <strong>lasers_and_smoke.fla</strong> file I referenced above.<a href="http://flashcove.net/wp-content/uploads/2011/04/skull.png"><img class="alignright size-full wp-image-505" title="skull" src="http://flashcove.net/wp-content/uploads/2011/04/skull.png" alt="" width="100" height="120" /></a></li>
<li>Place the skull somewhere in the upper left-hand corner of the stage. We&#8217;re going to have to calculate the exact position of his eyes later on to anchor our flash laser effect down.</li>
<li>Once the skull is in place, we are going to need to set up a global Boolean, and three event listeners, Event.ENTER_FRAME, MouseEvent.MOUSE_DOWN, and MouseEvent.MOUSE_UP.  When the mouse goes down, flip on the global boolean, when it goes up, flip the boolean off. This will give us a real-time response rate for the laser effect, and almost all flash game input is setup using this type of framework.  The code skeleton will look something like this!</li>
</ol>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> mouseState<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// true for down, false for up</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> enter_frame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_DOWN</span><span style="color: #000066; font-weight: bold;">,</span> mouse_down<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span> mouse_up<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> mouse_down <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; mouseState = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> mouse_up <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; mouseState = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> enter_frame <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>mouseState<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//logic for when the mouse is down</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//AKA draw lasers and do hit detect for smoke</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>We&#8217;ve got the two listeners for switching mouse states, and ENTER_FRAME checking that boolean. If the boolean <strong>mouseState </strong>is true, execute laser generation sequence.</p>
<p>4.  When it is time to create the laser, we have got to pick one of two options.  We can either have the laser effect appear <em><strong>instantly</strong></em>, hitting our target mouse position, or have the laser &#8220;<em><strong>beam out</strong></em>&#8221; to the position over a short series of frames. Obviously, the beam out effect looks much cleaner, and since it isn&#8217;t too much more actionscript 3 code to enter, we&#8217;ll go ahead and implement the animated laser beam pullout.</p>
<p>With that, we need three more global variables, laserbeam1 MovieClip, laserbeam2 MovieClip, and our &#8220;percentage&#8221; Number, to show how far along our beam generation has proceeded. The actionscript 3 code looks like this (placed at the top of the file):</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> laserbeam1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> laserbeam2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> percent_level<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>Since we are creating these two laser beams dynamically, we also need to reserve a block of memory for them, and add to stage. Our new (and final) initialization function now looks like this:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; laserbeam1 = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; laserbeam2 = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html"><span style="color: #004993;">MovieClip</span></a><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: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>laserbeam1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>laserbeam2<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> enter_frame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_DOWN</span><span style="color: #000066; font-weight: bold;">,</span> mouse_down<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span> mouse_up<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Looking sharp! Next step.</p>
<p>5.  Let us go ahead and create a function called <strong>drawLaser()</strong>.  With this separate function piece, we can call just this one line in our ENTER_FRAME event, and the code will cleanly execute every time. It looks nicer, and is easier to understand for our fellow readers.</p>
<p>First thing&#8217;s first, since our skull has TWO eye sockets, we are going to draw TWO laser beams (of course that&#8217;s why we create two instances in the init() function).  Our laser beams are completely procedural.. no tweening or pre-created movieclips. Since everything is dynamically drawn, it is easy to tweak, and easy to update at your convenience. To update a color, simply change one line; to change the width, just update one variable, etc.. I hope, after seeing many of the flash tutorials on flashcove.net, you see there are great benefits in creating things dynamically. Not only is it faster, but also more customizable.</p>
<p>The laser beam effect in this flash game tutorial consists of a single line drawn, in addition to a blur and glow filter. The line is a dark red, the glow is a light red, and the blur effect gives softer edges for a polished representation. This is done as follows:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> drawLaser<span style="color: #000000;">&#40;</span>percent<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; laserbeam1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//first clear the old drawings (if any)</span><br />
&nbsp; &nbsp; laserbeam1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">,</span>0xF75D59<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//set line settings to 4px width, and a darkish red</span><br />
<br />
&nbsp; &nbsp; laserbeam1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">60</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">65</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">///This is where we start drawing</span><br />
&nbsp; &nbsp; laserbeam1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>mouseX<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">60</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>percent<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">65</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>mouseY<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">65</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>percent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//this is where we draw to</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> glowy<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=glowfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:glowfilter.html"><span style="color: #004993;">GlowFilter</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=glowfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:glowfilter.html"><span style="color: #004993;">GlowFilter</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//init a glow filter</span><br />
&nbsp; &nbsp; glowy<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = 0xE77471<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//set a soft red color</span><br />
&nbsp; &nbsp; glowy<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurX</span> = <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//blur the glow</span><br />
&nbsp; &nbsp; glowy<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurY</span> = <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; glowy<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">strength</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//keep standard strength value</span><br />
&nbsp; &nbsp; glowy<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">quality</span> = <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//redraw 3 times to make high quality glow</span><br />
&nbsp; &nbsp; glowy<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">0.2</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//set a low alpha so it doesn't look too invasive</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> blurFilter<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=blurfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:blurfilter.html"><span style="color: #004993;">BlurFilter</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=blurfilter%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:blurfilter.html"><span style="color: #004993;">BlurFilter</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//init a blur filter</span><br />
&nbsp; &nbsp; blurFilter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurX</span> = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//set standard blur values</span><br />
&nbsp; &nbsp; blurFilter<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">blurY</span> = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> filtersArray<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span>glowy<span style="color: #000066; font-weight: bold;">,</span> blurFilter<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//this&quot;filterArray&quot; stores both the blur and glow</span><br />
&nbsp; &nbsp; laserbeam1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = filtersArray<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//assign to laser1</span><br />
<br />
&nbsp; &nbsp; laserbeam2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//clear laser2</span><br />
&nbsp; &nbsp; laserbeam2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">,</span>0xF75D59<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//set it to the same red color</span><br />
&nbsp; &nbsp; laserbeam2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">82</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">65</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">///This is where we start drawing</span><br />
&nbsp; &nbsp; laserbeam2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">82</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>mouseX<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">82</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>percent<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">65</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>mouseY<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">65</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>percent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//this is where we draw to</span><br />
&nbsp; &nbsp; laserbeam2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = filtersArray<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//assign the same blur and glow filters</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>The comments are pretty self explanatory for most of this code, but the biggest thing to be noted is that we are <strong>passing a parameter in to the function</strong>. <em>This &#8220;percent&#8221; is what we use to draw the progressive laser animation upon start-up</em>.</p>
<p>When we call <strong>drawLaser()</strong> every frame in the main EVENT_FRAME function, we have to pass in the global<strong> percent_level </strong>so that <strong>percent</strong> has the correct value to use.  The numbers (60,65) and (82,65) that you see in my code example are the specific pixel locations of the skull eye sockets. If you&#8217;re implementing this yourself, you&#8217;ll have to manually determine those locations through some trial and error (Either that, or just use the <strong>lasers_and_smoke.fla </strong>file located at the top).</p>
<p>6. We have got the flash laser generation function coded up, now what we need to do is calculate the percentage for the laser beam tween effect.  We need to increment the percentage force inside enter_frame, and we also need to reset it to zero every time the mouse button is lifted, as we have to start over again.</p>
<p>The new code for <strong>enter_frame</strong>, therefore, looks like this:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> enter_frame <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>mouseState<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>percent_level <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #009900; font-style: italic;">//if we don't have full power beam, crank it up</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; percent_level <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">0.34</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; percent_level = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//if we do, keep it at a 1:1 ratio so it doesn't over shoot</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; drawLaser<span style="color: #000000;">&#40;</span>percent_level<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//pass the value in</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>and the new mouse_up function looks like this (we also have to clear the graphics on MOUSE_UP too):</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> mouse_up <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html"><span style="color: #004993;">MouseEvent</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; laserbeam1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//clear the laser1 graphic</span><br />
&nbsp; &nbsp; laserbeam2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//clear the laser2 graphic</span><br />
&nbsp; &nbsp; percent_level = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//reset our charge</span><br />
&nbsp; &nbsp; mouseState = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//turn off the boolean</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Of course, at the very bottom of our actionscript file, we need 1 more line:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>&nbsp;</p>
<p>Throwing all of this code together, and compiling your flash file via Ctrl+Enter, gives us the following laser effect in flash!</p>
<p>&nbsp;</p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-13" class="efe-flash"><!-- --></div></div>
</pre>
<p>For the smoke effects in our flash game application, in addition to the Easter Egg collision detection, please keep on the lookout for <strong>part two of this laser and smoke effects in Flash Tutorial on flashcove.net!</strong></p>
<p>&nbsp;</p>
<p>Thanks for tuning in! (And have a Happy Flash Loaded Easter!)</p>
<p>(TO VIEW THE SECOND PART OF THIS TUTORIAL, GO TO THE LINK DIRECTLY BELOW!)</p>
<blockquote><p><a href="http://flashcove.net/577/part-2-laser-beams-and-smoke-effects-in-flash-games/">http://flashcove.net/577/part-2-laser-beams-and-smoke-effects-in-flash-games/</a></p></blockquote>
<p>-Dave at the FlashCove (http://flashcove.net)<strong><br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/496/laser-and-smoke-effects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elastic Collision Detection between Entities in Flash AS3</title>
		<link>http://flashcove.net/452/elastic-collision-detection-and-resolution-between-entities-in-flash-as3/</link>
		<comments>http://flashcove.net/452/elastic-collision-detection-and-resolution-between-entities-in-flash-as3/#comments</comments>
		<pubDate>Sat, 23 Apr 2011 18:22:04 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[collision detection]]></category>
		<category><![CDATA[elastic collision]]></category>
		<category><![CDATA[physics]]></category>

		<guid isPermaLink="false">http://flashcove.net/?p=452</guid>
		<description><![CDATA[<p><a href="http://flashcove.net/452/elastic-collision-detection-and-resolution-between-entities-in-flash-as3/">Elastic Collision Detection between Entities in Flash AS3</a></p><p>What we have got here today is a nice simple example of some Flash AS3 physics source code! Real-time collision between several varying size and mass circles. Along with that...</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://flashcove.net/452/elastic-collision-detection-and-resolution-between-entities-in-flash-as3/">Elastic Collision Detection between Entities in Flash AS3</a></p><p>What we have got here today is a nice simple example of some Flash AS3 physics source code!  Real-time collision between several varying size and mass circles.  Along with that of course, is some boundary detection to keep our floating objects in frame of view.  This flash physics source code is simple to read, and easy to understand. Coming in at not even 100 lines of actionscript code, you&#8217;ll be well on your way to more advanced examples of elastic collision in applications and flash games.</p>
<p><strong>Source code can be found right here:</strong></p>
<blockquote><p><strong>Open up the &#8220;circle_collision.fla&#8221;, and then run it by pressing Ctrl + Enter in Flash.</strong></p>
<p><div class='documentIcons'><div class='documentIcons_icon'><a href='http://flashcove.net/wp-content/uploads/2011/04/circle_collision.fla'><img src='http://flashcove.net/wp-content/plugins/attachment-file-icons/mime/fla-icon.png'/></a></div><div class='documentIcons_link'><a href='http://flashcove.net/wp-content/uploads/2011/04/circle_collision.fla'>circle_collision.fla</a></div></div><div class='clear'></div></p></blockquote>
<p><strong>And a demo of this application is here:</strong></p>
<pre>
<div style="border: 2px solid #000000; width: 550px;"><div id="efe-swf-14" class="efe-flash"><!-- --></div></div>
</pre>
<p>Now, in case you need a refresher on what <strong>&#8220;elastic collision&#8221;</strong> actually is, <em>its a transfer of momentum between two moving (or static) objects</em>.  When the balls bounce in the flash physics demo above, they don&#8217;t just simply bounce off each other, there is actually a transfer of energy between the two. Mass has an effect on what happens, and if a tiny ball is moving incredibly fast, and collides with a nearly static large ball, the large one will get a small push, absorbing most of the energy into its mass rather than into its momentum/velocity.  To achieve this flash physics effect though, is actually quite simple, and doesn&#8217;t take much more as3 code rather than implementing collision with the lack of elasticity.</p>
<p>With that, let&#8217;s begin.</p>
<ol>
<li>After opening up a new default 550px by 400px, AS3 file; draw a nice round circle with a thick black border (maybe stroke of 8-12). <strong>Select -&gt; Right Click -&gt; Convert to Movieclip</strong>, and label it as <strong>&#8220;circle&#8221;</strong>.  Before pressing okay though, check the<em><strong> &#8220;Export for Actionscript&#8221;</strong></em>! You can keep it as the default &#8220;circle&#8221; export name, we&#8217;ll be using that reference in the code.<a href="http://flashcove.net/wp-content/uploads/2011/04/make_circle.png"><img class="aligncenter size-full wp-image-468" title="make_circle" src="http://flashcove.net/wp-content/uploads/2011/04/make_circle.png" alt="" width="510" height="278" /></a></li>
<li>Once the circle movieclip is created, we need to convert the<strong> inner_circle</strong> as well.  Inside the &#8220;circle&#8221; movieclip, click to select the inner, colored part; and then  <strong>Right Click -&gt; Convert to Movieclip</strong>, and label it as <strong>&#8220;inner_circle&#8221;</strong>.  That specific instance of the inner_circle should be named &#8220;<strong>inner</strong>&#8220;.  This is the way we are going to reference it in the code, through circle.inner (using a pointer).<a href="http://flashcove.net/wp-content/uploads/2011/04/make_inner.png"><img class="aligncenter size-full wp-image-472" title="make_inner" src="http://flashcove.net/wp-content/uploads/2011/04/make_inner.png" alt="" width="490" height="230" /></a></li>
<li>Once these two embedded movieclips are successfully in your library, it is time to write the code.  No circles instances are needed on the stage, you can leave it blank.</li>
<li>The actionscript 3 source code is actually pretty simple, just two functions and some initialization.</li>
</ol>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><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.geom</span><span style="color: #000066; font-weight: bold;">.</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> NUMBER_OF_CIRCLES<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">15</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> circle_array<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> vector_array<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> mass_array<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>The import call is so we can use the Point class for distance calculations, it allows for more optimized code, and easy distance calculation.  NUMBER_OF_CIRCLES is just a constant to tell the rest of the actionscript program how many circles we want bouncing around.</p>
<p>And the last thing, 3 arrays.  <strong>circle_array</strong> actually contains all of our AS3 MovieClip instances.  <strong>vector_array</strong> is multidimensional, each array index contains two values in [0] and [1]; the x and y velocities for our specific circle.  The last one, mass_array contains masses for each circle, so we know how collisions can resolve in regards to momentum transfer.</p>
<p>Next up is the<strong> initialization function</strong>:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; circle_array = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; vector_array = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; mass_array = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> NUMBER_OF_CIRCLES<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> circle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">15</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">550</span> <span style="color: #000066; font-weight: bold;">-</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span> <span style="color: #000066; font-weight: bold;">-</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> new_color<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=uint%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:uint.html"><span style="color: #004993;">uint</span></a> = 0x000077 <span style="color: #000066; font-weight: bold;">*</span> <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; new_color <span style="color: #000066; font-weight: bold;">+</span>= 0x000088<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">switch</span><span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">%</span>3<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_color = new_color <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_color = new_color <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//new_color = new_color &lt;&lt; 16;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> myColor<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=colortransform%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:colortransform.html"><span style="color: #004993;">ColorTransform</span></a> = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">transform</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">colorTransform</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myColor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">color</span> = new_color<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">inner</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">transform</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">colorTransform</span> = myColor<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id = i<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; vector_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mass_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.10</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> circle_update<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Obviously, first, we need to initialize the three arrays. Once inside the loop, quite a few things happen.</p>
<p>First, we call the constructor for the new circle, and insert it into the array, according to the loop variable <strong>i</strong>.</p>
<p>Set a random width so we get variate sizes of circles (minimum 15, max 40), and set the height equal to the same since circles are equal width and height (just the diameter).</p>
<p>Next thing, is a random position on the screen. Of course we want to constrain it into the region of view, so that&#8217;s the reason for all the accessing of the circle&#8217;s widths and heights, so we can be also close as possible to the edge without actually crossing over.</p>
<p>The next portion, about color, is easily the most confusing thing.  First, I set <strong>new_color</strong> to a random value between 0&#215;000000 and 0&#215;000077, which, according to our RGB setup, gives us a dark blue to black color. I then add 0&#215;000088 to the mix, setting<strong> new_color</strong> now to a medium to extremely bright blue color.  The switch case breaks our creation of circles into three discrete chunks. A set that, when divided by three, has remainder 0, 1 ,or 2.  If the remainer is zero, I&#8217;m shifting left by 16 binary bits, which gives us a value between 0&#215;880000 and 0xFF0000 (red!). If remainder is one, I&#8217;m shifting by 8 binary bits, giving us the new range 0&#215;008800 to 0x00FF00 (green!). If remainder is two, I don&#8217;t do anything (leave it blue!).  Set the <strong>new_color</strong> to <strong>circle_array[i].inner</strong> to actually update our inner_circle, and we&#8217;re on to the next step.</p>
<p>addChild(circle_array[i]) actually puts our instance of the circle on the stage so it can be seen.</p>
<p>We set the id of the circle to be the number it was assigned with, for cleanliness.</p>
<p>And then store the data for the other two arrays.  In vector array, we initialize a sub-array of length two, each containing values between 0-10. The first represents our x velocity, the second our y velocity.  As for the mass, we set it to 1/10th the radius. While the 10% is arbitrary, it keeps the consistent ratio that, the bigger the circle, the more mass it has.</p>
<p>Once outside of the loop, we add the ENTER_FRAME event listener to our other function, so the balls actually can be updated from frame-to-frame.</p>
<p>That is it for the initialization of this actionscript3 source code.</p>
<p>__________________________________________________________________________________________</p>
<p>The final function, <strong>circle_update</strong>!</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> circle_update<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html"><span style="color: #004993;">Event</span></a><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> NUMBER_OF_CIRCLES<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= vector_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= vector_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//bounding wall collisions</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">550</span> <span style="color: #000066; font-weight: bold;">-</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp;circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">400</span> <span style="color: #000066; font-weight: bold;">-</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//ball to ball collisions</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> g<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> g <span style="color: #000066; font-weight: bold;">&lt;</span> NUMBER_OF_CIRCLES<span style="color: #000066; font-weight: bold;">;</span> g<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> g_pos<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> circle_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> k<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = g <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> k <span style="color: #000066; font-weight: bold;">&lt;</span> NUMBER_OF_CIRCLES<span style="color: #000066; font-weight: bold;">;</span> k<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> dist = <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">distance</span><span style="color: #000000;">&#40;</span>g_pos<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=point%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:point.html"><span style="color: #004993;">Point</span></a><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> circle_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> dist <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//they're colliding!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//elastic response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> mass1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = mass_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> mass2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = mass_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> mass_sum = mass1 <span style="color: #000066; font-weight: bold;">+</span> mass2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> velX1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> velX2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> velY1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> velY2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> new_velX1 = <span style="color: #000000;">&#40;</span>velX1 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass1 <span style="color: #000066; font-weight: bold;">-</span> mass2<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass2 <span style="color: #000066; font-weight: bold;">*</span> velX2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> new_velX2 = <span style="color: #000000;">&#40;</span>velX2 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass2 <span style="color: #000066; font-weight: bold;">-</span> mass1<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass1 <span style="color: #000066; font-weight: bold;">*</span> velX1<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> new_velY1 = <span style="color: #000000;">&#40;</span>velY1 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass1 <span style="color: #000066; font-weight: bold;">-</span> mass2<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass2 <span style="color: #000066; font-weight: bold;">*</span> velY2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> new_velY2 = <span style="color: #000000;">&#40;</span>velY2 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass2 <span style="color: #000066; font-weight: bold;">-</span> mass1<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass1 <span style="color: #000066; font-weight: bold;">*</span> velY1<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = new_velX1<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = new_velX2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = new_velY1<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = new_velY2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= new_velX1<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= new_velY1<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= new_velX2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= new_velY2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>&nbsp;</p>
<p>Next up for this actionscript3 source code is this update function.  Let&#8217;s go through the source in some detail.</p>
<p>First thing that happens in the loop is I update the position of my circle via it&#8217;s X and Y velocities. Pretty simple.</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= vector_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= vector_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>Next up are the boundary collisions against the four walls.  The special thing about these is that all the circles have varying radii, so we have to be picky about testing for the boundary collisions in flash.</p>
<p>Since the circles are indexed from their absolute center, when a ball is touching the boundary, it&#8217;s actually the<strong> x-position PLUS the radius</strong>. Once collision is detected, we simple need to reset the circle&#8217;s position to the edge (precaution in case the circle was moving extremely fast, and will thereby get pinned outside the stage), adn flip the corresponding velocity.  Flip X for the left and right walls, flip Y for the top and bottom walls. That&#8217;s it!</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">//bounding wall collisions</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">550</span> <span style="color: #000066; font-weight: bold;">-</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp;circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">400</span> <span style="color: #000066; font-weight: bold;">-</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000;">&#40;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> vector_array<span style="color: #000000;">&#91;</span>circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = circle_array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.50</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp; <span style="color: #000000;">&#125;</span></div></div>
<p>Only thing left is our elastic collisions, the meat of this flash actionscript 3 tutorial. Since every single circle needs to view every other circle, every single frame, we need a double embedded for loop.</p>
<p>So, if I have 10 circles, the inner loop will run 10 times, and for the second, it will run 9 (as we don&#8217;t need to repeat test with the first ball), for the third, 8, and so on..  Giving us a final loop count of 10+9+8+7+6+5+4+3+2+1 = 55!  In general purpose, this is (n^2 + n) / 2  which, in just general asymptotic running time, is O(n^2).  Not too bad I suppose, but if you&#8217;re running this on a slower PC, you may have frame-rate issues for a large amount of colliding objects.</p>
<p>Before we start the second, internal <strong>for loop</strong>, we store a copy of the first circle&#8217;s position in a temporary<strong> Point </strong>object, for quick reference. Given any two circles, of any size, collision can be tested very easily.  If the distance between the two circles (from their centers) is less than the sum of circle1&#8242;s radius and circle2&#8242;s radius, they&#8217;re touching!<a href="http://flashcove.net/wp-content/uploads/2011/04/circle_collide.png"><img class="aligncenter size-full wp-image-484" title="circle_collide" src="http://flashcove.net/wp-content/uploads/2011/04/circle_collide.png" alt="" width="439" height="219" /></a></p>
<p>Once a flash entity collision is confirmed, we need to find the resulting forces and directions for each circle.  What I am following in this program is an Elastic collision setup. A nice, general purpose formula can be seen right here..</p>
<p><a href="http://flashcove.net/wp-content/uploads/2011/04/elastic_formula.png"><img class="aligncenter size-full wp-image-486" title="elastic_formula" src="http://flashcove.net/wp-content/uploads/2011/04/elastic_formula.png" alt="" width="232" height="47" /></a></p>
<p>v1 is our new velocity, u1 is our old velocity, u2 is the other circle&#8217;s old velocity, and m1 and m2 are the respective masses for each.</p>
<p>This is the exact equation we need, but<strong> it only accounts for one dimension, of one circle</strong>. We need to do this calculation four times, for X on circle1, Y on circle1, X on circle2, and Y on circle2. But.. we have all the data needed to complete the calculations, and converting this formula to actionscript 3 code, we get the following:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">//elastic response</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> mass1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = mass_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> mass2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = mass_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> mass_sum = mass1 <span style="color: #000066; font-weight: bold;">+</span> mass2<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> velX1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> velX2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> velY1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> velY2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #6699cc; font-weight: bold;">var</span> new_velX1 = <span style="color: #000000;">&#40;</span>velX1 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass1 <span style="color: #000066; font-weight: bold;">-</span> mass2<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass2 <span style="color: #000066; font-weight: bold;">*</span> velX2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> new_velX2 = <span style="color: #000000;">&#40;</span>velX2 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass2 <span style="color: #000066; font-weight: bold;">-</span> mass1<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass1 <span style="color: #000066; font-weight: bold;">*</span> velX1<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> new_velY1 = <span style="color: #000000;">&#40;</span>velY1 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass1 <span style="color: #000066; font-weight: bold;">-</span> mass2<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass2 <span style="color: #000066; font-weight: bold;">*</span> velY2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> new_velY2 = <span style="color: #000000;">&#40;</span>velY2 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>mass2 <span style="color: #000066; font-weight: bold;">-</span> mass1<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">*</span> mass1 <span style="color: #000066; font-weight: bold;">*</span> velY1<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> mass_sum<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = new_velX1<span style="color: #000066; font-weight: bold;">;</span><br />
vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = new_velX2<span style="color: #000066; font-weight: bold;">;</span><br />
vector_array<span style="color: #000000;">&#91;</span>g<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = new_velY1<span style="color: #000066; font-weight: bold;">;</span><br />
vector_array<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = new_velY2<span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>&nbsp;</p>
<p>Now, that last part where we actually<strong> update positions again, a second time in the frame</strong>, we need to do this to prevent the circles from &#8220;sticking&#8221; to each other.  If both balls are heading the same direction and collide, and we don&#8217;t update position a second time, there&#8217;s a much higher chance that the circles will end up overlapping, bouncing around the screen together and clumped, which looks bad.  We separate them by an extra boost to help prevent this output. To see what I&#8217;m talking about, remove those couple lines of code, and check it out in the flash tutorial demo.</p>
<p>&nbsp;</p>
<p>Once more line of code though before you can compile, you need to actually call the initialization function!</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:535px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>At that, we are officially done!  The background art was something I just quickly sketched for presentation, and instead of a black ring around the circles, I&#8217;ve got a glowy gradient. But I&#8217;ll leave that to you guys (or just steal it from the source lol).</p>
<p>This type of physics is perfect for a bouncy type flash game, or maybe a pool flash game. All you&#8217;d need to do is change the art,  fire velocities only on hitting the cue ball, etc.. and of course a points system.</p>
<p>I hope you enjoyed this collision detection flash tutorial, and also hope you like the free actionscript3 source code.</p>
<p>&nbsp;</p>
<p>-Dave at the Flash Cove (http://flashcove.net)</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://flashcove.net/452/elastic-collision-detection-and-resolution-between-entities-in-flash-as3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: flashcove.net @ 2013-05-19 17:40:04 -->