{"id":1031,"date":"2009-01-03T08:02:45","date_gmt":"2009-01-03T12:02:45","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1031"},"modified":"2022-05-29T08:23:33","modified_gmt":"2022-05-29T12:23:33","slug":"tutorial-create-a-platform-game-in-as2-part-3","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-platform-game-in-as2-part-3\/","title":{"rendered":"Tutorial: Create a Platform Game in AS2 &#8211; Part 3"},"content":{"rendered":"<div class=\"toc\">\n<h3 id=\"1020_table-of-contents_1\" >Table of Contents<\/h3>\n<ol>\n<li><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 class=\"c_chap\"><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<h3 id=\"1031_step-3-programming-t_1\" >Step 3: Programming the Level<\/h3>\n<p>Now that we&#8217;ve set up all of the blocks onto the stage, we can actually add some code to them. The first thing we&#8217;re going to do is to make it so the blocks are the only thing that the main guy can stand on. Replace this code in the <tt>mainJump()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/if main hits the floor, then stop jumping\r\n\/\/we'll have to change this however when we create the blocks in the level\r\nif(mcMain._y >= Stage.height - mcMain._height){\r\n\tmainJumping = false;\r\n\tmcMain._y = Stage.height - mcMain._height;\r\n}\r\n<\/pre>\n<p>with this:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/if main hits a block, then stop jumping\r\n\/\/this loop will check a hit test with any block\r\nfor(var cBlock:String in blockHolder){\r\n\t\/\/cBlock is simply the name of the chosen block\r\n\t\/\/we can use this to access and do what we want with it\r\n\tif(mcMain.hitTest(blockHolder[cBlock]) && mcMain._y <= hitBlock._y){\r\n\t\tmainOnGround = true;\r\n\t\t\/\/stop jumping\r\n\t\tmainJumping = false;\r\n\t\t\/\/and set the guys coordinates to be on top of the block\r\n\t\tmcMain._y = blockHolder[cBlock]._y - mcMain._height;\r\n\t\t\/\/break out of the loop to save processor power\r\n\t\tbreak;\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, we have to make the character fall when he isn't on a block. First, define a variable, <tt>mainOnGround<\/tt> at the top, and set it as a false boolean. Then, set it to true if <tt>mcMain<\/tt> hits a block in the <tt>mainJump()<\/tt> function. Hopefully, I don't have to walk you through this one. Next, add the following code to the main <tt>onEnterFrame()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/checking if he isn't on the ground\r\n\/\/run a loop checking if he's touching blocks\r\nfor(var cBlock:String in blockHolder){\r\n\t\/\/checking hit test and if main is above the brick\r\n\tif(mcMain.hitTest(blockHolder[cBlock]) && mcMain._y < blockHolder[cBlock]._y){\r\n\t\t\/\/this time, we just make mainOnGround true\r\n\t\tmainOnGround = true;\r\n\t\t\/\/and break from the loop\r\n\t\tbreak;\r\n\t}\r\n\tmainOnGround = false;\r\n}\r\n\/\/then we make him fall if he isn't on the ground\r\nif(!mainOnGround){\r\n\tmainJumping = true;\r\n}\r\n<\/pre>\n<p>The next thing we're going to do is make the guy bounce off of the bricks if he hits them below. Otherwise, he'd somehow get on top of the bricks from below, which we don't want. To do this, we're going to have to change the <tt>for<\/tt> loop in the <tt>mainJump()<\/tt> function. Here's what you should replace it with:<\/p>\n<pre lang=\"actionscript\">\r\nfor(var cBlock:String in blockHolder){\r\n\t\/\/cBlock is simply the name of the chosen block\r\n\t\/\/we can use this to access and do what we want with it\r\n\tif(mcMain.hitTest(blockHolder[cBlock])){\r\n\t\t\/\/if main is falling down\r\n\t\tif(jumpSpeed > 0){\r\n\t\t\t\/\/stop jumping\r\n\t\t\tmainJumping = false\r\n\t\t\t\/\/and set the guy's coordinates to be on the top of the block\r\n\t\t\tmcMain._y = blockHolder[cBlock]._y - 25;\r\n\t\t\t\/\/he's now on solid ground\r\n\t\t\tmainOnGround = true;\r\n\t\t\t\/\/break out of the loop to save processing power\r\n\t\t\tbreak;\r\n\t\t} else {\r\n\t\t\tjumpSpeed = Math.abs(jumpSpeed);\r\n\t\t\t\/\/making the main guy get away from the block\r\n\t\t\tmcMain._y = blockHolder[cBlock]._y + blockHolder[cBlock]._height + 1;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, if you test it out, the guy bounces when hitting the block. Now, the last thing we're going to do is make the background move with the character if he moves too far to the left or right. In order to do this, I'm going to change around <tt>lvlArray1<\/tt> so the changes are more necessary. Here's what I'll change it to:<\/p>\n<pre lang=\"actionscript\">\r\nvar lvlArray1:Array = new Array(\r\n\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\r\n\t0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\r\n);\r\n<\/pre>\n<p>Now, we have to edit the <tt>createLvl<\/tt> function first it centers the main character and actually makes this code go to work. Replace the current function with this code:<\/p>\n<pre lang=\"actionscript\">\r\nfunction createLvl():Void{\r\n\t\/\/finding the array of the current level we're on\r\n\t\/\/this is just a way to dynamically select an Array within the document\r\n\tvar lvlArray:Array = _root['lvlArray'+lvlCurrent];\r\n\t\/\/we have to find how far this level will span\r\n\t\/\/this will be used so we know when to move to the next row\r\n\t\/\/there will always be 16 rows, so this is how we find it out\r\n\t\/\/of course, this will make the level formatting very strict\r\n\tvar lvlColumns:Number = Math.ceil(lvlArray.length\/16);\r\n\t\/\/now we can create the level\r\n\tfor(var i:Number=0;i<lvlArray.length;i++){\r\n\t\tif(i\/lvlColumns == int(i\/lvlColumns)){\r\n\t\t\trow ++; \/\/moving onto the next row once we're done with a column\r\n\t\t}\r\n\t\t\r\n\t\tif(lvlArray[i] == 1){\r\n\t\t\t\/\/making a new block and adding it to stage\r\n\t\t\tblockHolder.attachMovie('mcBlock', 'Block'+i,blockHolder.getNextHighestDepth());\r\n\t\t\t\/\/modifying this guy's coordinates\r\n\t\t\tblockHolder['Block'+i]._x = (i-(row-1)*lvlColumns)*25;\r\n\t\t\tblockHolder['Block'+i]._y = (row-1)*25;\r\n\t\t} else if (lvlArray[i] == 'MAIN'){\r\n\t\t\t\/\/changing main's coordinates if we spot him\r\n\t\t\tmcMain._x = (i-(row-1)*lvlColumns)*25;\r\n\t\t\tmcMain._y = (row-1)*25;\r\n\t\t}\r\n\t}\r\n\t\/\/reset the row for later use\r\n\trow = 0;\r\n\t\/\/then we center the screen on the main character\r\n\t\/\/this variable will tell us how far away we have to move everything\r\n\t\/\/250 is about centered for mcMain we'll use that number\r\n\tvar mainMove:Number = 250 - mcMain._x;\r\n\t\/\/then move mcMain and the entire level to center on him\r\n\tmcMain._x += mainMove;\r\n\tblockHolder._x += mainMove;\r\n}\r\n<\/pre>\n<p>This code makes me very happy. The screen will now center on the main character in every level, no matter where you place him. Next, we want the actual background to move instead of the character when he moves so we can keep track of him at all times. This'll be easy. In the main <tt>onEnterFrame()<\/tt> function, replace:<\/p>\n<pre lang=\"actionscript\">\r\nif(Key.isDown(37) || Key.isDown(65)){ \/\/if the \"A\" key or Left Arrow Key is Down\r\n\tmcMain._x -= mainSpeed;\/\/then the move the guy left\r\n} else if(Key.isDown(39) || Key.isDown(68)){\/\/if the \"D\" key or Right Arrow Key is Down\r\n\tmcMain._x += mainSpeed; \/\/then move the guy to the right\r\n}\r\n<\/pre>\n<p>with:<\/p>\n<pre lang=\"actionscript\">\r\nif(Key.isDown(37) || Key.isDown(65)){ \/\/if the \"A\" key or Left Arrow Key is Down\r\n\tblockHolder._x += mainSpeed;\/\/then the move the guy left\r\n} else if(Key.isDown(39) || Key.isDown(68)){\/\/if the \"D\" key or Right Arrow Key is Down\r\n\tblockHolder._x -= mainSpeed; \/\/then move the guy to the right\r\n}\r\n<\/pre>\n<p>Pretty easy, eh? Now there's only one problem I want to fix before I end this lesson. The character doesn't really jump high enough. So, we're just going to recode this part a bit:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/if we're already jumping, then continue to do so\r\nif(jumpSpeed < 0){\r\n\tjumpSpeed *= 1 - jumpSpeedLimit\/75;\r\n\tif(jumpSpeed > -jumpSpeedLimit*.2){\r\n\t\tjumpSpeed *= -1;\r\n\t}\r\n}\r\nif(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){\r\n\tjumpSpeed *= 1 + jumpSpeedLimit\/50;\r\n}\r\nmcMain._y += jumpSpeed;\r\n<\/pre>\n<p>Replace it with this:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/if we're already jumping, then continue to do so\r\nif(jumpSpeed < 0){\r\n\tjumpSpeed *= 1 - jumpSpeedLimit\/125;\r\n\tif(jumpSpeed > -jumpSpeedLimit*.2){\r\n\t\tjumpSpeed *= -1;\r\n\t\t}\r\n\t}\r\nif(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){\r\n\tjumpSpeed *= 1 + jumpSpeedLimit\/50;\r\n}\r\nmcMain._y += jumpSpeed;\r\n<\/pre>\n<p>Also, change the <tt>jumpSpeedLimit<\/tt> to 20, eh?<\/p>\n<p>Now, we're done with the intense block programming. Next lesson, we'll add some stuff to the level, like ladders and other obstacles. We also have to make it so that when we run into a wall, we're stopped by it. Stay tuned...<\/p>\n<h4 id=\"1031_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\/pt3\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt3\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt3\/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 Step 3: Programming the Level Now that we&#8217;ve set up all of the blocks onto the stage, we can actually add some code to them. The first thing we&#8217;re going to do [&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\/1031"}],"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=1031"}],"version-history":[{"count":3,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1191,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1031\/revisions\/1191"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}