<?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>Mr Sun Studios &#187; Intermediate Tutorials</title>
	<atom:link href="http://www.mrsunstudios.com/category/tutorials/intermediate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mrsunstudios.com/blog</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Tue, 28 Jul 2009 15:03:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 7</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-7/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-7/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:07:04 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1215</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 7: Finishing Touches Well, now for the last step, adding the finishing touches. Just like in my previous tutorial, I won&#8217;t really go into much description in this section, in hopes that you can [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 7: Finishing Touches</h3>
<p>Well, now for the last step, adding the finishing touches. Just like in my previous tutorial, I won&#8217;t really go into much description in this section, in hopes that you can figure out how to do these without my help. As always, the source file is always available at the bottom.</p>
<p>We haven&#8217;t displayed the score to the user yet, so we&#8217;ll do that first. It&#8217;ll be mad easy. Just create a dynamic text field (make sure to embed the font) and set the &#8220;Instance Name&#8221; field to <tt>txtScore</tt>. Of course, when you do this, and error pops up because of the <tt>for</tt> loop that we have. To fix this, just create an entire MovieClip called <tt>mcText</tt> and place all of the dynamic textfields in there.  I&#8217;ll leave the rest of the coding to you for this.</p>
<p>We also have to show the <tt>scoreString</tt>. We can also do this with a dynamic text field. This time, however, the &#8220;Instance Name&#8221; field should be set to <tt>txtScoreString</tt>. Also, we&#8217;ve got to set the scoreString to &#8220;Bad&#8221; whenever the user misses the arrow or hits it at the wrong time. Hopefully you can figure out yourself how to do that yourself.</p>
<p>Now, to show the combo to the player. We first have to define a <tt>combo</tt> variable at the top and set it to 0. Then, we have to increment it every time the user hits the right key, and reset it every time the user doesn&#8217;t. Then, we can just place a dynamic text field that shows the combo where ever we want.</p>
<p>Next, we&#8217;re going to have a win screen and a lose screen. Just make two frames, one labeled &#8220;win&#8221; and the other labeled &#8220;lose&#8221;. Have the user navigate to &#8220;win&#8221; when the game is over, and to &#8220;lose&#8221; when the user loses. It&#8217;s pretty straightforward. If you want to, you can display the stats. Then, make a button that will navigate back to the menu.</p>
<p>Finally, there is one last thing to do. If you haven&#8217;t noticed, whenever we start a game, the arrows don&#8217;t work unless you click on the background first. Simply add this code to the top of the &#8220;game&#8221; frame to fix it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">focus</span> = <span style="color: #0066CC;">stage</span>;</pre></div></div>

<p>Well, now we&#8217;re basically done. Of course, this is just the beginning of making a game like this. You must figure out everything else on your own if you want to become a real programmer.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt7/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt7/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt7/source.zip">Source Files (zipped)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-7/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-6/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-6/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:06:53 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1210</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 6: Scoring The next step in making our rhythm game is scoring. The user will get points depending on how far away the arrow is from the receptor when it hits it. We&#8217;re also [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 6: Scoring</h3>
<p>The next step in making our rhythm game is scoring. The user will get points depending on how far away the arrow is from the receptor when it hits it. We&#8217;re also going to make the health bar gain and lose parts when the user hits or misses the receptor. First of all, we need to define a score variable at the top of the game frame. We also have to define a String variable that will show how well the user hits the arrow.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//the score variable</span>
<span style="color: #000000; font-weight: bold;">var</span> score:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #808080; font-style: italic;">//either perfect, great, nice, or good</span>
<span style="color: #000000; font-weight: bold;">var</span> scoreString:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">''</span>;</pre></div></div>

<p>That was pretty easy. Next, we want to increase the score whenever the user hits the right key when it hits the score. We&#8217;re going to make it increase by 10 for a perfect hit, 8 for a great hit, 6 for an nice hit, and 4 for a good hit.  Add this code to the &#8220;Arrow.as&#8221; class in the <tt>checkKeys()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkKeys<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//checking if a certain key is down and it's touching the receptor</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == arrowCode <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>				
		<span style="color: #808080; font-style: italic;">//checking if the correct key is down</span>
		<span style="color: #808080; font-style: italic;">//if the user hits it about perfectly</span>
		<span style="color: #808080; font-style: italic;">//the receptors y coordinate is about 10 px away from the</span>
		<span style="color: #808080; font-style: italic;">//symbol's y coordinate.</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">15</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">10</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Perfect'</span>;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">25</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span>-<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">8</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Great'</span>;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> + <span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span>.<span style="color: #006600;">y</span> -<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">6</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Nice'</span>;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">4</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">scoreString</span> = <span style="color: #ff0000;">'Good'</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove it from stage</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The next thing we have to do now is program the health bar. If the user misses or hits the arrow key at the wrong time, then we have to lose health. If the user hits it right though, the health will increase. First of all, give the health bar an instance name, <tt>mcHealth</tt>. Then, we&#8217;re going to make a function for this, so we don&#8217;t have to write all the if statements repeatedly. Define this function at the bottom of your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//this function will change the health depending on how much health change</span>
