{"id":1020,"date":"2009-01-03T08:00:52","date_gmt":"2009-01-03T12:00:52","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1020"},"modified":"2022-05-29T08:23:33","modified_gmt":"2022-05-29T12:23:33","slug":"tutorial-create-a-platform-game-in-as2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-platform-game-in-as2\/","title":{"rendered":"Tutorial: Create a Platform Game in AS2"},"content":{"rendered":"<div class=\"toc\">\n<h3 id=\"1020_table-of-contents_1\" >Table of Contents<\/h3>\n<ol>\n<li class=\"c_chap\"><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-platform-game-in-as2\">Basic Character Programming<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-platform-game-in-as2-part-2\">Creating The Level<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-platform-game-in-as2-part-3\">Programming the Level<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-platform-game-in-as2-part-4\">Adding Level Elements<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-platform-game-in-as2-part-5\">Adding Enemies<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-platform-game-in-as2-part-6\">Goals and Level Completion<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-create-a-platform-game-in-as2-part-7\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<p>Due to popular request, I&#8217;ve decided to make an AS2 version of my tutorial. For consistency, it will follow the same format as the <a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3\">AS3 version<\/a> and will also use excerpts of the same text. Here we go.<\/p>\n<h3 id=\"1020_step-1-basic-charact_1\" >Step 1: Basic Character Programming<\/h3>\n<p>Let&#8217;s first begin with the basic setup of our game. I&#8217;m going to make the background black and the frame rate 24 fps (like in previous tutorials). The first thing to make is our little character. I won&#8217;t be too artistic with this one, just a little white circle without any outline.<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt1\/main.gif\" alt=\"My character guy\" \/><br \/>\nIts dimensions are 25&#215;25 pixels.<\/p>\n<p>Next, convert it into a MovieClip and give it an instance name of <tt>mcMain<\/tt>. Now, we can add code to move this character.<\/p>\n<p>Just create a new &#8220;actions&#8221; layer and place this code in the first frame (<strong>DON&#8217;T ADD IT TO THE MOVIECLIP<\/strong>):<\/p>\n<pre lang=\"actionscript\">\r\nvar mainSpeed:Number = 7; \/\/The speed of the character\r\nonEnterFrame = function(){ \/\/this function will run every frame (needed for moving the character\r\n\tif(Key.isDown(37) || Key.isDown(65)){ \/\/if the \"A\" key or Left Arrow Key is Down\r\n\t\tmcMain._x -= mainSpeed;\/\/then the move the guy left\r\n\t} else if(Key.isDown(39) || Key.isDown(68)){\/\/if the \"D\" key or Right Arrow Key is Down\r\n\t\tmcMain._x += mainSpeed; \/\/then move the guy to the right\r\n\t}\r\n}\r\n<\/pre>\n<p>This code will simply make the character move left and right. Now, we have to make the character jump. We&#8217;ll accomplish this with a jump function and running it whenever the up key is down. First, we have to define a few variables at the top of the code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/JUMPING VARIABLES\r\n\/\/whether or not the main guy is jumping\r\nvar mainJumping:Boolean = false;\r\n\/\/how quickly should the jump start off\r\nvar jumpSpeedLimit:Number = 15;\r\n\/\/the current speed of the jump;\r\nvar jumpSpeed:Number = jumpSpeedLimit;\r\n<\/pre>\n<p>Then, we&#8217;ll define a function which will make the guy jump. It&#8217;ll take some moderately complicated math. Add this code to the bottom of your code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/THE JUMPING FUNCTION\r\nfunction mainJump():Void{\r\n\t\/\/if main isn't already jumping\r\n\tif(!mainJumping){\r\n\t\tif(Key.isDown(38) || Key.isDown(87)){\r\n\t\t\t\/\/then start jumping\r\n\t\t\tmainJumping = true;\r\n\t\t\tjumpSpeed = jumpSpeedLimit*-1;\r\n\t\t\tmcMain._y += jumpSpeed;\r\n\t\t}\r\n\t} else {\r\n\t\t\/\/if we're already jumping, then continue to do so\r\n\t\tif(jumpSpeed < 0){\r\n\t\t\tjumpSpeed *= 1 - jumpSpeedLimit\/75;\r\n\t\t\tif(jumpSpeed > -jumpSpeedLimit*.2){\r\n\t\t\t\tjumpSpeed *= -1;\r\n\t\t\t}\r\n\t\t}\r\n\t\tif(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){\r\n\t\t\tjumpSpeed *= 1 + jumpSpeedLimit\/50;\r\n\t\t}\r\n\t\tmcMain._y += jumpSpeed;\r\n\t\t\/\/if main hits the floor, then stop jumping\r\n\t\t\/\/we'll have to change this however when we create the blocks in the level\r\n\t\tif(mcMain._y >= Stage.height - mcMain._height){\r\n\t\t\tmainJumping = false;\r\n\t\t\tmcMain._y = Stage.height - mcMain._height;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Pretty intense, eh? The last thing we need to do in order to make the jumping work is to add the <tt>mainJump()<\/tt> function to the <tt>enterFrame<\/tt>. This is would we should change it to:<\/p>\n<pre lang=\"actionscript\">\r\nonEnterFrame = function(){ \/\/this function will run every frame (needed for moving the character\r\n\tif(Key.isDown(37) || Key.isDown(65)){ \/\/if the \"A\" key or Left Arrow Key is Down\r\n\t\tmcMain._x -= mainSpeed;\/\/then the move the guy left\r\n\t} else if(Key.isDown(39) || Key.isDown(68)){\/\/if the \"D\" key or Right Arrow Key is Down\r\n\t\tmcMain._x += mainSpeed; \/\/then move the guy to the right\r\n\t}\r\n\tmainJump();\r\n}\r\n<\/pre>\n<p>Congratulations! We&#8217;ve just made the character move and jump! Next time, we&#8217;ll create a level with blocks we can walk on!<\/p>\n<h4 id=\"1020_final-product_1\" >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:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt1\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt1\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt1\/source.fla\">Source .fla File<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents Basic Character Programming Creating The Level Programming the Level Adding Level Elements Adding Enemies Goals and Level Completion Finishing Touches Due to popular request, I&#8217;ve decided to make an AS2 version of my tutorial. For consistency, it will follow the same format as the AS3 version and will also use excerpts of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[160,5,7,4,6],"tags":[25,160,7,162,19,18,161,22,11],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1020"}],"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=1020"}],"version-history":[{"count":6,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1020\/revisions"}],"predecessor-version":[{"id":1188,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1020\/revisions\/1188"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1020"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}