{"id":1140,"date":"2009-01-17T08:05:02","date_gmt":"2009-01-17T12:05:02","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1140"},"modified":"2022-05-29T08:23:30","modified_gmt":"2022-05-29T12:23:30","slug":"tutorial-make-a-vertical-shooter-in-as2-part-5","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as2-part-5\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 5"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-make-a-vertical-shooter-in-as2\">Programming the Character<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-make-a-vertical-shooter-in-as2-part-2\">Programming the Character &#8211; Part 2<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-make-a-vertical-shooter-in-as2-part-3\">Creating the Enemies<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-make-a-vertical-shooter-in-as2-part-4\">Programming the Enemies<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-make-a-vertical-shooter-in-as2-part-5\">Scoring<\/a><\/li>\n<li><a href=\"http:\/\/www.mrsunstudios.com\/2009\/01\/tutorial-make-a-vertical-shooter-in-as2-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  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; and, in smaller text, &#8220;Press Space to Restart&#8221;.<\/p>\n<p>Then, in the actions, type in the following code:<\/p>\n<pre lang=\"actionscript\">\r\nstop();\/\/stops the game so nothing goes wrong\r\n\r\n_root.onEnterFrame = function(){ \/\/create a function to check for keystrokes\r\n\tif(Key.isDown(32)){\/\/if the space key is down...\r\n\t\tgotoAndStop(1); \/\/then go back to the first frame, restarting the game\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, I&#8217;ve noticed a bit of a problem in the main code.  For some reason, the game thinks that you get hit whenever you shoot an enemy.  I&#8217;ve found a way around this strange behavior, however. First of all, add this variable to the top of the code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/showing the amount of enemies that have been added to the stage\r\nvar enemyTotal:Number = 0;\r\n<\/pre>\n<p>Next, find the code where we have to create the enemy and add it to the stage.  Change it to this:<\/p>\n<pre lang=\"actionscript\">\r\n_root.attachMovie('mcEnemy', 'en'+enemyTotal,_root.getNextHighestDepth());\/\/then add the enemy\r\n\/\/setting it's coordinates\r\n_root['en'+enemyTotal]._x = int(Math.random()*Stage.width);\/\/randomly within the boundaries\r\n_root['en'+enemyTotal]._y = -50; \/\/sets this offstage at first\r\n_root['en'+enemyTotal].onEnterFrame = function(){\/\/then give it some functions\r\n\tthis._y += 5;\r\n\t\r\n\t\/\/run a loop checking if it's touching any bullets\r\n\tfor(var cBullet:String in _root.bulletHolder){\r\n\t\t\/\/if it's touching the bullet\r\n\t\t\/\/we have to use coordinates because hit testing doesn't seem to work\r\n\t\tif(this._y >= _root.bulletHolder[cBullet]._y-30 && this._y <= _root.bulletHolder[cBullet]._y){\r\n\t\t\tif(this._x <= _root.bulletHolder[cBullet]._x+5 &#038;&#038; this._x >= _root.bulletHolder[cBullet]._x -35){\r\n\t\t\t\t\/\/then destroy this guy\r\n\t\t\t\tthis.removeMovieClip();\r\n\t\t\t\t\/\/and destroy the bullet\r\n\t\t\t\t_root.bulletHolder[cBullet].removeMovieClip();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\r\n\t\/\/hit testing with the user\r\n\tif(this.hitTest(_root.mcMain)){\r\n\t\t\/\/we'll add code here in the next part\r\n\t\t\/\/but for now, we'll just trace something\r\n\t\ttrace('You just got hit!');\r\n\t}\r\n}\r\nenemyTime = 0;\/\/reset the time\r\nenemyTotal ++; \/\/add 1 more to the amount of enemies total\r\n<\/pre>\n<p>I&#8217;m sorry about this delay, but we need this to continue on with our game.<\/p>\n<p>Now, we have to create a new variable at the top that will say whether or not the game has been ended:<\/p>\n<pre lang=\"actionscript\">\r\nstop();\/\/add a stop function too!\r\n\r\nvar gameOver:Boolean = false;\r\n<\/pre>\n<p>Then, add this code to both the <tt>mcBullet<\/tt>&#8216;s and <tt>mcEnemy<\/tt>&#8216;s <tt>onEnterFrame<\/tt> functions:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/checking if the game is over\r\nif(gameOver){\r\n\t\/\/destroy this guy if the game is over\r\n\tthis.removeMovieClip();\r\n}\r\n<\/pre>\n<p>Now, we can make this Boolean false when the user gets touched by the enemy. Find where we hit test for <tt>mcMain<\/tt> and replace that trace statement with:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/hit testing with the user\r\nif(this.hitTest(_root.mcMain)){\r\n\t\/\/set the game to be over and go to lose screen\r\n\tgameOver = true;\r\n\tgotoAndStop('lose');\r\n}\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:Number = 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>onEnterFrame<\/tt> function.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/updating the score text field\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-as2\/pt5\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as2\/pt5\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as2\/pt5\/source.fla\">Source .fla File<\/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,7,4,6,68],"tags":[25,7,19,18,68,152,22,150,151,11,149],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1140"}],"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=1140"}],"version-history":[{"count":7,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":1208,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions\/1208"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}