{"id":672,"date":"2008-08-30T08:01:23","date_gmt":"2008-08-30T12:01:23","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=672"},"modified":"2022-05-29T08:23:35","modified_gmt":"2022-05-29T12:23:35","slug":"tutorial-create-a-platform-game-in-as3-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-platform-game-in-as3-part-2\/","title":{"rendered":"Tutorial: Create a Platform Game in AS3 &#8211; Part 2"},"content":{"rendered":"<div class=\"toc\">\n<h3 id=\"672_table-of-contents_1\" >Table of Contents<\/h3>\n<ol>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3\">Basic Character Programming<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3-part-2\">Creating The Level<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3-part-3\">Programming the Level<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3-part-4\">Adding Level Elements<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3-part-5\">Adding Enemies<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3-part-6\">Goals and Level Completion<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-create-a-platform-game-in-as3-part-7\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<h3 id=\"672_step-2-creating-the-_1\" >Step 2: Creating the Level<\/h3>\n<p>Now, we have to set up blocks on stage that will account for a level. We&#8217;ll use an array to accomplish this manly feat. We&#8217;re also going to create some other level variables. Place this code at the top:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/LEVEL VARIABLES\r\n\/\/the current lvl\r\nvar lvlCurrent:int = 1;\r\n\/*The key for the level arrays:\r\n1: Regular Block\r\nX: Main Character\r\n*\/\r\n\/\/this variable will hold the character\r\nvar X:String = 'MAIN';\r\n\/\/the array for level 1\r\nvar lvlArray1:Array = new Array(\r\n\t1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,\r\n\t1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1\r\n);\r\n<\/pre>\n<p>This is a pretty big array, but it won&#8217;t be too hard to understand, I hope. The next thing we have to do is to actually make a function which creates the level. We&#8217;ll make some more variables for this as well:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/current row that we are creating\r\nvar row:int = 0;\r\n<\/pre>\n<p>Now, we have to create a class that will have all of the code for the blocks. Create a new external ActionScript file called &#8220;Block.as&#8221;. Then, place the following code into it:<\/p>\n<pre lang=\"actionscript\">\r\npackage{\r\n\timport flash.display.Sprite;\r\n\timport flash.display.MovieClip;\r\n\timport flash.events.*;\r\n\t\r\n\t\/\/sprites are just movieclips without any frames in them\r\n\tpublic class Block extends Sprite{\r\n\t\t\/\/_root will signify the root of the document\r\n\t\tprivate var _root:Object;\r\n\t\t\r\n\t\tpublic function Block(){\r\n\t\t\t\/\/this code will only be run once\r\n\t\t\taddEventListener(Event.ADDED, beginClass);\r\n\t\t\t\/\/this code will constantly be run\r\n\t\t\taddEventListener(Event.ENTER_FRAME, eFrame);\r\n\t\t}\r\n\t\t\r\n\t\tprivate function beginClass(event:Event):void{\r\n\t\t\t\/\/defining the root of the document\r\n\t\t\t_root = MovieClip(root);\r\n\t\t}\r\n\t\t\r\n\t\tprivate function eFrame(event:Event):void{\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>We&#8217;ll add on to this later but for now, we have to define the function that will place these bricks on stage. Place the following code at the end of the frame:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/creating the level\r\n\/\/this guy will hold all of the blocks\r\nvar blockHolder:Sprite = new Sprite();\r\n\/\/then we add him to stage\r\naddChild(blockHolder);\r\nfunction createLvl():void{\r\n\t\/\/getting the current level that we are on\r\n\tvar lvlArray:Array = MovieClip(root)['lvlArray'+lvlCurrent];\r\n\t\/\/we have to find how far this level goes\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 lvl formatting very strict\r\n\tvar lvlColumns:int = Math.ceil(lvlArray.length\/16);\r\n\t\/\/now we must create the level\r\n\tfor(var i:int = 0;i<lvlArray.length;i++){\r\n\t\tif(lvlArray[i] == 1){\r\n\t\t\t\/\/checking if we move onto the next row\r\n\t\t\t\/\/this checks if i is divisible by the # of columns\r\n\t\t\tif(i\/lvlColumns == int(i\/lvlColumns)){\r\n\t\t\t\trow ++;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/making a new block\r\n\t\t\tvar newBlock:Block = new Block();\r\n\t\t\t\/\/drawing the block\r\n\t\t\tnewBlock.graphics.beginFill(0xFFFFFF\/*The color for shape*\/,1\/*The alpha for the shape*\/);\r\n\t\t\t\/\/turning the shape into a square\r\n\t\t\tnewBlock.graphics.drawRect(0,0,25,25);\r\n\t\t\t\/\/change the coordinates of the block\r\n\t\t\tnewBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;\r\n\t\t\tnewBlock.y = (row-1)*newBlock.height;\r\n\t\t\t\/\/then finally adding it to stage\r\n\t\t\tblockHolder.addChild(newBlock);\r\n\t\t} else if (lvlArray[i] == 'MAIN'){\r\n\t\t\tmcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;\r\n\t\t\tmcMain.y = (row-1)*newBlock.height;\r\n\t\t}\r\n\t}\r\n\t\/\/reset the row for another use\r\n\trow = 0;\r\n}\r\n\r\n\/\/running the createlvl funciton\r\ncreateLvl();\r\n<\/pre>\n<p>This is some pretty intense code. Look it over for a bit and try to comprehend it. This type of code is what you need to make if you want to become a better developer.<\/p>\n<p>This concludes this part of the tutorial. Next time, we'll add some code to these blocks and make them interact with the character. Stay tuned...<\/p>\n<h4 id=\"672_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-as3\/pt2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as3\/pt2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as3\/pt2\/source.zip\">Source Files (zipped)<\/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 2: Creating the Level Now, we have to set up blocks on stage that will account for a level. We&#8217;ll use an array to accomplish this manly feat. We&#8217;re also going [&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,8,4,6],"tags":[25,160,8,162,19,18,161,22,11],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/672"}],"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=672"}],"version-history":[{"count":4,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/672\/revisions"}],"predecessor-version":[{"id":1029,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/672\/revisions\/1029"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}