{"id":632,"date":"2008-08-23T08:04:34","date_gmt":"2008-08-23T12:04:34","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=632"},"modified":"2022-05-29T08:23:36","modified_gmt":"2022-05-29T12:23:36","slug":"tutorial-make-a-vertical-shooter-in-as3-part-5","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as3-part-5\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS3 &#8211; Part 5"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-make-a-vertical-shooter-in-as3\">Programming the Character<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-make-a-vertical-shooter-in-as3-part-2\">Programming the Character &#8211; Part 2<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-make-a-vertical-shooter-in-as3-part-3\">Creating the Enemies<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-make-a-vertical-shooter-in-as3-part-4\">Programming the Enemies<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-make-a-vertical-shooter-in-as3-part-5\">Scoring<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2008\/08\/tutorial-make-a-vertical-shooter-in-as3-part-6\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<h3>Step 5: Scoring<\/h3>\n<p>Now that we&#8217;ve got the hardest part down, it all gets easier from here. This chapter will be simple, just some code that has scoring. Also, as promised, we&#8217;re going to have a function run when the player is hit by an enemy. Let&#8217;s start with this one first, eh?<\/p>\n<p>In order to do this, we have to make a frame called &#8220;lose&#8221;. This will be the frame that we&#8217;ll navigate to when we get hit. It&#8217;ll be simple, just some text that says &#8220;You Lose&#8221; and a listener for keystrokes or a click that will return us to play another game. First, make the frame called &#8220;lose&#8221;. I recommend making an entire new layer for labels, but this isn&#8217;t required. In that frame, draw or type in whatever you want to signify that the player has lost the game. I&#8217;m just going to put the text, &#8220;YOU LOSE&#8221;.<\/p>\n<p>Then, in the actions, type in the following code:<\/p>\n<pre lang=\"actionscript\">\r\nstop();\r\n\r\nstage.addEventListener(MouseEvent.CLICK, goBack);\r\n\r\nfunction goBack(event:MouseEvent):void{\r\n\tgotoAndStop(1);\r\n\tstage.removeEventListener(MouseEvent.CLICK, goBack);\r\n}\r\n<\/pre>\n<p>Then, we&#8217;ll have to add a variable called <tt>gameOver<\/tt> to signify that the game is over and that some stuff should be deleted and such.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/whether or not the game is over\r\nvar gameOver:Boolean = false;\r\n<\/pre>\n<p>Then, both in the <tt>Bullet<\/tt>&#8216;s and the <tt>Enemy<\/tt>&#8216;s <tt>eFrame()<\/tt> function, add the following code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/checking if game is over\r\nif(_root.gameOver){\r\n\tremoveEventListener(Event.ENTER_FRAME, eFrame);\r\n\tthis.parent.removeChild(this);\r\n}\r\n<\/pre>\n<p>Finally, add this code to where we hit tested for the main character in &#8220;Enemy.as&#8221;<\/p>\n<pre lang=\"actionscript\">\r\n\/\/hit testing with the user\r\nif(hitTestObject(_root.mcMain)){\r\n\t\/\/losing the game\r\n\t_root.gameOver = true;\r\n\t_root.gotoAndStop('lose');\r\n}\r\n<\/pre>\n<p>If you test the game, there will be a bug that comes up. Don&#8217;t worry, we can fix it. Just keep <tt>mcMain<\/tt> in the &#8220;lose&#8221; frame. Also, so it can&#8217;t be controlled while in the &#8220;lose&#8221; frame, place the following code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/keeping mcMain out of sight\r\nmcMain.x = stage.stageWidth;\r\nmcMain.y = stage.stageHeight;\r\nmcMain.removeEventListener(Event.ENTER_FRAME, moveChar);\r\n<\/pre>\n<p>Phew, that was a lot of work, wasn&#8217;t it? Now we can move onto actually scoring the game. First, we have to define a score variable at the top:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/the player's score\r\nvar score:int = 0;\r\n<\/pre>\n<p>Then, increment the score every time an enemy is killed. Place this code in the Enemy&#8217;s hit testing for the bullet.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/up the score\r\n_root.score += 5;\r\n<\/pre>\n<p>Now, we can show the score to the user with a dynamic text field. Make one at the bottom of the stage and give it an instance name of <tt>txtScore<\/tt>. Next, place this code into the <tt>moveChar<\/tt> function.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/updating the score text\r\ntxtScore.text = 'Score: '+score;\r\n<\/pre>\n<p>Pretty easy, right? Well, that&#8217;s all we&#8217;re going to do for scoring. Next, we&#8217;ll add some sweet finishing touches, eh?<\/p>\n<h4>The Final Product:<\/h4>\n<p><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"300\" 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\/vert-shooter-as3\/pt5\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt5\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt5\/source.zip\">Source Files (zipped)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents Programming the Character Programming the Character &#8211; Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 5: Scoring Now that we&#8217;ve got the hardest part down, it all gets easier from here. This chapter will be simple, just some code that has scoring. Also, as promised, we&#8217;re going to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,8,4,6,68],"tags":[25,8,19,18,68,152,22,150,151,11,149],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/632"}],"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=632"}],"version-history":[{"count":6,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/632\/revisions"}],"predecessor-version":[{"id":1145,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/632\/revisions\/1145"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=632"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}