{"id":1049,"date":"2009-01-03T08:06:36","date_gmt":"2009-01-03T12:06:36","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1049"},"modified":"2022-05-29T08:23:32","modified_gmt":"2022-05-29T12:23:32","slug":"tutorial-create-a-platform-game-in-as2-part-7","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-platform-game-in-as2-part-7\/","title":{"rendered":"Tutorial: Create a Platform Game in AS2 &#8211; Part 7"},"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><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 class=\"c_chap\"><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>Step 7: Finishing Touches<\/h3>\n<p>As I&#8217;ve done with most of my previous tutorials, this &#8220;Finishing Touches&#8221; part of the tutorial will mostly be me telling you what to do, but not how to do it. Of course, if it&#8217;s new or advanced material, I&#8217;ll definitely give you the code. Let&#8217;s get started, shall we?<\/p>\n<p>The first thing we need to do to our game is show the score to the user. Just create a dynamic text field somewhere at the top of the stage and make some code that will update it with the text &#8220;Score: 0&#8221;. Of course, the 0 would be replaced with whatever the guy&#8217;s score is. I suggest placing it somewhere in the <tt>onEnterFrame()<\/tt> function so that you don&#8217;t have to create a totally new function for it.<\/p>\n<p>The next thing I want to do is reset the score whenever the player dies. This actually will be a little bit tricky because we use the same <tt>resetLvl()<\/tt> function both when the player loses and when the player beats a level. This can be accomplished pretty easily. First of all, just set the score to 0 at the end of the <tt>resetLvl()<\/tt> function. Now, find the code where we reset the level when the player wins, where we create the actions for the goal. Change it to this:<\/p>\n<pre lang=\"actionscript\">\r\nif(this.hitTest(_root.mcMain)){\r\n\t\/\/go to the next lvl\r\n\tlastScore = _root.mainScore;\r\n\t_root.lvlCurrent ++;\r\n\t_root.createLvl();\r\n\t_root.resetLvl();\r\n\t_root.mainScore = lastScore;\r\n}\r\n<\/pre>\n<p>This just saves the score that we have before advancing a level and then re-applies that score after it&#8217;s reset. Pretty cool, eh?<\/p>\n<p>Next, we&#8217;re going to add a background to the game. The background will be a bit darker so we can distinguish it from the game, and it&#8217;ll also move slower than the game background to create an illusion that it&#8217;s farther away. The first thing we have to do is create another holder for the particles within the level holder. Do it just like how we did it for the bumpers and trampolines. It should be called <tt>bgHolder<\/tt> and you should place it before all of the other elements so it&#8217;s under everything. Do it also again within the <tt>resetLvl()<\/tt> function<\/p>\n<p>Next, add this code to the end of the <tt>createLvl()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/creating random background particles\r\n\/\/we'll create 1 particle for every block\r\nlvlHolder.bgHolder.createEmptyMovieClip(\"bg\"+i, lvlHolder.bgHolder.getNextHighestDepth());\r\nvar bgX:Number = int(Math.random()*lvlColumns*50)-550; \/\/the x value of this shape\r\nvar bgY:Number = (row-1)*25; \/\/the y value of this shape\r\nlvlHolder.bgHolder[\"bg\"+i].beginFill(0x222222); \/\/this just determines the shape's color\r\nlvlHolder.bgHolder[\"bg\"+i].moveTo(bgX, bgY); \/\/moves the shape\r\n\/\/creating 4 random points to make a random shape\r\nlvlHolder.bgHolder[\"bg\"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));\r\nlvlHolder.bgHolder[\"bg\"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));\r\nlvlHolder.bgHolder[\"bg\"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));\r\nlvlHolder.bgHolder[\"bg\"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));\r\nlvlHolder.bgHolder[\"bg\"+i].endFill();\/\/finishes up the shape\r\n<\/pre>\n<p>Next, we have to make them move slower than the game background. You can do this in the <tt>onEnterFrame<\/tt> function. Find this code:<\/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\tlvlHolder._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\tlvlHolder._x -= mainSpeed; \/\/then move the guy to the right\r\n}\r\n<\/pre>\n<p>and replace it with this:<\/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\tlvlHolder._x += mainSpeed;\/\/then the move the guy left\r\n\tlvlHolder.bgHolder._x -= mainSpeed*.5;\r\n} else if(Key.isDown(39) || Key.isDown(68)){\/\/if the \"D\" key or Right Arrow Key is Down\r\n\tlvlHolder._x -= mainSpeed; \/\/then move the guy to the right\r\n\tlvlHolder.bgHolder._x += mainSpeed*.5;\r\n}\r\n<\/pre>\n<p>This just slows down the movement of the background. Well, that&#8217;s basically all that I&#8217;m going to do for the finishing touches. Enjoy your new game!<\/p>\n<h4>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\/pt7\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as2\/pt7\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/platform-as2\/pt7\/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 7: Finishing Touches As I&#8217;ve done with most of my previous tutorials, this &#8220;Finishing Touches&#8221; part of the tutorial will mostly be me telling you what to do, but not how [&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\/1049"}],"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=1049"}],"version-history":[{"count":4,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1049\/revisions"}],"predecessor-version":[{"id":1196,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1049\/revisions\/1196"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1049"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}