{"id":1042,"date":"2009-01-03T08:04:39","date_gmt":"2009-01-03T12:04:39","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1042"},"modified":"2022-05-29T08:23:32","modified_gmt":"2022-05-29T12:23:32","slug":"tutorial-create-a-platform-game-in-as2-part-5","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-platform-game-in-as2-part-5\/","title":{"rendered":"Tutorial: Create a Platform Game in AS2 &#8211; Part 5"},"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><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 class=\"c_chap\"><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=\"1042_step-5-adding-enemie_1\" >Step 5: Adding Enemies<\/h3>\n<p>The next step in making our exciting platform game is to add some enemies! In order to do this, we&#8217;re first going to have to make an Enemy MovieClip. Mine is just going to be a red circle. Then, we&#8217;re going to turn it into a MovieClip called &#8220;mcEnemy&#8221;.<\/p>\n<p>Next, I&#8217;m just going to make a <tt>mcInvisMarker<\/tt> that will tell the enemy when it should turn around when it moves. It&#8217;ll be invisible, so I&#8217;m going to make it a square with the alpha set to 0. Then, I&#8217;ll just make it into a MovieClip that&#8217;s exported for ActionScript.<\/p>\n<p>Now, we have to add some code to the <tt>createLvl()<\/tt> function. This code will just add the enemy to stage and also some the invisible markers. Add the following to the for loop in the <tt>createLvl()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n else if (lvlArray[i] == 5){\r\n\tlvlHolder.enemyHolder.attachMovie('mcEnemy', 'Enemy'+i,lvlHolder.enemyHolder.getNextHighestDepth());\r\n\tlvlHolder.enemyHolder['Enemy'+i]._x = (i-(row-1)*lvlColumns)*25;\r\n\tlvlHolder.enemyHolder['Enemy'+i]._y = (row-1)*25;\r\n} else if (lvlArray[i] == 6){\r\n\tlvlHolder.markerHolder.attachMovie('mcInvisMarker', 'Marker'+i,lvlHolder.markerHolder.getNextHighestDepth());\r\n\tlvlHolder.markerHolder['Marker'+i]._x = (i-(row-1)*lvlColumns)*25;\r\n\tlvlHolder.markerHolder['Marker'+i]._y = (row-1)*25;\r\n}\r\n<\/pre>\n<p>Of course, we&#8217;re going to have to add both a <tt>markerHolder<\/tt> and an <tt>enemyHolder<\/tt>. Hopefully, you can do this on your own. Just do the exact same thing we did with the other holders except with that name. Next, change the level array to this:<\/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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,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,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>We can now add some real programming to the Enemy. Add the following code to the <tt>createLvl()<\/tt> function where we add the enemy to the stage:<\/p>\n<pre lang=\"actionscript\">\r\nif (lvlArray[i] == 5){\r\n\tlvlHolder.enemyHolder.attachMovie('mcEnemy', 'Enemy'+i,lvlHolder.enemyHolder.getNextHighestDepth());\r\n\tlvlHolder.enemyHolder['Enemy'+i]._x = (i-(row-1)*lvlColumns)*25;\r\n\tlvlHolder.enemyHolder['Enemy'+i]._y = (row-1)*25;\r\n\tlvlHolder.enemyHolder['Enemy'+i].speed = 5;\r\n\tlvlHolder.enemyHolder['Enemy'+i].direction = 1;\r\n\tlvlHolder.enemyHolder['Enemy'+i].onEnterFrame = function(){\/\/here's the function to program the enemy\r\n\t\t\/\/MOVING THE ENEMY\r\n\t\tthis._x += this.speed * this.direction;\r\n\t\t\/\/checking if touching any invisible markers\r\n\t\tfor(var cMarker:String in lvlHolder.markerHolder){\r\n\t\t\t\/\/the process is very similar to the main guy's testing with other elements\r\n\t\t\tif(this.hitTest(lvlHolder.markerHolder[cMarker])){\r\n\t\t\t\tthis.direction *= -1;\r\n\t\t\t\tthis._x += this.speed * this.direction;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\/\/TOUCHING THE MAIN CHARACTER\r\n\t\tif(this.hitTest(_root.mcMain)){\r\n\t\t\t\/\/if it touches the main guy while he's jumping\r\n\t\t\t\/\/and the guy is falling down, not jumping up\r\n\t\t\tif(_root.mainJumping && _root.jumpSpeed > 0){\r\n\t\t\t\t\/\/kill this guy\r\n\t\t\t\tthis.removeMovieClip();\r\n\t\t\t} else {\r\n\t\t\t\t\/\/otherwise, kill the main character\r\n\t\t\t\t\/\/we don't have any death frame yet, so we'll make it later\r\n\t\t\t\t\/\/and we'll add a trace statement\r\n\t\t\t\ttrace('OH NO! You died!');\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Test it out. You should see a red circle moving around the platform above where you start off. If you touch it, a trace statement should be called. If you jump on it, the enemy should disappear. Pretty nifty, eh? The next thing we have to do is to make a function that will reset the game whenever the main guy dies. Add this code to the main timeline:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/resets the level\r\nfunction resetLvl():Void{\r\n\tlvlHolder.removeMovieClip(); \/\/destroy the lvl holder and everything in it\r\n\t\/\/then we add everything back again\r\n\t_root.createEmptyMovieClip('lvlHolder', _root.getNextHighestDepth());\r\n\tlvlHolder.createEmptyMovieClip('blockHolder',lvlHolder.getNextHighestDepth());\r\n\tlvlHolder.createEmptyMovieClip('ladderHolder',lvlHolder.getNextHighestDepth());\r\n\tlvlHolder.createEmptyMovieClip('bumperHolder',lvlHolder.getNextHighestDepth());\r\n\tlvlHolder.createEmptyMovieClip('trampHolder',lvlHolder.getNextHighestDepth());\r\n\tlvlHolder.createEmptyMovieClip('enemyHolder',lvlHolder.getNextHighestDepth());\r\n\tlvlHolder.createEmptyMovieClip('markerHolder',lvlHolder.getNextHighestDepth());\r\n\t\/\/then we remake the lvl and reset the main's existence\r\n\tcreateLvl();\r\n\tmcMain.swapDepths(getNextHighestDepth());\r\n}\r\n<\/pre>\n<p>Now, we have to run this function whenever the main guy runs off of the stage or touches the enemy. I&#8217;m going to leave it up to you to figure that out. Of course, there are always the source files if you need them.<\/p>\n<p>Well, that&#8217;s basically it for enemy creation. Join us next time when we actually make this thing into a game!<\/p>\n<h4 id=\"1042_the-final-product_1\" >The 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\/pt5\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt5\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/platform-as2\/pt5\/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 5: Adding Enemies The next step in making our exciting platform game is to add some enemies! In order to do this, we&#8217;re first going to have to make an Enemy [&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,68],"tags":[25,160,7,162,19,18,161,22,11],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1042"}],"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=1042"}],"version-history":[{"count":7,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1042\/revisions"}],"predecessor-version":[{"id":1195,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1042\/revisions\/1195"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1042"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}