{"id":260,"date":"2008-07-31T10:33:17","date_gmt":"2008-07-31T14:33:17","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=260"},"modified":"2022-05-29T08:23:38","modified_gmt":"2022-05-29T12:23:38","slug":"tutorial-create-a-brick-breaker-game-in-as3-part-4","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-brick-breaker-game-in-as3-part-4\/","title":{"rendered":"Tutorial: Create a Brick Breaker Game in AS3 &#8211; Part 4"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/07\/tutorial-create-a-brick-breaker-game-in-as3\">Coding Paddle Movement<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/07\/tutorial-create-a-brick-breaker-game-in-as3-part-2\">Programming the Ball<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/07\/tutorial-create-a-brick-breaker-game-in-as3-part-3\">Setting Up the Bricks on Stage<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/www.mrsunstudios.com\/2008\/07\/tutorial-create-a-brick-breaker-game-in-as3-part-4\">Hit Testing the Bricks<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/07\/tutorial-create-a-brick-breaker-game-in-as3-part-5\">Creating Levels<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-brick-breaker-game-in-as3-part-6\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<h3>Step 4: Hit Testing the Bricks<\/h3>\n<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>\n<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 \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt4\/new-actionscript.gif\" alt=\"Create a new ActionScript file\" \/><\/p>\n<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>\n<pre lang=\"actionscript\" line=\"1\">\r\n\/\/Classes must always be wrapped in a package\r\npackage {\r\n\t\/\/importing display elements that we can use in this class\r\n\timport flash.display.*;\r\n\t\/\/importing flash events that we can use (like ENTER_FRAME)\r\n\timport flash.events.*;\r\n\t\r\n\t\/\/defining the class name and saying that it\r\n\t\/\/extends the MovieClip class, meaning that it has the same\r\n\t\/\/properties as a movieclip\r\n\tpublic class Brick extends MovieClip {\r\n\t\t\/\/all classes must have a function that runs every time\r\n\t\t\/\/an instance of the class is put on stage\r\n\t\tpublic function Brick(){\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<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>\n<pre lang=\"actionscript\" line=\"1\">\r\n\/\/IMPORTS\r\nimport Brick;\r\n<\/pre>\n<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>\n<p>So, right click the mcBrick MovieClip in your library and click on linkage. This window will pop up.<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt4\/linkage-window.gif\" alt=\"The Linkage Window\" \/><\/p>\n<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>\n<pre lang=\"actionscript\">\r\nvar brick:MovieClip = new mcBrick();\r\n<\/pre>\n<p>to:<\/p>\n<pre lang=\"actionscript\">\r\nvar brick:Brick = new Brick();\r\n<\/pre>\n<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>\n<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>\n<pre lang=\"actionscript\">\r\npublic function Brick(){\r\n\t\/\/Code that will be run when the brick is added to the stage\r\n\taddEventListener(Event.ADDED, beginClass);\r\n\t\/\/Enter frame code\r\n\taddEventListener(Event.ENTER_FRAME, enterFrameEvents);\r\n}\r\n<\/pre>\n<p>Next, we define those two functions.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/private function are just functions that you can't access\r\n\/\/from the main timeline, but only within the class itself\r\nprivate function beginClass(event:Event):void{\r\n\t\r\n}\r\nprivate function enterFrameEvents(event:Event):void{\r\n\t\r\n}\r\n<\/pre>\n<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>\n<pre lang=\"actionscript\">\r\npublic class Brick extends MovieClip {\r\n\t\/\/The main timeline!\r\n\tprivate var _root:MovieClip;\r\n\/\/etc etc etc\r\n<\/pre>\n<p>Then, we can define the variable within the <tt>beginClass()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/defining _root as the document root\r\n_root = MovieClip(root);\r\n<\/pre>\n<p>Now, we can easily access the main timeline within the class. Go on, test it out. Make a trace statement or two.<\/p>\n<p>Now, let&#8217;s make some hit testing, eh?<\/p>\n<pre lang=\"actionscript\">\r\nprivate function enterFrameEvents(event:Event):void{\r\n\t\/\/hit testing with the ball\r\n\tif(this.hitTestObject(_root.mcBall)){\r\n\t\t\/\/making the ball bounce off vertically\r\n\t\t_root.ballYSpeed *= -1;\r\n\t\t\/\/destroying this brick\r\n\t\tthis.parent.removeChild(this);\r\n\t\t\/\/stop running this code\r\n\t\tremoveEventListener(Event.ENTER_FRAME, enterFrameEvents);\r\n\t}\r\n}\r\n<\/pre>\n<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>\n<h4>Final Code for Main Flash File Frame 1:<\/h4>\n<pre lang=\"actionscript\">\r\n\/\/IMPORTS\r\nimport Brick;\r\n\/\/Current level player is on\r\nvar currentLvl:int = 1;\r\n\/\/The array code for lvl 1\r\nvar lvl1Code:Array = new Array(1,1,1,1,1,1,1);\r\n\/\/The array that contains all of the level codes\r\nvar lvlArray:Array = new Array(lvl1Code);\r\n<\/pre>\n<h4>Final Code for Main Flash File Frame 2:<\/h4>\n<pre lang=\"actionscript\">\r\nstop();\r\n\/\/VARIABLES\r\n\/\/These variables are needed for moving the ball\r\nvar ballXSpeed:Number = 8; \/\/X Speed of the Ball\r\nvar ballYSpeed:Number = 8; \/\/Y Speed of the Ball\r\n\/\/First I defined a function where all of\r\n\/\/the code needed to start the game is placed\r\n\/\/This includes listeners, variable definitions, and other stuff\r\nfunction beginCode():void{\r\n\t\/\/Adds a listener to the paddle which \r\n\t\/\/runs a function every time a frame passes\r\n\tmcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);\r\n\t\/\/Adds a listener to the ball which\r\n\t\/\/runs a function every time a frame passes\r\n\tmcBall.addEventListener(Event.ENTER_FRAME, moveBall);\r\n\t\/\/making the level\r\n\tmakeLvl();\r\n}\r\n\r\nfunction movePaddle(event:Event):void{\r\n\t\/\/The paddle follows the mouse\r\n\tmcPaddle.x = mouseX - mcPaddle.width \/ 2;\r\n\t\/\/Keeping the paddle in the stage\r\n\t\r\n\t\/\/If the mouse goes off too far to the left\r\n\tif(mouseX < mcPaddle.width \/ 2){\r\n\t\t\/\/Keep the paddle on stage\r\n\t\tmcPaddle.x = 0;\r\n\t}\r\n\t\/\/If the mouse goes off too far to the right\r\n\tif(mouseX > stage.stageWidth - mcPaddle.width \/ 2){\r\n\t\t\/\/Keep the paddle on stage\r\n\t\tmcPaddle.x = stage.stageWidth - mcPaddle.width;\r\n\t}\r\n}\r\n\r\nfunction moveBall(event:Event):void{\r\n\t\/\/Code for moving ball goes here\r\n\tmcBall.x += ballXSpeed; \/\/Move the ball horizontally\r\n\tmcBall.y += ballYSpeed; \/\/Move the ball vertically\r\n\t\/\/Bouncing the ball off of the walls\r\n\tif(mcBall.x >= stage.stageWidth-mcBall.width){\r\n\t\t\/\/if the ball hits the right side\r\n\t\t\/\/of the screen, then bounce off\r\n\t\tballXSpeed *= -1;\r\n\t}\r\n\tif(mcBall.x <= 0){\r\n\t\t\/\/if the ball hits the left side\r\n\t\t\/\/of the screen, then bounce off\r\n\t\tballXSpeed *= -1;\r\n\t}\r\n\tif(mcBall.y >= stage.stageHeight-mcBall.height){\r\n\t\t\/\/if the ball hits the bottom\r\n\t\t\/\/then bounce up\r\n\t\tballYSpeed *= -1;\r\n\t}\r\n\tif(mcBall.y <= 0){\r\n\t\t\/\/if the ball hits the top\r\n\t\t\/\/then bounce down\r\n\t\tballYSpeed *= -1;\r\n\t}\r\n\t\/\/Hitting the paddle\r\n\tif(mcBall.hitTestObject(mcPaddle)){\r\n\t\tcalcBallAngle();\r\n\t}\r\n}\r\n\r\nfunction calcBallAngle():void{\r\n\t\/\/ballPosition is the position of the ball is on the paddle\r\n\tvar ballPosition:Number = mcBall.x - mcPaddle.x;\r\n\t\/\/hitPercent converts ballPosition into a percent\r\n\t\/\/All the way to the left is -.5\r\n\t\/\/All the way to the right is .5\r\n\t\/\/The center is 0\r\n\tvar hitPercent:Number = (ballPosition \/ (mcPaddle.width - mcBall.width)) - .5;\r\n\t\/\/Gets the hitPercent and makes it a larger number so the\r\n\t\/\/ball actually bounces\r\n\tballXSpeed = hitPercent * 10;\r\n\t\/\/Making the ball bounce back up\r\n\tballYSpeed *= -1;\r\n}\r\n\r\nfunction makeLvl():void{ \/\/Places bricks onto Level\r\n\t\/\/finding the array length of the lvl code\r\n\t\r\n\t\/\/The index has to be currentLvl-1 because:\r\n\t\/\/array indexes start on 0 and our lvl starts at 1\r\n\t\/\/our level will always be 1 higher than the actual index of the array\r\n\tvar arrayLength:int = lvlArray[currentLvl-1].length;\r\n\t\/\/the current row of bricks we are creating\r\n\tvar brickRow:int = 0;\r\n\t\/\/Now, creating a loop which places the bricks onto the stage\r\n\tfor(var i:int = 0;i<arrayLength;i++){\r\n\t\t\/\/checking if it should place a brick there\r\n\t\tif(lvlArray[currentLvl-1][i] == 1){\r\n\t\t\t\/\/creating a variable which holds the brick instance\r\n\t\t\tvar brick:Brick = new Brick();\r\n\t\t\t\/\/setting the brick's coordinates via the i variable and brickRow\r\n\t\t\tbrick.x = 15+(i-brickRow*7)*75;\r\n\t\t\tbrick.y = 10+brickRow*20;\r\n\t\t\t\/\/checks if the current brick needs a new row\r\n\t\t\tfor(var c:int = 1;c<=10;c++){\r\n\t\t\t\tif(i == c*7-1){\r\n\t\t\t\t\tbrickRow ++;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\/\/finally, add the brick to stage\r\n\t\t\taddChild(brick);\r\n\t\t}\r\n\t}\r\n}\r\n\r\nbeginCode();\r\n<\/pre>\n<h4>Final Code for Brick.as<\/h4>\n<pre lang=\"actionscript\">\r\n\/\/Classes must always be wrapped in a package\r\npackage {\r\n\t\/\/importing display elements that we can use in this class\r\n\timport flash.display.*;\r\n\timport flash.events.*;\r\n\t\r\n\t\/\/defining the class name and saying that it\r\n\t\/\/extends the MovieClip class, meaning that it has the same\r\n\t\/\/properties as a movieclip\r\n\tpublic class Brick extends MovieClip {\r\n\t\t\/\/The main timeline!\r\n\t\tprivate var _root:MovieClip;\r\n\t\t\/\/all classes must have a function that runs every time\r\n\t\t\/\/an instance of the class is put on stage\r\n\t\tpublic function Brick(){\r\n\t\t\t\/\/Code that will be run when the brick is added to the stage\r\n\t\t\taddEventListener(Event.ADDED, beginClass);\r\n\t\t\t\/\/Enter frame code\r\n\t\t\taddEventListener(Event.ENTER_FRAME, enterFrameEvents);\r\n\t\t}\r\n\t\t\/\/private function are just functions that you can't access\r\n\t\t\/\/from the main timeline, but only within the class itself\r\n\t\tprivate function beginClass(event:Event):void{\r\n\t\t\t\/\/defining _root as the document root\r\n\t\t\t_root = MovieClip(root);\r\n\t\t}\r\n\t\tprivate function enterFrameEvents(event:Event):void{\r\n\t\t\t\/\/hit testing with the ball\r\n\t\t\tif(this.hitTestObject(_root.mcBall)){\r\n\t\t\t\t\/\/making the ball bounce off vertically\r\n\t\t\t\t_root.ballYSpeed *= -1;\r\n\t\t\t\t\/\/destroying this brick\r\n\t\t\t\tthis.parent.removeChild(this);\r\n\t\t\t\t\/\/stop running this code\r\n\t\t\t\tremoveEventListener(Event.ENTER_FRAME, enterFrameEvents);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<h4>The Final Product:<\/h4>\n<p><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"550\" height=\"400\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt4\/brick-breaker-as3-pt4.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt4\/brick-breaker-as3-pt4.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt4\/source.zip\">Source Files (Zipped)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents Coding Paddle Movement Programming the Ball Setting Up the Bricks on Stage Hit Testing the Bricks Creating Levels Finishing Touches Step 4: Hit Testing the Bricks Now that we&#8217;ve actually made the bricks, we can now break them with our ball&#8230; Anyway, this will be pretty easy to accomplish. But, if you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,8,65,4,6],"tags":[25,8,14,66,26,67,19,18,22],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/260"}],"collection":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":11,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"predecessor-version":[{"id":369,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions\/369"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}