<span style="color: #808080; font-style: italic;">//it needs, positive or negative</span>
<span style="color: #000000; font-weight: bold;">function</span> changeHealth<span style="color: #66cc66;">&#40;</span>healthDiff:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	healthDiff <span style="color: #66cc66;">/</span>= <span style="color: #cc66cc;">100</span>;<span style="color: #808080; font-style: italic;">//the scaleX only changes in percentages</span>
	<span style="color: #808080; font-style: italic;">//checking if the health is already at it's full</span>
	<span style="color: #808080; font-style: italic;">//or will be full after this hit</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcHealth.<span style="color: #006600;">scaleX</span> + healthDiff <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		mcHealth.<span style="color: #006600;">scaleX</span> = <span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcHealth.<span style="color: #006600;">scaleX</span> + healthDiff <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//checking if the health will be equal or below 0</span>
		<span style="color: #808080; font-style: italic;">//lose function will go here</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if there are no problems</span>
		mcHealth.<span style="color: #006600;">scaleX</span> += healthDiff;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The next thing we&#8217;re going to do is run this function positively when the user hits the key correctly. In the arrow&#8217;s code where it hit tests with the receptor, put this code for perfect:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">changeHealth<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This will increase the health by 5. You can do the rest yourself, just keep on decrementing the health change by 1, and you&#8217;ll be good.</p>
<p>Next comes the hard part, making the user lose health. Again, we have to define a few variables at the top. They will be Booleans saying whether or not the receptor is touching a certain key.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Booleans checking if the arrows are touching the receptor</span>
<span style="color: #000000; font-weight: bold;">var</span> touchLeft:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #000000; font-weight: bold;">var</span> touchUp:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #000000; font-weight: bold;">var</span> touchDown:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #000000; font-weight: bold;">var</span> touchRight:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;</pre></div></div>

<p>Now, add this code to the <tt>makeLvl()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//checking if mcReceptor is touching any arrows</span>
	<span style="color: #808080; font-style: italic;">//first we reset the variables we got last time just in case they aren't true anymore</span>
	touchLeft = <span style="color: #000000; font-weight: bold;">false</span>;
	touchUp = <span style="color: #000000; font-weight: bold;">false</span>;
	touchDown = <span style="color: #000000; font-weight: bold;">false</span>;
	touchRight = <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #808080; font-style: italic;">//this for loop will be used for the hit testing</span>
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>numChildren;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">target</span>:<span style="color: #0066CC;">Object</span> = getChildAt<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">target</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span>mcReceptor<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the object is an arrow and is touching the receptor</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">37</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if left arrow</span>
				touchLeft = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if up arrow</span>
				touchUp = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if down arrow</span>
				touchDown = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>.<span style="color: #006600;">arrowCode</span> == <span style="color: #cc66cc;">39</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if right arrow</span>
				touchRight = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now that we have the variables checking which arrow is touching the receptor, now we need some key checkers. Add this to the bottom of your code in the &#8220;game&#8221; frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_DOWN</span>, checkKeys<span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">function</span> checkKeys<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if the left key is down and no left arrows are touching the receptor</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">37</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchLeft<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> 
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//make the health go down</span>
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">38</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchUp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//and so on</span>
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchDown<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == <span style="color: #cc66cc;">39</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>touchRight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		changeHealth<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Phew, that took a while. Now, for the final part of losing health, when the person misses the arrow altogether. Place this code at the end of the arrow&#8217;s <tt>eFrame</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//if game is over or it's off the stage, destroy it</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span> <span style="color: #66cc66;">||</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span> -<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if off the stage</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span> -<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">changeHealth</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, that&#8217;s all for scoring. This probably has been the longest lesson in the series. If you survived through this without having any trouble, then you can do the next part without my help, the finishing touches.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt6/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt6/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt6/source.zip">Source Files (zipped)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 5</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-5/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-5/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:05:31 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1171</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 5: Make a Level (or five) Now that we&#8217;ve programmed the basic gameplay for our game, we can make a menu system with levels. It&#8217;s going to be very simple, only one page because [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 5: Make a Level <span style="font-weight:normal;font-size:15px;">(or five)</span></h3>
<p>Now that we&#8217;ve programmed the basic gameplay for our game, we can make a menu system with levels. It&#8217;s going to be very simple, only one page because we aren&#8217;t going to make many levels. If you want to paginate it yourself, go ahead. But, this isn&#8217;t the focus of this tutorial, so that will be up to you to figure out.</p>
<p>Let&#8217;s first create the 5 levels that we&#8217;re going to need. Place this code in your first frame.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//The array for the first level of the game</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArray0:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvlArray1:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvlArray2:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvlArray3:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvlArray4:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The names of all of the levels</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlNames:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'level1'</span>,<span style="color: #ff0000;">'level2'</span>,<span style="color: #ff0000;">'level3'</span>,<span style="color: #ff0000;">'level4'</span>,<span style="color: #ff0000;">'level5'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The array holding all of the lvl arrays</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArrayAll:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span>lvlArray0,lvlArray1,lvlArray2,lvlArray3,lvlArray4<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The levels aren&#8217;t too complicated, just patterns of the four arrows repeated a few times. Now, we have to make the menu system. First of all, we have to create another layer where we will place all of the labels. Now, we can separate the frames a bit so we can actually see the labels when we make them. I&#8217;m going to label the first one &#8220;begin&#8221; and the game frame &#8220;game&#8221;.</p>
<p>Next, create a &#8220;menu&#8221; frame in between the first frame and the game frame. So far, the frames should look like this:<br />
<img src="http://mrsunstudios.com/obj/tuts/rhythm-as3/pt5/frames.gif" alt="How the frame layout should be" /></p>
<p>Ok, now for the menu. First, we&#8217;re going to have to put a <tt>stop();</tt> in the &#8220;menu&#8221; actions so we can actually use it. Then, on the top of the stage, we&#8217;re going to type in the following words: &#8220;Select a Level:&#8221;. Then, we have to create a MovieClip which is the level select button. Just draw a rectangle (mine&#8217;s 250x50px) and convert it to a MovieClip. We&#8217;re using a MovieClip so we can add actions to it. We&#8217;re going to name it <tt>btnSongSelect</tt>. We also have to export it for ActionScript. You can do this in the &#8220;Convert to Symbol&#8221; window by click on &#8220;Advanced&#8221;. Check off &#8220;Export for ActionScript&#8221; and leave everything else as it is.</p>
<p>Next, inside the symbol, add a dynamic text field that&#8217;s about the same height and width of the rectangle itself. In the area that says &#8220;&lt;Instance Name&gt;&#8221;, type in <tt>txtName</tt>. This is what your symbol should look like so far:<br />
<img src="http://mrsunstudios.com/obj/tuts/rhythm-as3/pt5/the-button.gif" alt="What your button should look like" /></p>
<p>Now, make a new layer above &#8220;Layer 1&#8243; and add the following code onto the first frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//lvlID will be passed into this MovieClip</span>
<span style="color: #808080; font-style: italic;">//when we dynamically place it onto the stage</span>
<span style="color: #808080; font-style: italic;">//It is the index number of the level and name arrays</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlID:<span style="color: #0066CC;">int</span>;
<span style="color: #808080; font-style: italic;">//lvlName is the variable that holds the name of the</span>
<span style="color: #808080; font-style: italic;">//level that this button navigates to</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlName:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">lvlNames</span><span style="color: #66cc66;">&#91;</span>lvlID<span style="color: #66cc66;">&#93;</span>;
<span style="color: #808080; font-style: italic;">//makes it so this movieclip doesn't keep on playing</span>
<span style="color: #000000; font-weight: bold;">var</span> destroy:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;<span style="color: #808080; font-style: italic;">//whether or not to destroy this</span>
<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//setting the text</span>
txtName.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">'Level '</span>+lvlID;
&nbsp;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,clickThis<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> clickThis<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if the user clicks on this button</span>
	<span style="color: #808080; font-style: italic;">//make the current level the one that this represents</span>
	<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">lvlCurrent</span> = lvlID;
	<span style="color: #808080; font-style: italic;">//go to the game frame</span>
	<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'game'</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//and remove the buttons from the stage</span>
	<span style="color: #808080; font-style: italic;">//(we'll define this function in a bit)</span>
	<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeButtons</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> eFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>destroy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if destroy is set to true</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//then remove this</span>
		<span style="color: #808080; font-style: italic;">//and remove all listeners as well</span>
		removeEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,clickThis<span style="color: #66cc66;">&#41;</span>;
		removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As always, I&#8217;ve commented this code pretty extensively. Now, we have to add actions to the main timeline that will actually place the buttons onto the stage and make a function to remove them when one is clicked:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//stop the frame</span>
