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

<channel>
	<title>Flash Game Tuts &#187; Beginner</title>
	<atom:link href="http://www.flashgametuts.com/tutorials/category/beginner/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flashgametuts.com</link>
	<description>Free Flash Game Tutorials in AS2 and AS3</description>
	<lastBuildDate>Wed, 29 Jul 2009 14:43:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 6</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-6/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-6/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:06:48 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=357</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<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://www.flashgametuts.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 <tt>beginCode()</tt> function. We aren&#8217;t going to remove the textfield 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>The next thing we have to do is display the current level, 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. But, I will give you some code to use below all the other.</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 to update the text fields</span>
addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, updateTextFields<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>You can create your own text fields with and update them with that function. But, be sure to remove that listener if the gamer loses, or something bad will happen, something very bad&#8230;</p>
<p>And don&#8217;t think I forgot about scoring. Just define a variable at the top called <tt>score</tt> and have it update along with the other stats. Also, you have to make it increase in the <tt>Brick</tt> class every time the brick is hit. You might want to show the score when the player loses too. But, that&#8217;s just optional.</p>
<p>Well, those are all the finishing touches that I&#8217;m going to add on to my game at least. Here is the final product of all our work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 5</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-5/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-5/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:05:38 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=281</guid>
		<description><![CDATA[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. We&#8217;re first going [...]]]></description>
			<content:encoded><![CDATA[<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;">int</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 in the Brick.as after defining <tt>_root</tt> in the <tt>beginClass()</tt> function.</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>hitTestObject</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 the amount of bricks on 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 a listener that will check if the value of bricks is 0.<br />
You can do so in the <tt>beginCode()</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//adding a listener to check if the level is done</span>
addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, checkLevel<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Next, we have to define this function. Place this at the end of the code, but before we run <tt>beginCode()</tt></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> checkLevel<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;">//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></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.</p>
<p>Remember at the bottom of the code when we ran <tt>beginCode()</tt>? Well, we can just have it run after listening to a mouse click. Here&#8217;s the code.</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 clicks, then begin the game</span>
<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, beginCode<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Just replace <tt>beginCode();</tt> with that. We also have to change the <tt>beginCode()</tt> function itself just slightly so it will accept a mouse event. When we define the function, just change it to:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span>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;">//removes the listener for a click</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, beginCode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#91;</span>..<span style="color: #006600;">Code</span>..<span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you test the movie, however, it looks a bit weird. The bricks appear only after clicking. This can be easily fixed. Just take the <tt>makeLvl()</tt> out of the <tt>beginCode()</tt> function and put it at the bottom of the code.</p>
<p>The next 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 place this code in the <tt>CheckLevel()</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> checkLevel<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;">//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;">//then resetting the ball's and paddle's position</span>
		mcBall.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
		mcBall.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">265</span>;
		mcPaddle.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">230</span>;
		<span style="color: #808080; font-style: italic;">//then removing all of the listeners</span>
		mcPaddle.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
		mcBall.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
		removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, checkLevel<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//then listening for a mouse click to start the game again</span>
		<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, beginCode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</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;">int</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: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</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 <tt>enterFrameEvents</tt> function in Brick.as.</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 player has lost</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">gameOver</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//destroy this brick</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//stop running this code</span>
	removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>For some reason, this code outputs an error. Don&#8217;t worry though, nothing is really wrong.</p>
<p>Now, we have to make the player be able to restart the game after losing. This will be easy. Just add a listener 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;"><span style="color: #808080; font-style: italic;">//The lose frame</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//resetting the game if the mouse is clicked</span>
<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, resetGame<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> resetGame<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;">//removing this listener</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, resetGame<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//resetting the game</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: #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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 4</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-4/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-4/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:04:17 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=260</guid>
		<description><![CDATA[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. But, if you are new to ActionScript 3.0 classes, it&#8217;ll be a pretty cool learning experience. First of all, we&#8217;re going to have to create a class file for the bricks. This [...]]]></description>
			<content:encoded><![CDATA[<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. But, if you are new to ActionScript 3.0 classes, it&#8217;ll be a pretty cool learning experience.</p>
<p>First of all, we&#8217;re going to have to create a class file for the bricks. This is easy, just go to <strong>File->New</strong> and select &#8220;ActionScript File&#8221;.<br />
<img src="http://www.flashgametuts.com/obj/tuts/brick-breaker-as3/pt4/new-actionscript.gif" alt="Create a new ActionScript file" /></p>
<p>Once you&#8217;ve created the file, immediately save it as &#8220;Brick.as&#8221; in the same folder as your .fla file. Then, type the following code in:</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
16
17
18
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Classes must always be wrapped in a package</span>
package <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//importing display elements that we can use in this class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #808080; font-style: italic;">//importing flash events that we can use (like ENTER_FRAME)</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//defining the class name and saying that it</span>
	<span style="color: #808080; font-style: italic;">//extends the MovieClip class, meaning that it has the same</span>
	<span style="color: #808080; font-style: italic;">//properties as a movieclip</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Brick <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//all classes must have a function that runs every time</span>
		<span style="color: #808080; font-style: italic;">//an instance of the class is put on stage</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Brick<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This is a skeleton of what most ActionScript classes will look like. Although making a class is not necessary for this small game, it will definitely help if you want to expand your game. In order the use the class file in your game, you must import it first, so add this code to the first line of the first frame of your main flash file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//IMPORTS</span>
<span style="color: #0066CC;">import</span> Brick;</pre></td></tr></table></div>

<p>We also have to change our previous brick MovieClip, <tt>mcBrick</tt>, to the class, Brick. This way, all of the code we place into Brick.as will be used in the Brick MovieClip.</p>
<p>So, right click the mcBrick MovieClip in your library and click on linkage. This window will pop up.<br />
<img src="http://www.flashgametuts.com/obj/tuts/brick-breaker-as3/pt4/linkage-window.gif" alt="The Linkage Window" /></p>
<p>Now, find where it says &#8220;Class&#8221; (it&#8217;s going to be highlighted), and set it from &#8220;mcBrick&#8221; to &#8220;Brick&#8221;. Now, we have a class. Of course, now we&#8217;re going to have to change the <tt>makeLvl()</tt> function slightly. Now we don&#8217;t add &#8220;mcBrick&#8221; to the stage, but just &#8220;Brick&#8221;. Just change the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> brick:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> mcBrick<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>to:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> brick:Brick = <span style="color: #000000; font-weight: bold;">new</span> Brick<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>If you test it out, it will work out exactly the same as before. Now, we add code to &#8220;Brick.as&#8221;. Let&#8217;s go.</p>
<p>We can&#8217;t access the root level of the document through the <tt>Brick()</tt> function, so we&#8217;re going to add two listeners to it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Brick<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;">//Code that will be run when the brick is added to the stage</span>
	addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//Enter frame code</span>
	addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Next, we define those two functions.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//private function are just functions that you can't access</span>
<span style="color: #808080; font-style: italic;">//from the main timeline, but only within the class itself</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<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>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrameEvents<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>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In order to access the main timeline, we have to type in <tt>MovieClip(root)</tt>. But, this is quite a bit of typing, so we can shorten it by putting it into a variable. Because I&#8217;m guessing that most of you have used ActionScript 2.0 before, I&#8217;m going to call this variable <tt>_root</tt>. Because this variable is non-existent in AS3, we can define it as anything. In this case, it actually will be the root of the document. Place this code after the class definition:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Brick <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//The main timeline!</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;
<span style="color: #808080; font-style: italic;">//etc etc etc</span></pre></div></div>

<p>Then, we can define the variable within the <tt>beginClass()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//defining _root as the document root</span>
<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, we can easily access the main timeline within the class. Go on, test it out. Make a trace statement or two.</p>
<p>Now, let&#8217;s make some hit testing, eh?</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrameEvents<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;">//hit testing with the ball</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">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;">//making the ball bounce off vertically</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">ballYSpeed</span> <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
		<span style="color: #808080; font-style: italic;">//destroying this brick</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//stop running this code</span>
		removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This will make the ball bounce off and destroy the brick every time it touches it. Pretty cool, right? Well, that&#8217;s all for this part of the tutorial. Next, we&#8217;re going to make more levels and add a win/lose system!</p>
<h4>Final Code for Main Flash File Frame 1:</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//IMPORTS</span>
<span style="color: #0066CC;">import</span> Brick;
<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;">int</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>Final Code for Main Flash File 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;">//VARIABLES</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;">8</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;">8</span>; <span style="color: #808080; font-style: italic;">//Y Speed of the Ball</span>
<span style="color: #808080; font-style: italic;">//First I defined a function where all of</span>
<span style="color: #808080; font-style: italic;">//the code needed to start the game is placed</span>
<span style="color: #808080; font-style: italic;">//This includes listeners, variable definitions, and other stuff</span>
<span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Adds a listener to the paddle which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcPaddle.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//Adds a listener to the ball which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcBall.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//making the level</span>
	makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> movePaddle<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;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #006600;">x</span> = mouseX - mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
	<span style="color: #808080; font-style: italic;">//Keeping the paddle in the stage</span>
&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>mouseX <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: #006600;">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>mouseX <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mcPaddle.<span style="color: #0066CC;">width</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> moveBall<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;">//Code for moving ball goes here</span>
	mcBall.<span style="color: #006600;">x</span> += ballXSpeed; <span style="color: #808080; font-style: italic;">//Move the ball horizontally</span>
	mcBall.<span style="color: #006600;">y</span> += ballYSpeed; <span style="color: #808080; font-style: italic;">//Move the ball vertically</span>
	<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: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">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: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</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: #006600;">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>
	<span style="color: #808080; font-style: italic;">//Hitting the paddle</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">hitTestObject</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>
		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: #006600;">x</span> - mcPaddle.<span style="color: #006600;">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>
&nbsp;
	<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;">int</span> = lvlArray<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;">int</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;">int</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: #000000; font-weight: bold;">var</span> brick:Brick = <span style="color: #000000; font-weight: bold;">new</span> Brick<span style="color: #66cc66;">&#40;</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>
			brick.<span style="color: #006600;">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>;
			brick.<span style="color: #006600;">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;">int</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: #808080; font-style: italic;">//finally, add the brick to stage</span>
			addChild<span style="color: #66cc66;">&#40;</span>brick<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h4>Final Code for Brick.as</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Classes must always be wrapped in a package</span>
package <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//importing display elements that we can use in this class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//defining the class name and saying that it</span>
	<span style="color: #808080; font-style: italic;">//extends the MovieClip class, meaning that it has the same</span>
	<span style="color: #808080; font-style: italic;">//properties as a movieclip</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Brick <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//The main timeline!</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:<span style="color: #0066CC;">MovieClip</span>;
		<span style="color: #808080; font-style: italic;">//all classes must have a function that runs every time</span>
		<span style="color: #808080; font-style: italic;">//an instance of the class is put on stage</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Brick<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;">//Code that will be run when the brick is added to the stage</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED</span>, beginClass<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Enter frame code</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//private function are just functions that you can't access</span>
		<span style="color: #808080; font-style: italic;">//from the main timeline, but only within the class itself</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> beginClass<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;">//defining _root as the document root</span>
			<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrameEvents<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;">//hit testing with the ball</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>.<span style="color: #006600;">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;">//making the ball bounce off vertically</span>
				<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">ballYSpeed</span> <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
				<span style="color: #808080; font-style: italic;">//destroying this brick</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #808080; font-style: italic;">//stop running this code</span>
				removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameEvents<span style="color: #66cc66;">&#41;</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></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 3</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-3/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-3/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:03:13 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=250</guid>
		<description><![CDATA[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. First of all, we need to look at how we&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<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 not to. 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://www.flashgametuts.com/obj/tuts/brick-breaker-as3/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://www.flashgametuts.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;">int</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>
&nbsp;
	<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;">int</span> = lvlArray<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;">int</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;">int</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: #000000; font-weight: bold;">var</span> brick:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> mcBrick<span style="color: #66cc66;">&#40;</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>
			brick.<span style="color: #006600;">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>;
			brick.<span style="color: #006600;">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;">int</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: #808080; font-style: italic;">//finally, add the brick to stage</span>
			addChild<span style="color: #66cc66;">&#40;</span>brick<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, just put <tt>makeLvl();</tt> into the <tt>beginCode()</tt> function and you&#8217;re set. The next step is getting the ball to break the bricks. But, that is for a later time. Here is the code you should have:</p>
<h4>First Frame:</h4>

<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;">int</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>

<h4>Game Frame:</h4>

<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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
</pre></td><td 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;">//VARIABLES</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;">8</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;">8</span>; <span style="color: #808080; font-style: italic;">//Y Speed of the Ball</span>
<span style="color: #808080; font-style: italic;">//First I defined a function where all of</span>
<span style="color: #808080; font-style: italic;">//the code needed to start the game is placed</span>
<span style="color: #808080; font-style: italic;">//This includes listeners, variable definitions, and other stuff</span>
<span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Adds a listener to the paddle which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcPaddle.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//Adds a listener to the ball which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcBall.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//making the level</span>
	makeLvl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> movePaddle<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;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #006600;">x</span> = mouseX - mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
	<span style="color: #808080; font-style: italic;">//Keeping the paddle in the stage</span>
&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>mouseX <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: #006600;">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>mouseX <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mcPaddle.<span style="color: #0066CC;">width</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> moveBall<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;">//Code for moving ball goes here</span>
	mcBall.<span style="color: #006600;">x</span> += ballXSpeed; <span style="color: #808080; font-style: italic;">//Move the ball horizontally</span>
	mcBall.<span style="color: #006600;">y</span> += ballYSpeed; <span style="color: #808080; font-style: italic;">//Move the ball vertically</span>
	<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: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">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: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</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: #006600;">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>
	<span style="color: #808080; font-style: italic;">//Hitting the paddle</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">hitTestObject</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>
		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: #006600;">x</span> - mcPaddle.<span style="color: #006600;">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>
&nbsp;
	<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;">int</span> = lvlArray<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;">int</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;">int</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: #000000; font-weight: bold;">var</span> brick:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> mcBrick<span style="color: #66cc66;">&#40;</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>
			brick.<span style="color: #006600;">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>;
			brick.<span style="color: #006600;">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;">int</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: #808080; font-style: italic;">//finally, add the brick to stage</span>
			addChild<span style="color: #66cc66;">&#40;</span>brick<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 2</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-2/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-2/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:02:06 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=227</guid>
		<description><![CDATA[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;. I&#8217;m also going to give my ball an instance name of mcBall. Now it&#8217;s time to create some code for our lil&#8217; ball. We&#8217;re going [...]]]></description>
			<content:encoded><![CDATA[<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://www.flashgametuts.com/obj/tuts/brick-breaker-as3/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 it&#8217;s time to create some code for our lil&#8217; ball. We&#8217;re going to do the same thing we did for the paddle and add an enter frame event listener to it in the <tt>beginCode()</tt> function.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Adds a listener to the ball which</span>
<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
mcBall.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Then, we&#8217;re going to create the function which moves the ball.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> moveBall<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;">//Code for moving ball goes here</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>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>moveBall()</tt> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcBall.<span style="color: #006600;">x</span> += ballXSpeed;
mcBall.<span style="color: #006600;">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>moveBall()</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: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">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: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</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: #006600;">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 us delve into this code.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//This code should be placed in the moveBall() function</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">hitTestObject</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>
	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: #006600;">x</span> - mcPaddle.<span style="color: #006600;">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. Here&#8217;s an example of what the final ActionScript should look 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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//VARIABLES</span>
<span style="color: #808080; font-style: italic;">//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;">8</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;">8</span>; <span style="color: #808080; font-style: italic;">//Y Speed of the Ball</span>
<span style="color: #808080; font-style: italic;">//First I defined a function where all of</span>
<span style="color: #808080; font-style: italic;">//the code needed to start the game is placed</span>
<span style="color: #808080; font-style: italic;">//This includes listeners, variable definitions, and other stuff</span>
<span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Adds a listener to the paddle which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcPaddle.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//Adds a listener to the ball which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcBall.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> movePaddle<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;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #006600;">x</span> = mouseX - mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
	<span style="color: #808080; font-style: italic;">//Keeping the paddle in the stage</span>
&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>mouseX <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: #006600;">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>mouseX <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mcPaddle.<span style="color: #0066CC;">width</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> moveBall<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;">//Code for moving ball goes here</span>
	mcBall.<span style="color: #006600;">x</span> += ballXSpeed; <span style="color: #808080; font-style: italic;">//Move the ball horizontally</span>
	mcBall.<span style="color: #006600;">y</span> += ballYSpeed; <span style="color: #808080; font-style: italic;">//Move the ball vertically</span>
	<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: #006600;">x</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">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: #006600;">y</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</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: #006600;">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>
	<span style="color: #808080; font-style: italic;">//Hitting the paddle</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mcBall.<span style="color: #006600;">hitTestObject</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>
		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: #006600;">x</span> - mcPaddle.<span style="color: #006600;">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;
beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS3 &#8211; Part 1</title>
		<link>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-1/</link>
		<comments>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-1/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:01:54 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=119</guid>
		<description><![CDATA[Ok, this is going to be my first tutorial on hopefully a simple subject. This is probably going to be broken up into a few parts. So, here we go. 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 [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, this is going to be my first tutorial on hopefully a simple subject. This is probably going to be broken up into a few parts. So, here we go.</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://www.flashgametuts.com/obj/tuts/brick-breaker-as3/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"><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: #808080; font-style: italic;">//First I defined a function where all of</span>
<span style="color: #808080; font-style: italic;">//the code needed to start the game is placed</span>
<span style="color: #808080; font-style: italic;">//This includes listeners, variable definitions, and other stuff</span>
<span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Adds a listener to the paddle which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcPaddle.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> movePaddle<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;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #006600;">x</span> = mouseX;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></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: #006600;">x</span> = mouseX;</pre></div></div>

<p>with</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mcPaddle.<span style="color: #006600;">x</span> = mouseX - mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;</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 movePaddle 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>mouseX <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: #006600;">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>mouseX <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//First I defined a function where all of</span>
<span style="color: #808080; font-style: italic;">//the code needed to start the game is placed</span>
<span style="color: #808080; font-style: italic;">//This includes listeners, variable definitions, and other stuff</span>
<span style="color: #000000; font-weight: bold;">function</span> beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Adds a listener to the paddle which</span>
	<span style="color: #808080; font-style: italic;">//runs a function every time a frame passes</span>
	mcPaddle.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, movePaddle<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> movePaddle<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;">//The paddle follows the mouse</span>
	mcPaddle.<span style="color: #006600;">x</span> = mouseX - mcPaddle.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
	<span style="color: #808080; font-style: italic;">//Keeping the paddle in the stage</span>
&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>mouseX <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: #006600;">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>mouseX <span style="color: #66cc66;">&gt;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</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: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mcPaddle.<span style="color: #0066CC;">width</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
beginCode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS2 &#8211; Part 6</title>
		<link>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-6/</link>
		<comments>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-6/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 12:05:47 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1073</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<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://www.flashgametuts.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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS2 &#8211; Part 5</title>
		<link>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-5/</link>
		<comments>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-5/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 12:04:46 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1071</guid>
		<description><![CDATA[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. We&#8217;re first going [...]]]></description>
			<content:encoded><![CDATA[<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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS2 &#8211; Part 4</title>
		<link>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-4/</link>
		<comments>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-4/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 12:03:45 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1069</guid>
		<description><![CDATA[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 makeLvl() function: if&#40;lvlArray&#91;currentLvl-1&#93;&#91;i&#93; == 1&#41;&#123; //creating a variable which holds the brick instance _root.attachMovie&#40;'mcBrick', 'brick'+i,_root.getNextHighestDepth&#40;&#41;&#41;; //setting the brick's coordinates via the [...]]]></description>
			<content:encoded><![CDATA[<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>

]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create a Brick Breaker Game in AS2 &#8211; Part 3</title>
		<link>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-3/</link>
		<comments>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-3/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 12:02:44 +0000</pubDate>
		<dc:creator>Mr Sun</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Beginner]]></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[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.mrsunstudios.com/?p=1062</guid>
		<description><![CDATA[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. First of all, we need to look at how we&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<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://www.flashgametuts.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://www.flashgametuts.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>
]]></content:encoded>
			<wfw:commentRss>http://www.flashgametuts.com/tutorials/as2/how-to-create-a-brick-breaker-game-in-as2-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
