{"id":1052,"date":"2009-01-10T08:00:40","date_gmt":"2009-01-10T12:00:40","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1052"},"modified":"2022-05-29T08:23:31","modified_gmt":"2022-05-29T12:23:31","slug":"tutorial-create-a-brick-breaker-game-in-as2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-brick-breaker-game-in-as2\/","title":{"rendered":"Tutorial: Create a Brick Breaker Game in AS2"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li class=\"c_chap\"><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-brick-breaker-game-in-as2\">Coding Paddle Movement<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-brick-breaker-game-in-as2-part-2\">Programming the Ball<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-brick-breaker-game-in-as2-part-3\">Setting Up the Bricks on Stage<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-brick-breaker-game-in-as2-part-4\">Hit Testing the Bricks<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-brick-breaker-game-in-as2-part-5\">Creating Levels<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-brick-breaker-game-in-as2-part-6\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<h3>Step 1: Coding Paddle Movement<\/h3>\n<p>Alright, I&#8217;ve decided to make a tutorial for you guys on how to create a brick breaker game in ActionScript 2.0. It&#8217;ll be very similar to my <a href=\"http:\/\/www.mrsunstudios.com\/2008\/07\/tutorial-create-a-brick-breaker-game-in-as3\/\">one for AS3<\/a>, except coded differently. So, here we go, eh?<\/p>\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-as2\/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\">\r\nonEnterFrame = function(){ \/\/this function will run during every single frame\r\n\t\/\/The paddle follows the mouse\r\n\tmcPaddle._x = _xmouse;\r\n}\r\n\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 = _xmouse;\r\n<\/pre>\n<p>with:<\/p>\n<pre lang=\"actionscript\">\r\nmcPaddle._x = _xmouse - mcPaddle._width*.5;\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 <tt>onEnterFrame<\/tt> function.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/If the mouse goes off too far to the left\r\nif(_xmouse < 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(_xmouse > Stage.width - mcPaddle._width \/ 2){\r\n\t\/\/Keep the paddle on stage\r\n\tmcPaddle._x = Stage.width - 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\nonEnterFrame = function(){ \/\/this function will run during every single frame\r\n\t\/\/The paddle follows the mouse\r\n\tmcPaddle._x = _xmouse - mcPaddle._width*.5;\r\n\t\r\n\t\/\/If the mouse goes off too far to the left\r\n\tif(_xmouse < 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(_xmouse > Stage.width - mcPaddle._width \/ 2){\r\n\t\t\/\/Keep the paddle on stage\r\n\t\tmcPaddle._x = Stage.width - mcPaddle._width;\r\n\t}\r\n}\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-as2\/pt1\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/brick-breaker-as2\/pt1\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/brick-breaker-as2\/pt1\/source.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 Alright, I&#8217;ve decided to make a tutorial for you guys on how to create a brick breaker game in ActionScript 2.0. It&#8217;ll be very similar to my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,7,65,4,6],"tags":[25,7,14,66,26,67,19,18,22],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1052"}],"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=1052"}],"version-history":[{"count":8,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1052\/revisions"}],"predecessor-version":[{"id":1349,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1052\/revisions\/1349"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1052"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}