<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//this loop will create the buttons</span>
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>lvlArrayAll.<span style="color: #0066CC;">length</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//set a movieclip variable to be the button</span>
	<span style="color: #000000; font-weight: bold;">var</span> newButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> btnSongSelect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	addChild<span style="color: #66cc66;">&#40;</span>newButton<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//the _x value will be 130, centered</span>
	newButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">130</span>;
	<span style="color: #808080; font-style: italic;">//the y value will be at a minimum of 70 and then continue</span>
	<span style="color: #808080; font-style: italic;">//to be added by 60 every subsequent button</span>
	newButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">70</span>+<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">*</span>i;
	<span style="color: #808080; font-style: italic;">//define the id of this button, or which level it</span>
	<span style="color: #808080; font-style: italic;">//represents</span>
	newButton.<span style="color: #006600;">lvlID</span> = i;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//this function will remove all of the buttons from the stage</span>
<span style="color: #000000; font-weight: bold;">function</span> removeButtons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//we're going to use the same loop</span>
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>numChildren;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> remove = getChildAt<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//set the target's destroy variable to true</span>
		remove.<span style="color: #006600;">destroy</span> = <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, when you run this code, it should work fine, until the game finishes and we are returned back to the menu. We have to make some changes to the code. Do you remember this at around line 80?</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//get the next arrow if it the song isn't finished</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sArrow <span style="color: #66cc66;">&lt;</span> lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	sArrow ++;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if the song is finished, then reset the game</span>
	<span style="color: #808080; font-style: italic;">//of course, we don't have the code for that yet</span>
	<span style="color: #808080; font-style: italic;">//so we're just going to go back to the first frame</span>
	<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
	gameIsOver = <span style="color: #000000; font-weight: bold;">true</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, we can change a few bits of it to make this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//get the next arrow if it the song isn't finished</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sArrow <span style="color: #66cc66;">&lt;</span> lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	sArrow ++;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if the song is finished, then reset the game</span>
	<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'menu'</span><span style="color: #66cc66;">&#41;</span>;
	gameIsOver = <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #808080; font-style: italic;">//then remove this enter_frame listener</span>
	removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, makeLvl<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, that&#8217;s all we&#8217;re going to do for this lesson. Next time, we&#8217;ll add some scoring methods and then change that part of the code even more (but just the <tt>gotoAndStop()</tt> part.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt5/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt5/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt5/source.zip">Source Files (zipped)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 4</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-4/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-4/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:04:59 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1162</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 4: Programming the Arrows: Part 2 Ok, now that we&#8217;ve gotten the arrows onto the stage, we can actually make them move up the stage and hitTest them with the receptors. We&#8217;re going to [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 4: Programming the Arrows: Part 2 </h3>
