{"id":726,"date":"2008-08-30T08:06:26","date_gmt":"2008-08-30T12:06:26","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=726"},"modified":"2022-05-29T08:23:35","modified_gmt":"2022-05-29T12:23:35","slug":"tutorial-create-a-platform-game-in-as3-part-7","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-platform-game-in-as3-part-7\/","title":{"rendered":"Tutorial: Create a Platform Game in AS3 &#8211; Part 7"},"content":{"rendered":"<div class=\"toc\">\n<h3>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><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 class=\"c_chap\"><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>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>moveChar()<\/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 won. It should be at around line 30 in the &#8220;Goal.as&#8221; file. Change it to this:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/advancing a level\r\n_root.lvlCurrent ++;\r\nvar lastScore:int = _root.mainScore;\r\n_root.resetLvl();\r\n_root.mainScore = lastScore;\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<\/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\nvar newPart:Shape = new Shape();\r\nnewPart.graphics.beginFill(0x222222);\r\nnewPart.graphics.drawRect(0,0,int(Math.random()*10)+1,int(Math.random()*10)+1);\r\nnewPart.x = int(Math.random()*lvlColumns*50)-550;\r\nnewPart.y = (row-1)*25;\r\nbgHolder.addChild(newPart);\r\n<\/pre>\n<p>Next, we have to make them move slower than the game background. You can do this in the <tt>moveChar<\/tt> function. Find this code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/if certain keys are down then move the character\r\nif(leftKeyDown){\r\n\tlvlHolder.x += mainSpeed;\r\n}\r\nif(rightKeyDown){\r\n\tlvlHolder.x -= mainSpeed;\r\n}\r\n<\/pre>\n<p>and replace it with this:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/if certain keys are down then move the character\r\nif(leftKeyDown){\r\n\tlvlHolder.x += mainSpeed;\r\n\tbgHolder.x -= mainSpeed*.5;\r\n}\r\nif(rightKeyDown){\r\n\tlvlHolder.x -= mainSpeed;\r\n\tbgHolder.x += mainSpeed*.5;\r\n}\r\n<\/pre>\n<p>The final thing we need to do for the background is to reset it&#8217;s x value whenever the level is reset. Just set it to x either in the <tt>createLvl()<\/tt> function or <tt>resetLvl()<\/tt> function, whichever works best for you. I&#8217;m going to do mine it the <tt>createLvl()<\/tt> function.<\/p>\n<p>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-as3\/pt7\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/platform-as3\/pt7\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/platform-as3\/pt7\/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 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,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\/726"}],"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=726"}],"version-history":[{"count":4,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":1444,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/726\/revisions\/1444"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}