{"id":1025,"date":"2009-01-03T08:01:23","date_gmt":"2009-01-03T12:01:23","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1025"},"modified":"2022-05-29T08:23:33","modified_gmt":"2022-05-29T12:23:33","slug":"tutorial-create-a-platform-game-in-as2-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-platform-game-in-as2-part-2\/","title":{"rendered":"Tutorial: Create a Platform Game in AS2 &#8211; Part 2"},"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 class=\"c_chap\"><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<h3 id=\"1025_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:Number = 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:Number = 0;\r\n<\/pre>\n<p>Next, we have to create a <tt>MovieClip<\/tt> that will act as the block. I&#8217;m just going to make it a 25&#215;25 white square without borders. Name it <tt>mcBlock<\/tt>, then export it for ActionScript:<br \/>\n<img loading=\"lazy\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/platform-as2\/pt2\/exporting.gif\" alt=\"Exporting this square for ActionScript\" title=\"Exporting this square for ActionScript\" width=\"600\" height=\"320\"\/><\/p>\n<p>Finally, we can do some more coding. Now, we have to define the function that will place the 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 empty movieclip will hold all of the blocks\r\n_root.createEmptyMovieClip('blockHolder',_root.getNextHighestDepth());\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}\r\n\/\/finally, we run the function for the first time\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=\"1025_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\/pt2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt2\/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 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,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\/1025"}],"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=1025"}],"version-history":[{"count":9,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1025\/revisions"}],"predecessor-version":[{"id":1190,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1025\/revisions\/1190"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1025"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}