<p>Ok, now that we&#8217;ve gotten the arrows onto the stage, we can actually make them move up the stage and hitTest them with the receptors. We&#8217;re going to do all of this by making a <em>base arrow class</em>.</p>
<p>The first step to creating such a thing is to create a new Actionscript (.as) file. Immediately save it as &#8220;Arrow.as&#8221; and place the following code into it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Arrow <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Arrow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This is the basic skeleton for any AS3 class.  But, before we add any more code to it, we have to set it as the base class for all of the arrows. Right click on any of the arrow symbols in the library and click on &#8220;Properties&#8230;&#8221;. Under the &#8220;Class:&#8221; field, there should be another field called &#8220;Base Class:&#8221;. Right now, it&#8217;s set as <tt>flash.display.MovieClip</tt>. Set it now as &#8220;Arrow&#8221;. Do this for all four of the arrows. Now, we can add code to the class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Arrow <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">Object</span>;<span style="color: #808080; font-style: italic;">//we'll use this to refer to main timeline</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> arrowCode:<span style="color: #0066CC;">int</span>;<span style="color: #808080; font-style: italic;">//the key that needs to be pressed</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Arrow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			 <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
			 <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//typing _root is much easier than MovieClip(root)</span>
&nbsp;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_DOWN</span>, checkKeys<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> eFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> -= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">arrowSpeed</span>;<span style="color: #808080; font-style: italic;">//move it up the stage</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">//if game is over or it's off the stage, destroy it</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span> <span style="color: #66cc66;">||</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&lt;</span> -<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkKeys<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//checking if a certain key is down and it's touching the receptor</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span> == arrowCode <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcReceptor</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//remove it from stage</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> destroyThis<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, eFrame<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_DOWN</span>, checkKeys<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, in order for this code to work, we have to add some stuff to the <tt>makeLvl</tt> function. Just change it to this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//code here will create the level</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sTime <span style="color: #66cc66;">&lt;</span> sTempo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the required time hasn't reached the limit</span>
		<span style="color: #808080; font-style: italic;">//then update the time</span>
		sTime ++;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the time has reached the limit</span>
		<span style="color: #808080; font-style: italic;">//then reset the time</span>
		sTime = <span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">//if an actual arrow can be made</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> currentArrow:<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">//this will hold the current arrow</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place a left arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #808080; font-style: italic;">//set the _x value of the arrow so that it is in the</span>
				<span style="color: #808080; font-style: italic;">//right place to touch the receptor</span>
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">135</span>;
				<span style="color: #808080; font-style: italic;">//set the arrow's y coordinate off of the stage</span>
				<span style="color: #808080; font-style: italic;">//so that the user can't see it when it appears</span>
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				<span style="color: #808080; font-style: italic;">//setting the key that needs to be pressed</span>
				currentArrow.<span style="color: #006600;">arrowCode</span> = <span style="color: #cc66cc;">37</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add it to stage</span>
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place an up arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">205</span>;
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				currentArrow.<span style="color: #006600;">arrowCode</span> = <span style="color: #cc66cc;">38</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place a down arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">275</span>;
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				currentArrow.<span style="color: #006600;">arrowCode</span> = <span style="color: #cc66cc;">40</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place a right arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">345</span>;
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				currentArrow.<span style="color: #006600;">arrowCode</span> = <span style="color: #cc66cc;">39</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//get the next arrow if it the song isn't finished</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sArrow <span style="color: #66cc66;">&lt;</span> lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			sArrow ++;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//if the song is finished, then reset the game</span>
			<span style="color: #808080; font-style: italic;">//of course, we don't have the code for that yet</span>
			<span style="color: #808080; font-style: italic;">//so we're just going to go back to the first frame</span>
			<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			gameIsOver = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, if you test the game now, there&#8217;s going to be a problem. The game will reset before all of the arrows even reach the receptor. There are two ways that I can think of to fix this. The first option is to count all of the <tt>arrow</tt> instances on stage and when it&#8217;s 0, then end the game. The second, which I prefer, is to just add a certain amount of &#8220;0&#8243;&#8216;s to the end of the array each time the game begins. The second option is what I&#8217;m going to do. In the <tt>beginCode()</tt> function, add the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//make the level array longer</span>
lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, the game should be working out pretty well. I&#8217;m pretty proud of our progress this lesson, so I&#8217;m going to end it now. We&#8217;ve finished the basic gameplay of the game. Next lesson, we will make a menu system where the player can choose which games to play. It&#8217;ll be great.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt4/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt4/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt4/source.zip">Source Files (zipped)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-3/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:03:06 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1159</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Programming the Arrows Well, we finally can program some stuff. In this part, we will create the arrows dynamically and make them move up screen. Let us begin. We are going to make an array [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Programming the Arrows</h3>
<p>Well, we finally can program some stuff. In this part, we will create the arrows dynamically and make them move up screen. Let us begin.</p>
<p>We are going to make an array for our levels, just as we did in the <a href="http://www.mrsunstudios.com/2008/07/tutorial-create-a-brick-breaker-game-in-as3/">Brick Breaker Tutorial</a>. So, we&#8217;re going to create a frame in the beginning of the timeline and we&#8217;re going to add the following code to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//The current level the player is on</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlCurrent:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #808080; font-style: italic;">//The array for the first level of the game</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArray0:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The array holding all of the lvl arrays</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArrayAll:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span>lvlArray0<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The second frame will be where the game will take place. For now, just put a <tt>stop();</tt> there, so the game doesn&#8217;t continuously loop. So now, we&#8217;re going to create two functions, one called <tt>beginCode()</tt> and another called <tt>makeLvl()</tt>. <tt>beginCode()</tt> will only be run once every level, while makeLvl will run the entire level. Here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, makeLvl<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Right now, this code won&#8217;t do anything. Now we have to make some variables that will aid in our level creation. Place this code at the top:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//VARIABLES</span>
<span style="color: #808080; font-style: italic;">//sTime is the current frame that is being played</span>
<span style="color: #808080; font-style: italic;">//Once it reaches sTempo, then it will be reset</span>
<span style="color: #808080; font-style: italic;">//and a note will be created</span>
<span style="color: #000000; font-weight: bold;">var</span> sTime:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #808080; font-style: italic;">//sTempo is how many frames it takes before</span>
<span style="color: #808080; font-style: italic;">//a note is created. Because it's 12, and</span>
<span style="color: #808080; font-style: italic;">//the frame rate is 24, it will take a half of a second</span>
<span style="color: #808080; font-style: italic;">//for a note to be made</span>
<span style="color: #000000; font-weight: bold;">var</span> sTempo:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">12</span>;
<span style="color: #808080; font-style: italic;">//sNote is the current arrow of the level that is created</span>
<span style="color: #808080; font-style: italic;">//0 makes no arrow</span>
<span style="color: #808080; font-style: italic;">//1 makes a left arrow</span>
<span style="color: #808080; font-style: italic;">//2 makes an up arrow</span>
<span style="color: #808080; font-style: italic;">//3 makes a down arrow</span>
<span style="color: #808080; font-style: italic;">//4 makes a right arrow</span>
<span style="color: #000000; font-weight: bold;">var</span> sArrow:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #808080; font-style: italic;">//arrowSpeed is how fast the arrow moves up the screen</span>
<span style="color: #000000; font-weight: bold;">var</span> arrowSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">10</span>;
<span style="color: #808080; font-style: italic;">//gameIsOver is whether the game's over</span>
<span style="color: #000000; font-weight: bold;">var</span> gameIsOver:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;</pre></div></div>

