{"id":119,"date":"2008-07-29T09:44:54","date_gmt":"2008-07-29T13:44:54","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=119"},"modified":"2022-05-29T08:23:39","modified_gmt":"2022-05-29T12:23:39","slug":"tutorial-create-a-brick-breaker-game-in-as3","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-brick-breaker-game-in-as3\/","title":{"rendered":"Tutorial: Create a Brick Breaker Game in AS3"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li class=\"c_chap\"><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><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 1: Coding Paddle Movement<\/h3>\n<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>\n<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 \/>\n<img loading=\"lazy\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt1\/paddle.gif\" width=\"266\" height=\"163\"\/><br \/>\nMine is 55&#215;10<\/p>\n<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>\n<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>\n<pre lang=\"actionscript\" line=\"1\">\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}\r\n\r\nfunction movePaddle(event:Event):void{\r\n\t\/\/The paddle follows the mouse\r\n\tmcPaddle.x = mouseX;\r\n}\r\n\r\nbeginCode();\r\n<\/pre>\n<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>\n<pre lang=\"actionscript\">\r\nmcPaddle.x = mouseX;\r\n<\/pre>\n<p>with<\/p>\n<pre lang=\"actionscript\">\r\nmcPaddle.x = mouseX - mcPaddle.width \/ 2;\r\n<\/pre>\n<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>\n<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>\n<pre lang=\"actionscript\">\r\n\/\/If the mouse goes off too far to the left\r\nif(mouseX < mcPaddle.width \/ 2){\r\n\t\/\/Keep the paddle on stage\r\n\tmcPaddle.x = 0;\r\n}\r\n\/\/If the mouse goes off too far to the right\r\nif(mouseX > stage.stageWidth - mcPaddle.width \/ 2){\r\n\t\/\/Keep the paddle on stage\r\n\tmcPaddle.x = stage.stageWidth - mcPaddle.width;\r\n}\r\n<\/pre>\n<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>\n<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>\n<pre lang=\"actionscript\" line=\"1\">\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}\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\nbeginCode();\r\n<\/pre>\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\/pt1\/brick-breaker-as3-pt1.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt1\/brick-breaker-as3-pt1.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/brick-breaker-as3\/pt1\/brick-breaker-as3-pt1.fla\">Source .fla File<\/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 1: Coding Paddle Movement 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, [&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\/119"}],"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=119"}],"version-history":[{"count":25,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":366,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/119\/revisions\/366"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}