<?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; Beginner Tutorials</title>
	<atom:link href="http://www.mrsunstudios.com/category/tutorials/beginner-tutorials/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: Create a Brick Breaker Game in AS2 &#8211; Part 6</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-6/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-6/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 12:05:47 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1073</guid>
		<description><![CDATA[Table of Contents Coding Paddle Movement Programming the Ball Setting Up the Bricks on Stage Hit Testing the Bricks Creating Levels Finishing Touches Step 6: Finishing Touches Well, we&#8217;re almost done with our game, now we just have to add some finishing touches. I won&#8217;t make a menu system like you usually should in a [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2">Coding Paddle Movement</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-2">Programming the Ball</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-3">Setting Up the Bricks on Stage</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-4">Hit Testing the Bricks</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-5">Creating Levels</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 6: Finishing Touches</h3>
<p>Well, we&#8217;re almost done with our game, now we just have to add some finishing touches. I won&#8217;t make a menu system like you usually should in a game before releasing it. But, this is just a tutorial, and hopefully you&#8217;ve learned something. You probably already know how to make a menu anyway. Also, I&#8217;m not going to teach you how to do some of the things that we need because hopefully you can do it yourself. If you can&#8217;t , there&#8217;s always the source file at the bottom.</p>
<p>Now, where were we? Oh yes. I realized that the last lesson, we were supposed to make more levels. I taught a lot of stuff, but I forgot to make more levels for you! I apologize. Anyway, here&#8217;s an example of a 5 level game (Put it on 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;">//The array code for lvl 1</span>
<span style="color: #808080; font-style: italic;">//All of the later levels add one more row of bricks</span>
<span style="color: #000000; font-weight: bold;">var</span> lvl1Code:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl2Code:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl3Code:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl4Code:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> lvl5Code:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The array that contains all of the level codes</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArray:<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>lvl1Code, lvl2Code, lvl3Code, lvl4Code, lvl5Code<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It&#8217;ll be a pretty straightforward game. Next, I think the gamer needs to know that he has to click the screen to start, and not think the the game is frozen or something. So, just add a dynamic textfield to the middle of the stage. Give it an instance game of <tt>txtStart</tt>, and make sure it isn&#8217;t selectable.<br />
<img src="http://mrsunstudios.com/obj/tuts/brick-breaker-as3/pt6/text-properties.gif" alt="Make sure that these properties are what you have" /></p>
<p>Next, add this code at the end of the frame.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//setting the text's word</span>
txtStart.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Click To Begin&quot;</span>;</pre></div></div>

<p>You can make the text different, but don&#8217;t be too mean. Next, we have to remove the text, which we can do in the mcBg&#8217;s <tt>onRelease()</tt> function. We aren&#8217;t going to remove the text field itself because we need it again for every level.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//removing the &quot;Click to Start&quot; Text</span>
txtStart.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">''</span>;</pre></div></div>

<p>Now, we have to reset it every level. I&#8217;m not going to tell you how to do it. I hope you&#8217;ve learned enough to be able to.</p>
<p>Now, we can make the scoring happen. 10 points should be awarded to the player for destroying one brick. I&#8217;ll leave it to you to find out how to do this.</p>
<p>The next thing we have to do is display the current level, the score, and how many lives the user has. This will be pretty simple as well, so I won&#8217;t really go into details about how to do it. Just create a dynamic text field and update it in the <tt>onEnterFrame()</tt> function. Pretty easy, eh?</p>
<h4>The 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/brick-breaker-as2/pt6/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/brick-breaker-as2/pt6/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt6/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-6/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Brick Breaker Game in AS2 &#8211; Part 5</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-5/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-5/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 12:04:46 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1071</guid>
		<description><![CDATA[Table of Contents Coding Paddle Movement Programming the Ball Setting Up the Bricks on Stage Hit Testing the Bricks Creating Levels Finishing Touches Step 5: Creating Levels Now that we&#8217;ve got the basic gameplay down, we can create some levels. Because we&#8217;re making only a simple game, we aren&#8217;t going to make that many. But [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2">Coding Paddle Movement</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-2">Programming the Ball</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-3">Setting Up the Bricks on Stage</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-4">Hit Testing the Bricks</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-5">Creating Levels</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 5: Creating Levels</h3>
<p>Now that we&#8217;ve got the basic gameplay down, we can create some levels. Because we&#8217;re making only a simple game, we aren&#8217;t going to make that many. But before we even start making multiple levels, we have to make it possible to win or lose a level. This will be pretty easy.</p>
<p>We&#8217;re first going make it possible to beat the level. In order for this to happen, we have to track how many bricks are on the stage. Just define the following variable at the top of the code.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> brickAmt:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Now, we have to increment this number every time a brick is placed onto the stage. Type in this code into the <tt>makeLvl()</tt> function where we actually create the brick:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//incrementing how many bricks are on the stage</span>
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">brickAmt</span> ++;</pre></div></div>

<p>Next, we have to decrement the number every time a brick is destroyed. Just type in this code in the <tt>hitTest</tt> if statement:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//decrementing how many bricks are on the stage</span>
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">brickAmt</span> --;</pre></div></div>

<p>That was pretty easy, right? Now, we have to add some code that will check if the value of bricks is 0.<br />
You can do so in 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;">//checking if the bricks are all gone</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>brickAmt == <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;">//reset the level by increasing the level</span>
	currentLvl ++;
	<span style="color: #808080; font-style: italic;">//and re-running makeLvl</span>
	makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>When this code runs, nothing will happen when you break all of the bricks because we haven&#8217;t defined more levels. But, there is one thing I want to fix before doing that. The game starts automatically, even if the player isn&#8217;t ready. So, we want to start the level only when the user first clicks on the screen. This is actually easier than you might think. The first thing we need to do is create a black rectangle that covers the entire stage. Then, we have to turn it into a MovieClip. Name it mcBg, Export it for ActionScript, and give it an instance name of mcBg. Then, add the following to the bottom of the code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcBg.<span style="color: #0066CC;">onRelease</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;">//creating a function that will run when bg is clicked</span>
	<span style="color: #808080; font-style: italic;">//move the bg out of range so it isn't bothersome</span>
	mcBg.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">800</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, we have to add the <strong>entire</strong> <tt>onEnterFrame()</tt> function to this, so it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcBg.<span style="color: #0066CC;">onRelease</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;">//creating a function that will run when bg is clicked</span>
	<span style="color: #808080; font-style: italic;">//move the bg out of range so it isn't bothersome</span>
	mcBg.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">800</span>;
	<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;">//this function will run during every single frame</span>
		<span style="color: #808080; font-style: italic;">//The paddle follows the mouse</span>
		mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_xmouse</span> - mcPaddle.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">*</span>.5;
&nbsp;
		<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the left</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&lt;</span> mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
			mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the right</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
			mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//MOVING THE BALL</span>
		mcBall.<span style="color: #0066CC;">_x</span> += ballXSpeed;
		mcBall.<span style="color: #0066CC;">_y</span> += ballYSpeed;
		<span style="color: #808080; font-style: italic;">//Bouncing the ball off of the walls</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">Stage</span>.<span style="color: #006600;">width</span>-mcBall.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//if the ball hits the right side</span>
			<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
			ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_x</span> <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;">//if the ball hits the left side</span>
			<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
			ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">Stage</span>.<span style="color: #006600;">height</span>-mcBall.<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 ball hits the bottom</span>
			<span style="color: #808080; font-style: italic;">//then bounce up</span>
			ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_y</span> <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;">//if the ball hits the top</span>
			<span style="color: #808080; font-style: italic;">//then bounce down</span>
			ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">hitTest</span><span style="color: #66cc66;">&#40;</span>mcPaddle<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;">//calculate the ball angle if ball hits paddle</span>
			calcBallAngle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//checking if the bricks are all gone</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>brickAmt == <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;">//reset the level by increasing the level</span>
			currentLvl ++;
			<span style="color: #808080; font-style: italic;">//and re-running makeLvl</span>
			makeLvl<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>This is pretty solid code, but it does have some issues. One problem we have to fix is that the level is immediately made after you break all of the bricks, without resetting the ball&#8217;s position or anything (it may not be like that on yours, but trust me, I&#8217;ve tested it). Just paste in this code where we level up in 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;">//checking if the bricks are all gone</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>brickAmt == <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;">//reset the level by increasing the level</span>
	currentLvl ++;
	<span style="color: #808080; font-style: italic;">//and re-running makeLvl</span>
	makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//reset the ball's position</span>
	mcBall.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">175</span>;
	mcBall.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">250</span>;
	<span style="color: #808080; font-style: italic;">//then move the background back</span>
	mcBg.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #808080; font-style: italic;">//and remove all of these events</span>
	<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">onEnterFrame</span> = <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now that we can beat a level, we now have to lose a level. We&#8217;re going to have to add a lives variable at the top first. We&#8217;re also going add a variable that defines if the game is over.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//how many lives you got</span>
<span style="color: #000000; font-weight: bold;">var</span> lives:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">3</span>;
<span style="color: #808080; font-style: italic;">//if the game is over</span>
<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, we have to subtract a life every time the ball hits the floor and do other stuff when the lives are all gone.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">Stage</span>.<span style="color: #006600;">height</span>-mcBall.<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 ball hits the bottom</span>
	<span style="color: #808080; font-style: italic;">//then bounce up and lose a life</span>
	ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	lives --;
	<span style="color: #808080; font-style: italic;">//if there aren't any lives left</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lives <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;">//the game is over now</span>
		gameOver = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #808080; font-style: italic;">//go to a lose frame</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>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, now we have to create a frame called &#8220;lose&#8221;. I&#8217;m just going to make a frame that that has the text, &#8220;YOU LOSE&#8221;. Make sure to give your frame a label of &#8220;lose&#8221;, or the code won&#8217;t work.</p>
<p>Also, we have to remove the bricks from the stage, because they were added dynamically and won&#8217;t go away if you just change a frame. So, type the following code into the brick&#8217;s <tt>onEnterFrame</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #808080; font-style: italic;">//if game's 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: #808080; font-style: italic;">//then remove this from stage</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, we have to make the player be able to restart the game after losing. This will be easy. Just add the mcBg back to the stage that will reset the game if the stage is clicked. This code should be in the &#8220;lose&#8221; frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcBg.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #808080; font-style: italic;">//putting the mcBg back</span>
mcBg.<span style="color: #0066CC;">onRelease</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: #0066CC;">gotoAndPlay</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;">//restart the game</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Well, that&#8217;s all for this lesson. The next one will be the final one, where we add some finishing touches, and fix some bugs.</p>
<h4>The 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/brick-breaker-as2/pt5/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/brick-breaker-as2/pt5/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt5/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Brick Breaker Game in AS2 &#8211; Part 4</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-4/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-4/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 12:03:45 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1069</guid>
		<description><![CDATA[Table of Contents Coding Paddle Movement Programming the Ball Setting Up the Bricks on Stage Hit Testing the Bricks Creating Levels Finishing Touches Step 4: Hit Testing the Bricks Now that we&#8217;ve actually made the bricks, we can now break them with our ball&#8230; Anyway, this will be pretty easy to accomplish. All you have [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2">Coding Paddle Movement</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-2">Programming the Ball</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-3">Setting Up the Bricks on Stage</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-4">Hit Testing the Bricks</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-5">Creating Levels</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 4: Hit Testing the Bricks</h3>
<p>Now that we&#8217;ve actually made the bricks, we can now break them with our ball&#8230; Anyway, this will be pretty easy to accomplish. All you have to do is change this code in the <tt>makeLvl()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<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;">//creating a variable which holds the brick instance</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mcBrick'</span>, <span style="color: #ff0000;">'brick'</span>+i,<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 brick's coordinates via the i variable and brickRow</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">15</span>+<span style="color: #66cc66;">&#40;</span>i-brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">75</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">10</span>+brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">20</span>;
			<span style="color: #808080; font-style: italic;">//checks if the current brick needs a new row</span>
			<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span>;c<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">10</span>;c++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i == c<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					brickRow ++;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>to this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<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;">//creating a variable which holds the brick instance</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mcBrick'</span>, <span style="color: #ff0000;">'brick'</span>+i,<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 brick's coordinates via the i variable and brickRow</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">15</span>+<span style="color: #66cc66;">&#40;</span>i-brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">75</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">10</span>+brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">20</span>;
			<span style="color: #808080; font-style: italic;">//giving this brick some actions</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<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: #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;">mcBall</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 this touches the ball</span>
					<span style="color: #808080; font-style: italic;">//then destroy this mofugger!</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 make the ball bounce away</span>
					ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">//checks if the current brick needs a new row</span>
			<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span>;c<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">10</span>;c++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i == c<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					brickRow ++;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>That was pretty easy, wasn&#8217;t it? Definitely much easier than what had to be done in AS3. Well, since we have time, I&#8217;m going to post here what your code should look like at this time:</p>
<h4>Frame 1:</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Current level player is on</span>
<span style="color: #000000; font-weight: bold;">var</span> currentLvl:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span>;
<span style="color: #808080; font-style: italic;">//The array code for lvl 1</span>
<span style="color: #000000; font-weight: bold;">var</span> lvl1Code:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The array that contains all of the level codes</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArray:<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>lvl1Code<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h4> Frame 2:</h4>

<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;">//These variables are needed for moving the ball</span>
<span style="color: #000000; font-weight: bold;">var</span> ballXSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">10</span>; <span style="color: #808080; font-style: italic;">//X Speed of the Ball</span>
<span style="color: #000000; font-weight: bold;">var</span> ballYSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">10</span>; <span style="color: #808080; font-style: italic;">//Y Speed of the Ball</span>
&nbsp;
<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;">//this function will run during every single frame</span>
	<span style="color: #808080; font-style: italic;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_xmouse</span> - mcPaddle.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">*</span>.5;
&nbsp;
	<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the left</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&lt;</span> mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
		mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the right</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
		mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//MOVING THE BALL</span>
	mcBall.<span style="color: #0066CC;">_x</span> += ballXSpeed;
	mcBall.<span style="color: #0066CC;">_y</span> += ballYSpeed;
	<span style="color: #808080; font-style: italic;">//Bouncing the ball off of the walls</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">Stage</span>.<span style="color: #006600;">width</span>-mcBall.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the ball hits the right side</span>
		<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
		ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_x</span> <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;">//if the ball hits the left side</span>
		<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
		ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">Stage</span>.<span style="color: #006600;">height</span>-mcBall.<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 ball hits the bottom</span>
		<span style="color: #808080; font-style: italic;">//then bounce up</span>
		ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_y</span> <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;">//if the ball hits the top</span>
		<span style="color: #808080; font-style: italic;">//then bounce down</span>
		ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">hitTest</span><span style="color: #66cc66;">&#40;</span>mcPaddle<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;">//calculate the ball angle if ball hits paddle</span>
		calcBallAngle<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;
<span style="color: #000000; font-weight: bold;">function</span> calcBallAngle<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;">//ballPosition is the position of the ball is on the paddle</span>
	<span style="color: #000000; font-weight: bold;">var</span> ballPosition:<span style="color: #0066CC;">Number</span> = mcBall.<span style="color: #0066CC;">_x</span> - mcPaddle.<span style="color: #0066CC;">_x</span>;
	<span style="color: #808080; font-style: italic;">//hitPercent converts ballPosition into a percent</span>
	<span style="color: #808080; font-style: italic;">//All the way to the left is -.5</span>
	<span style="color: #808080; font-style: italic;">//All the way to the right is .5</span>
	<span style="color: #808080; font-style: italic;">//The center is 0</span>
	<span style="color: #000000; font-weight: bold;">var</span> hitPercent:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>ballPosition <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span>mcPaddle.<span style="color: #0066CC;">_width</span> - mcBall.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> - .5;
	<span style="color: #808080; font-style: italic;">//Gets the hitPercent and makes it a larger number so the</span>
	<span style="color: #808080; font-style: italic;">//ball actually bounces</span>
	ballXSpeed = hitPercent <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">10</span>;
	<span style="color: #808080; font-style: italic;">//Making the ball bounce back up</span>
	ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</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: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span><span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">//Places bricks onto Level</span>
	<span style="color: #808080; font-style: italic;">//finding the array length of the lvl code</span>
	<span style="color: #808080; font-style: italic;">//The index has to be currentLvl-1 because:</span>
	<span style="color: #808080; font-style: italic;">//array indexes start on 0 and our lvl starts at 1</span>
	<span style="color: #808080; font-style: italic;">//our level will always be 1 higher than the actual index of the array</span>
	<span style="color: #000000; font-weight: bold;">var</span> arrayLength:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lvlArray</span><span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span>;
	<span style="color: #808080; font-style: italic;">//the current row of bricks we are creating</span>
	<span style="color: #000000; font-weight: bold;">var</span> brickRow:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #808080; font-style: italic;">//Now, creating a loop which places the bricks onto the stage</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;">Number</span> = <span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>arrayLength;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//checking if it should place a brick there</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<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;">//creating a variable which holds the brick instance</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mcBrick'</span>, <span style="color: #ff0000;">'brick'</span>+i,<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 brick's coordinates via the i variable and brickRow</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">15</span>+<span style="color: #66cc66;">&#40;</span>i-brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">75</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">10</span>+brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">20</span>;
			<span style="color: #808080; font-style: italic;">//giving this brick some actions</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<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: #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;">mcBall</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 this touches the ball</span>
					<span style="color: #808080; font-style: italic;">//then destroy this mofugger!</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 make the ball bounce away</span>
					ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">//checks if the current brick needs a new row</span>
			<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span>;c<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">10</span>;c++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i == c<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					brickRow ++;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//finally, run the function</span></pre></div></div>

<h4>The 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/brick-breaker-as2/pt4/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/brick-breaker-as2/pt4/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt4/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-4/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Brick Breaker Game in AS2 &#8211; Part 3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-3/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 12:02:44 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1062</guid>
		<description><![CDATA[Table of Contents Coding Paddle Movement Programming the Ball Setting Up the Bricks on Stage Hit Testing the Bricks Creating Levels Finishing Touches Step 3: Setting Up the Bricks on Stage Ok, we&#8217;ve got the paddle and the ball. Now, the only major thing left to program is the brick. This is also the hardest [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2">Coding Paddle Movement</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-2">Programming the Ball</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-3">Setting Up the Bricks on Stage</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-4">Hit Testing the Bricks</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-5">Creating Levels</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 3: Setting Up the Bricks on Stage</h3>
<p>Ok, we&#8217;ve got the paddle and the ball. Now, the only major thing left to program is the brick. This is also the hardest to program, so it will be split up into 2 parts, setting them up, and breaking them down. Let us begin.</p>
<p>First of all, we need to look at how we&#8217;re going to set the bricks on the stage. We&#8217;re going to use an array with different numbers that will signify different bricks. In this tutorial, because we are keeping it simple, we will only use 2 different numbers, 1 and 0. 1 will mean to place a brick, and 0 will mean and empty space. An example for a simple level would be this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//this would create one row of bricks</span>
lvl1Array:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Of course, this code will do nothing unless we create a function to actually make the bricks. The first step to this is actually making the brick MovieClip. So, mine is just going to be a plain white, with dimensions of 70&#215;15 pixels. Also, this MovieClip should be exported for ActionScript so we can dynamically add it to the stage.<br />
<img src="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt3/brick-symbol.gif" alt="Convert your brick" /></p>
<p>Got it? Now, here comes the tricky part, programming it. First of all, we are going to define a function called <tt>makeLvl()</tt>. Then, we are going to need to make some starting variables. These variables, however, can&#8217;t be reset every time the user beats a level, so it we have to make a frame before the the frame with all of the code. If you don&#8217;t get it, then here&#8217;s a picture.<br />
<img src="http://mrsunstudios.com/obj/tuts/brick-breaker-as3/pt3/frame.gif" alt="Making a new frame at the beginning" /></p>
<p>In this frame, put the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Current level player is on</span>
<span style="color: #000000; font-weight: bold;">var</span> currentLvl:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span>;
<span style="color: #808080; font-style: italic;">//The array code for lvl 1</span>
<span style="color: #000000; font-weight: bold;">var</span> lvl1Code:<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;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//The array that contains all of the level codes</span>
<span style="color: #000000; font-weight: bold;">var</span> lvlArray:<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>lvl1Code<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Hopefully, I&#8217;ve explained what each of the variables do well enough that you understand. Also, in the 2nd frame, the game frame, just add a <tt>stop();</tt> to the beginning. This way, the game doesn&#8217;t keep on looping.</p>
<p>Now, we&#8217;re going to go back to the game farme and we are going to add some intense code into the <tt>makeLvl()</tt> function. Here we go:</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: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span><span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">//Places bricks onto Level</span>
	<span style="color: #808080; font-style: italic;">//finding the array length of the lvl code</span>
	<span style="color: #808080; font-style: italic;">//The index has to be currentLvl-1 because:</span>
	<span style="color: #808080; font-style: italic;">//array indexes start on 0 and our lvl starts at 1</span>
	<span style="color: #808080; font-style: italic;">//our level will always be 1 higher than the actual index of the array</span>
	<span style="color: #000000; font-weight: bold;">var</span> arrayLength:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">lvlArray</span><span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span>;
	<span style="color: #808080; font-style: italic;">//the current row of bricks we are creating</span>
	<span style="color: #000000; font-weight: bold;">var</span> brickRow:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #808080; font-style: italic;">//Now, creating a loop which places the bricks onto the stage</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;">Number</span> = <span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>arrayLength;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//checking if it should place a brick there</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lvlArray<span style="color: #66cc66;">&#91;</span>currentLvl-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>i<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;">//creating a variable which holds the brick instance</span>
			<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mcBrick'</span>, <span style="color: #ff0000;">'brick'</span>+i,<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 brick's coordinates via the i variable and brickRow</span>
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">15</span>+<span style="color: #66cc66;">&#40;</span>i-brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">75</span>;
			<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'brick'</span>+i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_y</span> = <span style="color: #cc66cc;">10</span>+brickRow<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">20</span>;
			<span style="color: #808080; font-style: italic;">//checks if the current brick needs a new row</span>
			<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1</span>;c<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">10</span>;c++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i == c<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">7</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					brickRow ++;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//finally, run the function</span></pre></div></div>

<p>The next step is getting the ball to break the bricks. But, that is for a later time.</p>
<h4>The 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/brick-breaker-as2/pt3/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/brick-breaker-as2/pt3/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt3/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-3/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Brick Breaker Game in AS2 &#8211; Part 2</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-2/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-2/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 12:01:42 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1056</guid>
		<description><![CDATA[Table of Contents Coding Paddle Movement Programming the Ball Setting Up the Bricks on Stage Hit Testing the Bricks Creating Levels Finishing Touches Step 2: Programming the Ball The next obvious step in creating a brick breaker game is making the ball. So, I&#8217;m just going to make a small 10&#215;10 pixel white circle into [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2">Coding Paddle Movement</a></li>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-2">Programming the Ball</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-3">Setting Up the Bricks on Stage</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-4">Hit Testing the Bricks</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-5">Creating Levels</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 2: Programming the Ball</h3>
<p>The next obvious step in creating a brick breaker game is making the ball. So, I&#8217;m just going to make a small 10&#215;10 pixel white circle into the symbol, &#8220;mcBall&#8221;.<br />
<img src="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt2/ball.gif" alt="My lil' ball" width="278" height="161" /><br />
I&#8217;m also going to give my ball an instance name of mcBall.</p>
<p>Now, before we actually create the code for ball movement, we need 2 variables. Can you guess what they are? Great job! They <em>are</em> the x speed variable and the y speed variable! You&#8217;re such a smart little child. Add the following code to the very beginning (line 1) of your code. I like to organize all my variables so that they are all in the same place.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//These variables are needed for moving the ball</span>
<span style="color: #000000; font-weight: bold;">var</span> ballXSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">10</span>; <span style="color: #808080; font-style: italic;">//X Speed of the Ball</span>
<span style="color: #000000; font-weight: bold;">var</span> ballYSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">10</span>; <span style="color: #808080; font-style: italic;">//Y Speed of the Ball</span></pre></div></div>

<p>You can change these values if you want, but this is what I&#8217;m going to use. Now, we have to use these variables to make the ball move. We should now add the following code to 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;">//MOVING THE BALL</span>
mcBall.<span style="color: #0066CC;">_x</span> += ballXSpeed;
mcBall.<span style="color: #0066CC;">_y</span> += ballYSpeed;</pre></div></div>

<p>Of course, when you test the movie, the ball is just going to move diagonally without any force stopping it. The next step is to make the ball bounce off of the walls. This is pretty easy if you think about it. All you have to do is multiply the x speed by -1 if it hits a vertical wall, and the same for the y speed with a horizontal wall. Let&#8217;s do it. Add the following code to 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;">//Bouncing the ball off of the walls</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">Stage</span>.<span style="color: #006600;">width</span>-mcBall.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the ball hits the right side</span>
		<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
		ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_x</span> <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;">//if the ball hits the left side</span>
		<span style="color: #808080; font-style: italic;">//of the screen, then bounce off</span>
		ballXSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">Stage</span>.<span style="color: #006600;">height</span>-mcBall.<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 ball hits the bottom</span>
		<span style="color: #808080; font-style: italic;">//then bounce up</span>
		ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">_y</span> <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;">//if the ball hits the top</span>
		<span style="color: #808080; font-style: italic;">//then bounce down</span>
		ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
	<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you test the movie, then the ball will just keep on bouncing off the walls. Of course, we&#8217;re going to change the bottom bounce, but for now we&#8217;ll keep it like this. The next step is making the ball bounce off of the paddle. This might take a little more math skills. We don&#8217;t want the ball to keep on moving at the same angle the whole time, so we&#8217;re going to make it change depending on which part of the paddle it hits. Because this will take some intense calculations, we&#8217;re going to make a new function called <tt>calcBallAngle()</tt>. So, let&#8217;s first add this code to the <tt>onEnterFrame()</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #0066CC;">hitTest</span><span style="color: #66cc66;">&#40;</span>mcPaddle<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;">//calculate the ball angle if ball hits paddle</span>
	calcBallAngle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This part is simple. It just runs the <tt>calcBallAngle()</tt> function whenever the ball hits the paddle. Now, here&#8217;s the code for the actual function. It&#8217;s pretty intense.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calcBallAngle<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;">//ballPosition is the position of the ball is on the paddle</span>
	<span style="color: #000000; font-weight: bold;">var</span> ballPosition:<span style="color: #0066CC;">Number</span> = mcBall.<span style="color: #0066CC;">_x</span> - mcPaddle.<span style="color: #0066CC;">_x</span>;
	<span style="color: #808080; font-style: italic;">//hitPercent converts ballPosition into a percent</span>
	<span style="color: #808080; font-style: italic;">//All the way to the left is -.5</span>
	<span style="color: #808080; font-style: italic;">//All the way to the right is .5</span>
	<span style="color: #808080; font-style: italic;">//The center is 0</span>
	<span style="color: #000000; font-weight: bold;">var</span> hitPercent:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>ballPosition <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span>mcPaddle.<span style="color: #0066CC;">_width</span> - mcBall.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> - .5;
	<span style="color: #808080; font-style: italic;">//Gets the hitPercent and makes it a larger number so the</span>
	<span style="color: #808080; font-style: italic;">//ball actually bounces</span>
	ballXSpeed = hitPercent <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">10</span>;
	<span style="color: #808080; font-style: italic;">//Making the ball bounce back up</span>
	ballYSpeed <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I&#8217;ve commented the code out pretty extensively. Just read it and hopefully you&#8217;ll understand.</p>
<p>That&#8217;s basically all that I&#8217;m going to teach you today. Join us next time!</p>
<h4>The 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/brick-breaker-as2/pt2/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/brick-breaker-as2/pt2/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt2/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2-part-2/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a Brick Breaker Game in AS2</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 12:00:40 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[brick breaker]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1052</guid>
		<description><![CDATA[Table of Contents Coding Paddle Movement Programming the Ball Setting Up the Bricks on Stage Hit Testing the Bricks Creating Levels Finishing Touches Step 1: Coding Paddle Movement Alright, I&#8217;ve decided to make a tutorial for you guys on how to create a brick breaker game in ActionScript 2.0. It&#8217;ll be very similar to my [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li class="c_chap"><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2">Coding Paddle Movement</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-2">Programming the Ball</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-3">Setting Up the Bricks on Stage</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-4">Hit Testing the Bricks</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-5">Creating Levels</a></li>
<li><a href="http://www.mrsunstudios.com/2009/01/tutorial-create-a-brick-breaker-game-in-as2-part-6">Finishing Touches</a></li>
</ol>
</div>
<h3>Step 1: Coding Paddle Movement</h3>
<p>Alright, I&#8217;ve decided to make a tutorial for you guys on how to create a brick breaker game in ActionScript 2.0. It&#8217;ll be very similar to my <a href="http://www.mrsunstudios.com/2008/07/tutorial-create-a-brick-breaker-game-in-as3/">one for AS3</a>, except coded differently. So, here we go, eh?</p>
<p>First of all, we&#8217;re going to make the background of the stage black, simply because it looks better, and we&#8217;re going to set the frame rate to 24. Then, we&#8217;re going to draw the paddle and we&#8217;re going to convert it into a MovieClip.<br />
<img src="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt1/paddle.gif" width="266" height="163"/><br />
Mine is 55&#215;10</p>
<p>Next, we&#8217;re going to give the paddle an instance name, <tt>mcPaddle</tt>. The instance name is case sensitive, so type it exactly the way I did.</p>
<p>Now, we get into some basic ActionScript. Create a new layer named &#8220;actions&#8221; where all of your code will go in. Next, type in this code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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;">//this function will run during every single frame</span>
	<span style="color: #808080; font-style: italic;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_xmouse</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Hopefully, I&#8217;ve explained enough in the comments for you to get an idea of what the code does. If you test the movie, you might see a few problems. First of all, the paddle is not centered with the mouse, but is left aligned with it. This is easy to fix. Just replace:</p>

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

<p>with:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_xmouse</span> - mcPaddle.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">*</span>.5;</pre></div></div>

<p>This code basically makes it so instead of using the paddle&#8217;s <tt>_x</tt> value, which is the left side of the paddle, it uses the middle of the paddle instead to follow the mouse.</p>
<p>Another problem with this code is that the paddle sometimes runs off stage. We don&#8217;t want this, it annoys the user. So, we&#8217;re going to add the following code to 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;">//If the mouse goes off too far to the left</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&lt;</span> mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
	mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the right</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
	mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This code will keep your paddle in bounds, no matter how large the stage is or how wide your paddle is. I&#8217;m leaving the explanation of the code up to you to figure out yourself. After all, you can never become a real programmer if you just copy and paste the code without any idea of what it means.</p>
<p>And now I conclude part one of this tutorial with the final code you should have in your file with an example of what mine looks like.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><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;">//this function will run during every single frame</span>
	<span style="color: #808080; font-style: italic;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_xmouse</span> - mcPaddle.<span style="color: #0066CC;">_width</span><span style="color: #66cc66;">*</span>.5;
&nbsp;
	<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the left</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&lt;</span> mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
		mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #cc66cc;">0</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">//If the mouse goes off too far to the right</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_xmouse</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span> <span style="color: #66cc66;">/</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;">//Keep the paddle on stage</span>
		mcPaddle.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">width</span> - mcPaddle.<span style="color: #0066CC;">_width</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<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/brick-breaker-as2/pt1/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/brick-breaker-as2/pt1/source.swf"></embed></object></p>
<p><a href="http://mrsunstudios.com/obj/tuts/brick-breaker-as2/pt1/source.fla">Source .fla File</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-create-a-brick-breaker-game-in-as2/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Tutorial: Buying Adobe CS4 Suite and Saving Hundreds of Dollars</title>
		<link>http://www.mrsunstudios.com/blog/misc/tutorial-buying-adobe-cs4-suite-and-saving-hundreds-of-dollars/</link>
		<comments>http://www.mrsunstudios.com/blog/misc/tutorial-buying-adobe-cs4-suite-and-saving-hundreds-of-dollars/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 12:00:47 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Misc]]></category>
		<category><![CDATA[Tools/Services]]></category>
		<category><![CDATA[academic]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[bargain]]></category>
		<category><![CDATA[cheap]]></category>
		<category><![CDATA[superstore]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=955</guid>
		<description><![CDATA[Hey guys. In this tutorial, to celebrate the announcement of Adobe&#8217;s CS4 suite, I&#8217;ll show you guys how to buy programs from Adobe&#8217;s new CS4 Suite much cheaper than what you&#8217;d get from Adobe itself (and legally too). You can also buy many other programs for much less than you would get if you&#8217;re eligible. [...]]]></description>
			<content:encoded><![CDATA[<p>Hey guys. In this tutorial, to celebrate the announcement of Adobe&#8217;s CS4 suite, I&#8217;ll show you guys how to buy programs from Adobe&#8217;s new CS4 Suite much cheaper than what you&#8217;d get from Adobe itself (and legally too). You can also buy many other programs for much less than you would get if you&#8217;re eligible. The site is Academic Superstore.<br />
<a href="http://www.academicsuperstore.com"><img src="http://www.mrsunstudios.com/wp-content/uploads/2008/09/academic_superstore_header.gif" alt="Academic Superstore" title="Academic Superstore" /></a></p>
<p>Academic Superstore is a great place to find any kind of program for an amazingly cheap price (up to 80% off). It&#8217;s the place where I bought my <a href="http://www.academicsuperstore.com/products/Adobe/Creative+Suite+Web+Collections/902141">Creative Suite 3 Design Premium</a> for about $1000 less than it would have been from the Adobe site. That is quite a deal, isn&#8217;t it? But, there is one catch. You need to be eligible for the savings. Don&#8217;t worry, it&#8217;s very easy to be eligible.</p>
<div style="text-align:center"><a href="http://www.academicsuperstore.com/eligibility"><img src="http://www.mrsunstudios.com/wp-content/uploads/2008/09/superstore_eligibility.gif" alt="" title="Academic Superstore Eligibility"  /></a></div>
<p><a href="http://www.academicsuperstore.com/eligibility">Here is the site&#8217;s page</a> about eligibility for savings. In order to be eligible, all you have to be is either a student, the parent of a student, or a teacher. The eligibility requirements are different for different products. I&#8217;m a student, so that&#8217;s how I got my savings.</p>
<p>So, why don&#8217;t you go now and <a href="http://www.academicsuperstore.com/cs4/">pre-order Adobe CS4</a> now?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/misc/tutorial-buying-adobe-cs4-suite-and-saving-hundreds-of-dollars/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tutorial: How to Draw Simple Shapes in AS3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-how-to-draw-simple-shapes-in-as3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-how-to-draw-simple-shapes-in-as3/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 12:00:35 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[draw]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[shapes]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=837</guid>
		<description><![CDATA[Hey guys, today we&#8217;re going to learn something fun and exciting! That is, drawing shapes in ActionScript 3.0. I&#8217;ve actually covered a little bit of this in my platform tutorial, but I want to make it the focus of this tutorial. So, let&#8217;s get started shall we? First of all, we&#8217;re going to create a [...]]]></description>
			<content:encoded><![CDATA[<p>Hey guys, today we&#8217;re going to learn something fun and exciting! That is, drawing shapes in ActionScript 3.0. I&#8217;ve actually covered a little bit of this in my platform tutorial, but I want to make it the focus of this tutorial. So, let&#8217;s get started shall we?</p>
<p>First of all, we&#8217;re going to create a new flash file. Then, we&#8217;ll name &#8220;Layer 1&#8243; to &#8220;actions&#8221; because that will be the only layer that we&#8217;ll need to use today.</p>
<h3>Drawing Circles</h3>
<p>In order to draw a circle, we have to place the following code into 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;">//defining a new variable that will be the circle</span>
<span style="color: #000000; font-weight: bold;">var</span> newCircle:Shape = <span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//now we begin adding a fill to the variable</span>
<span style="color: #808080; font-style: italic;">//this will make the circle red</span>
newCircle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFF0000<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//now we call a function that will draw the circle</span>
<span style="color: #808080; font-style: italic;">//the first value (100) is the x value</span>
<span style="color: #808080; font-style: italic;">//the second value (100) is the y value</span>
<span style="color: #808080; font-style: italic;">//and the third value(50), is the radius</span>
<span style="color: #808080; font-style: italic;">//the x and y values count for the center of the circle,</span>
<span style="color: #808080; font-style: italic;">//now the left corner like you'd usually see</span>
newCircle.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//this function just tells actionscript that we're done</span>
<span style="color: #808080; font-style: italic;">//using this red fill</span>
newCircle.<span style="color: #006600;">graphics</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;">//adding the circle to the stage</span>
addChild<span style="color: #66cc66;">&#40;</span>newCircle<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I really commented out this one so hopefully you can understand what this code does. This will just draw a red circle that is 50 pixels in radius, 50 pixels from left and top of the stage. Without the comments, it only takes 5 lines, so it isn&#8217;t too complicated.</p>
<h4>End Product</h4>
<p> (I made the stage a bit smaller)<br />
<center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="200" 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/shapes-as3/circle.swf" /><embed type="application/x-shockwave-flash" width="200" height="200" src="http://www.mrsunstudios.com/obj/tuts/shapes-as3/circle.swf"></embed></object></center></p>
<h3>Drawing Rectangles/Squares</h3>
<p>Drawing a rectangle or square is very similar to the circle, except with a different function called. Here&#8217;s the commented code for doing so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> newRect:Shape = <span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
newRect.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFF0000<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//now we call a function that will draw the square</span>
<span style="color: #808080; font-style: italic;">//the first value (50) is the x value</span>
<span style="color: #808080; font-style: italic;">//the second value (50) is the y value</span>
<span style="color: #808080; font-style: italic;">//the third value (100), is the width</span>
<span style="color: #808080; font-style: italic;">//and the final value (100), is the height</span>
newRect.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
newRect.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>newRect<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I just commented the new stuff that was added in.</p>
<h4>The End Product</h4>
<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="200" 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/shapes-as3/rect.swf" /><embed type="application/x-shockwave-flash" width="200" height="200" src="http://www.mrsunstudios.com/obj/tuts/shapes-as3/rect.swf"></embed></object></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-how-to-draw-simple-shapes-in-as3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Simple Dynamic Perspective in AS2 and AS3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-simple-dynamic-perspective-in-as2-and-as3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-simple-dynamic-perspective-in-as2-and-as3/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 12:00:45 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[perspective]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=784</guid>
		<description><![CDATA[This tutorial will show you how to create a simple illusion of perspective, dynamically in both ActionScript 2.0 and ActionScript 3.0. Although this technique isn&#8217;t the most advanced or realistic, it gets the job done. Let&#8217;s see it, shall we? ActionScript 2.0 We&#8217;ll first make the effect in ActionScript 2.0. To begin, we have to [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will show you how to create a simple illusion of perspective, dynamically in both ActionScript 2.0 and ActionScript 3.0. Although this technique isn&#8217;t the most advanced or realistic, it gets the job done. Let&#8217;s see it, shall we?</p>
<h3>ActionScript 2.0</h3>
<p>We&#8217;ll first make the effect in ActionScript 2.0. To begin, we have to create an object that we have to add the perspective to. I&#8217;m simply going to make a square that is 50x50px. Here it is:<br />
<img src="http://mrsunstudios.com/obj/tuts/dynamic-perspective/box.gif" alt="My lil box" /><br />
Next, convert it into a MovieClip. Make sure it&#8217;s registration point is set to the center. It doesn&#8217;t matter what you call it. I&#8217;m going to call mine &#8220;box&#8221;. After doing so, give your newly created MovieClip an instance name of <tt>mcBox</tt>. We can now reference it in ActionScript.</p>
<p>Now, simply create a new layer called &#8220;actions&#8221; and place the following code into it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//creating a function that will run every frame</span>
mcBox.<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;">//scaling the box based on the y value</span>
	mcBox.<span style="color: #0066CC;">_xscale</span> = <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span>mcBox.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
	mcBox.<span style="color: #0066CC;">_yscale</span> = <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span>mcBox.<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//making the box follow the mouse</span>
	<span style="color: #808080; font-style: italic;">//so we can actually show the perspective change dynamically</span>
	mcBox.<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">_xmouse</span>;
	mcBox.<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">_ymouse</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>It&#8217;s pretty easy, right? But, if we test out the application, it isn&#8217;t as realistic as it could be. All you have to do to help with it is to add a gradient to the background, and have a horizon line at the top. Here&#8217;s the final product:</p>
<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/dynamic-perspective/source-as2.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/dynamic-perspective/source-as2.swf"></embed></object></p>
<h3>ActionScript 3.0</h3>
<p>Now, we can make this effect in ActionScript 3.0. We&#8217;re going to start off with the same thing as we did in AS2. If you skipped that part, don&#8217;t worry I&#8217;ll sum it up for you.</p>
<ol>
<li>Make a 50x50px box</li>
<li>Convert it into a MovieClip with registration point in center</li>
<li>Give it an instance name of <tt>mcBox</tt></li>
<li>Create a new layer called actions</li>
</ol>
<p>Now, all we have to do is add some code to the actions layer. Here it is:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcBox.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBox<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//creating a function that will run every frame</span>
<span style="color: #000000; font-weight: bold;">function</span> moveBox<span style="color: #66cc66;">&#40;</span>event: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;">//scaling the box based on the y value</span>
	mcBox.<span style="color: #006600;">scaleX</span> = mcBox.<span style="color: #006600;">y</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span>;
	mcBox.<span style="color: #006600;">scaleY</span> = mcBox.<span style="color: #006600;">y</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//making the box follow the mouse</span>
	<span style="color: #808080; font-style: italic;">//so we can actually show the perspective change dynamically</span>
	mcBox.<span style="color: #006600;">x</span> = mouseX;
	mcBox.<span style="color: #006600;">y</span> = mouseY;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Pretty nifty code, eh? Well, here&#8217;s what it&#8217;ll turn out to look like (after I add a gradient background).</p>
<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/dynamic-perspective/source-as3.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/dynamic-perspective/source-as3.swf"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-simple-dynamic-perspective-in-as2-and-as3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Pagination in ActionScript 3</title>
		<link>http://www.mrsunstudios.com/blog/flash/tutorial-pagination-in-actionscript-3/</link>
		<comments>http://www.mrsunstudios.com/blog/flash/tutorial-pagination-in-actionscript-3/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 12:00:36 +0000</pubDate>
		<dc:creator>MrSun</dc:creator>
				<category><![CDATA[All Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner Tutorials]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=578</guid>
		<description><![CDATA[This is going to be another tutorial about pagination. This time, however, it will be done in ActionScript 3.0. Here&#8217;s a brief description of what this tutorial will be about, taken from my previous tutorial in ActionScript 2.0: This is going to be a smaller tutorial about how to paginate objects in ActionScript 2.0. By [...]]]></description>
			<content:encoded><![CDATA[<p>This is going to be another tutorial about pagination. This time, however, it will be done in ActionScript 3.0. Here&#8217;s a brief description of what this tutorial will be about, taken from my previous tutorial in <a href="http://www.mrsunstudios.com/2008/08/tutorial-pagination-in-actionscript-2/">ActionScript 2.0</a>:</p>
<blockquote><p>This is going to be a smaller tutorial about how to paginate objects in ActionScript 2.0. By paginate, I mean taking a large group of objects, and making it so the user can navigate through them by page. For example, in our previous tutorial, <a href="../2008/08/tutorial-make-a-rhythm-game-in-as2/">Make a Rhythm Game in AS2</a>, we made a list of levels that the user could play. However, this list was only limited to 5 levels, and we didn’t paginate them. We’re going to do something like this today.</p></blockquote>
<p>In this tutorial, I&#8217;m just going to take excerpts of text from my AS2 tutorial and will make updates in the code a bit. The process will be the same, just with different code. Let&#8217;s get started, shall we?</p>
<blockquote><p>First of all, we’re going to make a large array of random values that we can paginate. Because we don’t care what the values are right now, we’re just going to make them totally random. If you’re going to use pagination for something more than a tutorial, then you probably should use your own values.</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//this values array will hold all of the values we need to paginate</span>
<span style="color: #000000; font-weight: bold;">var</span> values:<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: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//defining the 'i' variable so we can use it multiple times in for loops</span>
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//this loop adds 1000 different values to the array</span>
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&amp;</span>lt;<span style="color: #cc66cc;">1000</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//the values will range from 0-9999</span>
	values<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</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;">9999</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you want to check, then try making a trace() statement to see what the values are.</p>
<p>Next, we’re going to have to make a MovieClip called &#8220;mcValueHolder&#8221; that will be placed onto stage with a certain value in the array within it. We also need a dynamic text field to actually have the value in it. Here’s what mine looks like:</p>
<p><img src="http://mrsunstudios.com/obj/tuts/paginate-as2/movieclip.png" alt="The MovieClip" /></p>
<p>Also, give the text field an instance name of &#8220;txtValue&#8221;, so we can reference it in actionscript.</p>
<blockquote><p>Now, we have to export the MovieClip for ActionScript. If you don’t know how to do that, then right click on the MovieClip in the library and click on “Linkage…”. Then, check off “Export for ActionScript”. That’s all you have to do. Click on “Ok” and we can continue.</p>
<p>We also need two buttons to click to get to the next page and the previous page. Hopefully, you can figure out how to do that yourself. Just add them to the bottom of the stage and give them instance names of “mcPrev” and “mcNext”.</p></blockquote>
<p>Also, we need to make a text field which tells the user which page they are on out of the total pages. Just add it to the stage and set the instance name to txtPage.</p>
<blockquote><p>Now for the hard part, actually coding the pagination. Place this code in after we define the <tt>values</tt> variable.</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//the current page we're on</span>
<span style="color: #000000; font-weight: bold;">var</span> page:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span>;
<span style="color: #808080; font-style: italic;">//the limit of values we need per page</span>
<span style="color: #000000; font-weight: bold;">var</span> limit:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">6</span>;
<span style="color: #808080; font-style: italic;">//the current row that we're working on</span>
<span style="color: #000000; font-weight: bold;">var</span> row:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #808080; font-style: italic;">//The total pages that we have</span>
<span style="color: #000000; font-weight: bold;">var</span> totalPages:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">ceil</span><span style="color: #66cc66;">&#40;</span>values.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">/</span>limit<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> createValues<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;">//this will always limit the amount to the limit variable</span>
	<span style="color: #808080; font-style: italic;">//but, &quot;i&quot; will act as a marker for which part of the values</span>
	<span style="color: #808080; font-style: italic;">//array that we're going to use</span>
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #66cc66;">&#40;</span>page-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>limit;i<span style="color: #66cc66;">&lt;</span>page<span style="color: #66cc66;">*</span>limit;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//checks if there actually is a value where &quot;i&quot; is</span>
		<span style="color: #808080; font-style: italic;">//otherwise we'll get some undefined movieclips</span>
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i <span style="color: #66cc66;">&lt;</span> values.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> newValue:mcValueHolder = <span style="color: #000000; font-weight: bold;">new</span> mcValueHolder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//sets the coordinates based on the row</span>
			newValue.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>;
			newValue.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">5</span>+newValue.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">*</span>row;
			<span style="color: #808080; font-style: italic;">//sets this guys value to the correct part of the array</span>
			newValue.<span style="color: #006600;">txtValue</span>.<span style="color: #0066CC;">text</span> = values<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
			<span style="color: #808080; font-style: italic;">//changing this guys name so we can reference it later</span>
			newValue.<span style="color: #0066CC;">name</span> = <span style="color: #ff0000;">'value'</span>+row;
			<span style="color: #808080; font-style: italic;">//adds the mc to stage</span>
			addChild<span style="color: #66cc66;">&#40;</span>newValue<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//move onto the next row</span>
			row ++;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">//then we reset the rows so we can use the variable again</span>
	row=<span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//function to remove the mc's</span>
<span style="color: #000000; font-weight: bold;">function</span> removeValues<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;">//this loop will run once for each mc on stage</span>
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>limit;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//get the object in the current row and kill it!</span>
		removeChild<span style="color: #66cc66;">&#40;</span>getChildByName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'value'</span>+i<span style="color: #66cc66;">&#41;</span><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;">//we're defining a function which updates the string</span>
<span style="color: #000000; font-weight: bold;">function</span> updateString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	txtPage.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">'Page '</span>+page+<span style="color: #ff0000;">' out of '</span>+totalPages;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//next page and previous page functions</span>
<span style="color: #000000; font-weight: bold;">function</span> prevPage<span style="color: #66cc66;">&#40;</span>event: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;">//checking if the current page isn't too low</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>page <span style="color: #66cc66;">&gt;</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;">//take away all of the objects so we can make new ones</span>
		removeValues<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		page --;
		createValues<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//then update the page text</span>
		updateString<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: #000000; font-weight: bold;">function</span> nextPage<span style="color: #66cc66;">&#40;</span>event: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;">//checking if the current page isn't too high</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>page <span style="color: #66cc66;">&lt;</span> totalPages<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		removeValues<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		page ++;
		createValues<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		updateString<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: #808080; font-style: italic;">//first things first, update the string to be as it is</span>
updateString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//then we place the movieclips onto the stage</span>
createValues<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//adding listeners to the buttons</span>
btnPrev.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, prevPage<span style="color: #66cc66;">&#41;</span>;
btnNext.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, nextPage<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<blockquote><p>
Pretty intense, eh? Well, this code is basically useless by itself. I doubt that anybody wants to look through random numbers. But, implementing it into your project will help a lot.
</p></blockquote>
<h3>The Final Product</h3>
<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/paginate-as3/source.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mrsunstudios.com/obj/tuts/paginate-as3/source.swf"></embed></object></p>
<p><a href="http://www.mrsunstudios.com/obj/tuts/paginate-as3/source.fla">Source .fla file</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mrsunstudios.com/blog/flash/tutorial-pagination-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