<p>I <em>really</em> commented out this one so you&#8217;d understand what the variables do. If you still don&#8217;t, then maybe the code within the <tt>makeLvl()</tt> function might help explain:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//code here will create the level</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sTime <span style="color: #66cc66;">&lt;</span> sTempo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the required time hasn't reached the limit</span>
		<span style="color: #808080; font-style: italic;">//then update the time</span>
		sTime ++;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the time has reached the limit</span>
		<span style="color: #808080; font-style: italic;">//then reset the time</span>
		sTime = <span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">//if an actual arrow can be made</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> currentArrow:<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">//this will hold the current arrow</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place a left arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #808080; font-style: italic;">//set the _x value of the arrow so that it is in the</span>
				<span style="color: #808080; font-style: italic;">//right place to touch the receptor</span>
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">135</span>;
				<span style="color: #808080; font-style: italic;">//set the arrow's y coordinate off of the stage</span>
				<span style="color: #808080; font-style: italic;">//so that the user can't see it when it appears</span>
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add it to stage</span>
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place an up arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">205</span>;
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place a down arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">275</span>;
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>sArrow<span style="color: #66cc66;">&#93;</span> == <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//place a right arrow onto the stage</span>
				currentArrow = <span style="color: #000000; font-weight: bold;">new</span> arrowRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				currentArrow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">345</span>;
				currentArrow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">500</span>;
				addChild<span style="color: #66cc66;">&#40;</span>currentArrow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//get the next arrow if it the song isn't finished</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sArrow <span style="color: #66cc66;">&lt;</span> lvlArrayAll<span style="color: #66cc66;">&#91;</span>lvlCurrent<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			sArrow ++;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//if the song is finished, then reset the game</span>
			<span style="color: #808080; font-style: italic;">//of course, we don't have the code for that yet</span>
			<span style="color: #808080; font-style: italic;">//so we're just going to go back to the first frame</span>
			<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			gameIsOver = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Again, I did a lot of commenting to help you out. Right now, if you test the movie, nothing will really happen except that the game will black out for a millisecond. But, if you change the code that I gave you a bit, I&#8217;m sure you make it so you see the results. (Hint, change the <tt>y</tt> value). Well, that&#8217;s all that I&#8217;m going to teach this lesson. Next lesson, we&#8217;ll continue coding the arrows, making the move and hit testing them with the receptors.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt3/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt3/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt3/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3 &#8211; Part 2</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-2/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-2/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:02:00 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1154</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 2: Create the Require Symbols/Art Well, we aren&#8217;t going into the programming yet until the next step, so you have got to be patient. Anyway, in this step, we&#8217;re going to draw the symbols [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 2: Create the Require Symbols/Art</h3>
<p>Well, we aren&#8217;t going into the programming yet until the next step, so you have got to be patient. Anyway, in this step, we&#8217;re going to draw the symbols and art that are needed for the game creation. Let&#8217;s get the list that we made the last step:</p>
<h4>List of MovieClips</h4>
<ul>
<li>Left Arrow</li>
<li>Up Arrow</li>
<li>Right Arrow</li>
<li>Down Arrow</li>
<li>Receptors</li>
<li>Health Bar</li>
<li>Combo Counter</li>
<li>Score</li>
<li>Accuracy Meter</li>
</ul>
<p>Let&#8217;s start with making the arrows. They seem to be the easiest. First we&#8217;re going to make the background black, just because I like black backgrounds. We&#8217;re going to make most of the art white, just to keep it simple. Here are the arrows that I drew. They are 60&#215;60 pixels.</p>
<p><img src="http://mrsunstudios.com/obj/tuts/rhythm-as3/pt2/arrows.png" alt="My sexy arrows" /></p>
<p>When you turn your shapes into arrows, make sure that you check &#8220;Export for ActionScript&#8221; and to name them arrow<em>Side</em> (ex: arrowLeft or arrowRight).<br />
<img src="http://mrsunstudios.com/obj/tuts/rhythm-as3/pt2/convert-to-symbol.png" alt="Converting to a symbol" /></p>
<p>Now we&#8217;re going to create the receptor. You have to be careful in spacing each of the arrows a certain amount of pixels apart. Mine are 10 pixels apart.<br />
<img src="http://mrsunstudios.com/obj/tuts/rhythm-as3/pt2/receptor.png" alt="My receptors" /><br />
If you want to make yours more decorative, nobody&#8217;s stopping you.</p>
<p>Now, we have to make all of the statistical symbols. Actually, we don&#8217;t really need a symbol for the combo counter, health bar, or the accuracy meter. They can all be dynamic text fields. But, we definitely will need to make the health bar a symbol.</p>
<p>Our health bar symbol will be very simple, just one layer of a green rectangle. It&#8217;s going to be 550&#215;15 pixels.<br />
<img src="http://mrsunstudios.com/obj/tuts/rhythm-as3/pt2/healthbar.png" alt="My simple healthbar" /><br />
You don&#8217;t need to add any layers of frames to this, we will change its width later with ActionScript.</p>
<p>Well, those are all of the symbols that we need. Now, the next simple step is to get a basic placement of them on stage.</p>
<h4>Final Product</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt2/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt2/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/rhythm-as3/pt2/source.fla">Source .fla File</a></p>
<p>Now that we&#8217;re finally done with the brainstorming and art creating, we can finally continue on to program the game. That is what we&#8217;re going to do in the next part.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Rhythm Game in AS3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 12:01:16 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1150</guid>
		<description><![CDATA[The Brainstorming Create the Required Symbols/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Ok, this may not seem to be the most exciting step, but it is pretty important. Because this game is going to take a while to make, brainstorming is key. So, we have to figure [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li class="c_chap"><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3/">The Brainstorming</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-2/">Create the Required Symbols/Art</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-3/">Programming the Arrows</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-4/">Programming the Arrows Part 2</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-5/">Make a Level</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-6/">Scoring</a></li>
<li><a href="http://mrsunstudios.com/2009/01/tutorial-make-a-rhythm-game-in-as3-part-7/">Finishing Touches</a></li>
</ol>
</div>
<p>Ok, this may not seem to be the most exciting step, but it is pretty important. Because this game is going to take a while to make, brainstorming is key.</p>
<p>So, we have to figure out what movieclips we are going to have to make. What are the key symbols that we need for gameplay? If anything, it&#8217;s going to be the arrows.</p>
<h4>So far on the MovieClip list:</h4>
<ul>
<li>Left Arrow</li>
<li>Up Arrow</li>
<li>Right Arrow</li>
<li>Down Arrow</li>
</ul>
<p>Of course, the arrows are going to have to have a receptor that it hits when you have to press the certain key.</p>
<h4>So far on the MovieClip List:</h4>
<ul>
<li>Left Arrow</li>
<li>Up Arrow</li>
<li>Right Arrow</li>
<li>Down Arrow</li>
<li>Receptors</li>
</ul>
<p>There are four final MovieClips that we are going to have to create, and all of them have to do with scoring. One of them is the health bar, the second is the combo counter, the third is the players score, and the fourth is the word that tells the user how accurately they hit the key (Perfect, Awesome, Good, OK, Bad). </p>
<h4>The Final MovieClip List:</h4>
<ul>
<li>Left Arrow</li>
<li>Up Arrow</li>
<li>Right Arrow</li>
<li>Down Arrow</li>
<li>Receptors</li>
<li>Health Bar</li>
<li>Combo Counter</li>
<li>Score</li>
<li>Accuracy Meter</li>
</ul>
<p>Well, that&#8217;s all we&#8217;re going to do for brainstorming. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-rhythm-game-in-as3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-6/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-6/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 12:06:47 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[shooter]]></category>
		<category><![CDATA[teaching]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vertical]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1148</guid>
		<description><![CDATA[Table of Contents Programming the Character Programming the Character &#8211; Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 6: Finishing Touches As always, the finishing touches of this game won&#8217;t be explained too much by me, in hopes that you actually can do some of this stuff by yourself. Of course, [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2">Programming the Character</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-2">Programming the Character &#8211; Part 2</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-3">Creating the Enemies</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-4">Programming the Enemies</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-5">Scoring</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 6: Finishing Touches</h3>
<p>As always, the finishing touches of this game won&#8217;t be explained too much by me, in hopes that you actually can do some of this stuff by yourself. Of course, there will always be source files at the bottom if you need to clear anything up.</p>
<p>The first thing I want to do is show the score on the &#8220;lose&#8221; screen. That&#8217;ll be pretty easy, just add a dynamic text field there.</p>
<p>Next, we can make some particles move down the screen so it looks like the player is actually moving forward. We&#8217;re going to make them the same speed as the enemies. I&#8217;m actually show some code for this one, because it&#8217;s also a pretty new thing for me. We&#8217;re not going to make a MovieClip for this, we&#8217;re going to make dynamic shapes through ActionScript. First, we have to add a <tt>totalBgShapes</tt> variable to the top of the code. Next, create an empty MovieClip called <tt>bgHolder</tt> Then, here&#8217;s the code to place at the bottom of the <tt>onEnterFrame</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//creating background particles</span>
bgHolder.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes, bgHolder.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x333333<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//this just determines the shape's color</span>
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">550</span><span style="color: #66cc66;">&#41;</span>;
gHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = -<span style="color: #cc66cc;">50</span> - bgHolder.<span style="color: #0066CC;">_y</span>;
<span style="color: #808080; font-style: italic;">//creating 4 random points to make a random shape</span>
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//finishes up the shape</span>
&nbsp;
bgHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;bg&quot;</span>+totalBgShapes<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//giving it some actions</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">450</span> - bgHolder.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">||</span> <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if it goes off the stage or game is over</span>
		<span style="color: #808080; font-style: italic;">//then destroy it</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
totalBgShapes ++;
bgHolder.<span style="color: #0066CC;">_y</span> += <span style="color: #cc66cc;">2</span>;</pre></div></div>

<p>Pretty intense, eh? Well, this is a finishing touch, and most finishing touches <em>are</em> pretty intense. Now, we have to make it so <tt>mcMain</tt> is on top of everything with this code at the end:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcMain.<span style="color: #0066CC;">swapDepths</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Also, remove <tt>mcMain</tt> when <tt>gameOver</tt> is true or else strange things will happen.</p>
<p>There is one final thing that I want to do before actually wrapping up this tutorial.  For some reason, there is a bug where a bullet disappears for some strange reason.  I think I know exactly how to fix it.  When we added the bullet to the stage, we made its depth the <tt>_root.getNextHighestDepth</tt>. I&#8217;m sorry, it should actually be <tt>bulletHolder.getNextHighestDepth</tt>.</p>
<p>Well, that&#8217;s actually all that I really have to finish off the game. I hope you had fun making it!</p>
<h4>The Final Product:</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="300" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt6/source.swf" /><embed type="application/x-shockwave-flash" width="300" height="400" src="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt6/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt6/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-6/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 5</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-5/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-5/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 12:05:02 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[shooter]]></category>
		<category><![CDATA[teaching]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vertical]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1140</guid>
		<description><![CDATA[Table of Contents Programming the Character Programming the Character &#8211; Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 5: Scoring Now that we&#8217;ve got the hardest part down, it all gets easier from here. This chapter will be simple, just some code that has scoring. Also, as promised, we&#8217;re going to [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2">Programming the Character</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-2">Programming the Character &#8211; Part 2</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-3">Creating the Enemies</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-4">Programming the Enemies</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-5">Scoring</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 5: Scoring</h3>
<p>Now that we&#8217;ve got the hardest part down, it all gets easier from here. This chapter will be simple, just some code that has scoring. Also, as promised, we&#8217;re going to have a function run when the player is hit by an enemy. Let&#8217;s start with this one first, eh?</p>
<p>In order to do this, we have to make a frame called &#8220;lose&#8221;. This will be the frame that we&#8217;ll navigate to when we get hit. It&#8217;ll be simple, just some text that says &#8220;You Lose&#8221; and a listener for keystrokes  that will return us to play another game. First, make the frame called &#8220;lose&#8221;. I recommend making an entire new layer for labels, but this isn&#8217;t required. In that frame, draw or type in whatever you want to signify that the player has lost the game. I&#8217;m just going to put the text, &#8220;YOU LOSE&#8221; and, in smaller text, &#8220;Press Space to Restart&#8221;.</p>
<p>Then, in the actions, type in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//stops the game so nothing goes wrong</span>
&nbsp;
<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">//create a function to check for keystrokes</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Key</span>.<span style="color: #0066CC;">isDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the space key is down...</span>
		<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//then go back to the first frame, restarting the game</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, I&#8217;ve noticed a bit of a problem in the main code.  For some reason, the game thinks that you get hit whenever you shoot an enemy.  I&#8217;ve found a way around this strange behavior, however. First of all, add this variable to the top of the code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//showing the amount of enemies that have been added to the stage</span>
<span style="color: #000000; font-weight: bold;">var</span> enemyTotal:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Next, find the code where we have to create the enemy and add it to the stage.  Change it to this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mcEnemy'</span>, <span style="color: #ff0000;">'en'</span>+enemyTotal,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//then add the enemy</span>
<span style="color: #808080; font-style: italic;">//setting it's coordinates</span>
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'en'</span>+enemyTotal<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//randomly within the boundaries</span>
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'en'</span>+enemyTotal<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = -<span style="color: #cc66cc;">50</span>; <span style="color: #808080; font-style: italic;">//sets this offstage at first</span>
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'en'</span>+enemyTotal<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//then give it some functions</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> += <span style="color: #cc66cc;">5</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//run a loop checking if it's touching any bullets</span>
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> cBullet:<span style="color: #0066CC;">String</span> <span style="color: #b1b100;">in</span> <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if it's touching the bullet</span>
		<span style="color: #808080; font-style: italic;">//we have to use coordinates because hit testing doesn't seem to work</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>._y-<span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> -<span style="color: #cc66cc;">35</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//then destroy this guy</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #808080; font-style: italic;">//and destroy the bullet</span>
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//hit testing with the user</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">hitTest</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcMain</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//we'll add code here in the next part</span>
		<span style="color: #808080; font-style: italic;">//but for now, we'll just trace something</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'You just got hit!'</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
enemyTime = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//reset the time</span>
enemyTotal ++; <span style="color: #808080; font-style: italic;">//add 1 more to the amount of enemies total</span></pre></div></div>

<p>I&#8217;m sorry about this delay, but we need this to continue on with our game.</p>
<p>Now, we have to create a new variable at the top that will say whether or not the game has been ended:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//add a stop function too!</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> gameOver:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;</pre></div></div>

<p>Then, add this code to both the <tt>mcBullet</tt>&#8216;s and <tt>mcEnemy</tt>&#8216;s <tt>onEnterFrame</tt> functions:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//checking if the game is over</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gameOver<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//destroy this guy if the game is over</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, we can make this Boolean false when the user gets touched by the enemy. Find where we hit test for <tt>mcMain</tt> and replace that trace statement with:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//hit testing with the user</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">hitTest</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcMain</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//set the game to be over and go to lose screen</span>
	gameOver = <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'lose'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Phew, that was a lot of work, wasn&#8217;t it? Now we can move onto actually scoring the game. First, we have to define a score variable at the top:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//the player's score</span>
<span style="color: #000000; font-weight: bold;">var</span> score:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Then, increment the score every time an enemy is killed. Place this code in the Enemy&#8217;s hit testing for the bullet.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//up the score</span>
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">score</span> += <span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>Now, we can show the score to the user with a dynamic text field. Make one at the bottom of the stage and give it an instance name of <tt>txtScore</tt>. Next, place this code into the <tt>onEnterFrame</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//updating the score text field</span>
txtScore.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">'Score:  '</span>+score;</pre></div></div>

<p>Pretty easy, right? Well, that&#8217;s all we&#8217;re going to do for scoring. Next, we&#8217;ll add some sweet finishing touches, eh?</p>
<h4>The Final Product:</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="300" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt5/source.swf" /><embed type="application/x-shockwave-flash" width="300" height="400" src="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt5/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt5/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 4</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-4/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-4/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 12:04:34 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Intermediate Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[shooter]]></category>
		<category><![CDATA[teaching]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vertical]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1132</guid>
		<description><![CDATA[Table of Contents Programming the Character Programming the Character &#8211; Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 4: Programming the Enemies Now we have to make it so the enemies can be shot. This probably will be the most complicated of the steps. But, it also is what makes this [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2">Programming the Character</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-2">Programming the Character &#8211; Part 2</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-3">Creating the Enemies</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-4">Programming the Enemies</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-5">Scoring</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-make-a-vertical-shooter-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 4: Programming the Enemies</h3>
<p>Now we have to make it so the enemies can be shot. This probably will be the most complicated of the steps. But, it also is what makes this thing a game. There are two ways that I know of to accomplish this task. The first one is to have the bullet listen for hit testing the enemy, or the enemy listen for the bullet. It&#8217;s basically the same thing. However, I&#8217;m going to have the enemy listen for the bullet, just because I feel like it.</p>
<p>The first thing we need to do in order to accomplish this feat is to create a container for all of the bullets. This way, we can access all of the bullets through the container. I can&#8217;t really explain this too well, so I&#8217;ll just demonstrate it for you. At the top of the code, add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//this movieclip will hold all of the bullets</span>
<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">createEmptyMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'bulletHolder'</span>, <span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The <tt>bulletHolder</tt> is invisible and is just a container for all of the bullets. In order for this to be any use to us, we need to actually add the bullets to this container. This will be easy. Find where we add the bullet to the stage:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mcBullet'</span>, <span style="color: #ff0000;">'Bullet'</span>+bulletID,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//setting the coordinates of the bullet to be the same as the main character</span>
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = mcMain.<span style="color: #0066CC;">_x</span> + mcMain.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span> - <span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;
<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = mcMain.<span style="color: #0066CC;">_y</span>;
	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//giving the bullet some actions</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> -= <span style="color: #cc66cc;">10</span>; <span style="color: #808080; font-style: italic;">//moving the bullet</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&lt;</span> -<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_height</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the bullet goes off stage</span>
		<span style="color: #808080; font-style: italic;">//then destroy it</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Change it to this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">bulletHolder.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mcBullet'</span>, <span style="color: #ff0000;">'Bullet'</span>+bulletID,<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//setting the coordinates of the bullet to be the same as the main character</span>
bulletHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = mcMain.<span style="color: #0066CC;">_x</span> + mcMain.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span> - bulletHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;
bulletHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = mcMain.<span style="color: #0066CC;">_y</span>;
bulletHolder<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'Bullet'</span>+bulletID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//giving the bullet some actions</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> -= <span style="color: #cc66cc;">10</span>; <span style="color: #808080; font-style: italic;">//moving the bullet</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&lt;</span> -<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_height</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if the bullet goes off stage</span>
		<span style="color: #808080; font-style: italic;">//then destroy it</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now we enter complicated territory. Find the <tt>onEnterFrame</tt> function we added to the Enemy. Add the following to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//run a loop checking if it's touching any bullets</span>
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> cBullet:<span style="color: #0066CC;">String</span> <span style="color: #b1b100;">in</span> <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//if it's touching the bullet</span>
	<span style="color: #808080; font-style: italic;">//we have to use coordinates because hit testing doesn't seem to work</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>._y-<span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> -<span style="color: #cc66cc;">35</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//then destroy this guy</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'en'</span>+enID<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//and destroy the bullet</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">bulletHolder</span><span style="color: #66cc66;">&#91;</span>cBullet<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, try out your game. It should work pretty well now. The only problem with it, however, is that it doesn&#8217;t do anything when it touches the player. We aren&#8217;t going to really do anything with this right now, but we will set it up so we can make a function for it in the next part of the tutorial, &#8220;Scoring&#8221;. Just place the following code in the <tt>Enemy</tt>&#8216;s <tt>onEnterFrame()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">			<span style="color: #808080; font-style: italic;">//hit testing with the user</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcMain</span>._y-<span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcMain</span>.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcMain</span>.<span style="color: #0066CC;">_x</span>+<span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mcMain</span>.<span style="color: #0066CC;">_x</span> -<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					<span style="color: #808080; font-style: italic;">//we'll add code here in the next part</span>
					<span style="color: #808080; font-style: italic;">//but for now, we'll just trace something</span>
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'You got hit!'</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, that&#8217;s all for today. Join us next time when we score the game!</p>
<h4>The Final Product:</h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="300" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt4/source.swf" /><embed type="application/x-shockwave-flash" width="300" height="400" src="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt4/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/vert-shooter-as2/pt4/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-make-a-vertical-shooter-in-as2-part-4/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

