{"id":1116,"date":"2009-01-17T08:02:48","date_gmt":"2009-01-17T12:02:48","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1116"},"modified":"2022-05-29T08:23:31","modified_gmt":"2022-05-29T12:23:31","slug":"tutorial-make-a-vertical-shooter-in-as2-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as2-part-2\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 2"},"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 class=\"c_chap\"><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><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 2: Programming the Character Part 2 &#8211; Making it Shoot<\/h3>\n<p>Well, now that we&#8217;ve got the character moving, we have to make him able to shoot. The first step in doing this is to create a MovieClip which will be the bullet. Mine is 5&#215;30 pixels. Give it a name of &#8220;mcBullet&#8221; with the capitals. We also are going to have to export it for actionscript. Hopefully by now, you know how to do this. But, I&#8217;ll explain it once again (for the 5th time).<\/p>\n<p>Right click the MovieClip and click on &#8220;Linkage&#8230;&#8221;. A window will pop up. In this window, check off &#8220;Export for ActionScript&#8221; and leave everything just as it is.<\/p>\n<p>Now, we can add actions to the bullet. Add the following code to the <tt>onEnterFrame()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\tif(Key.isDown(32)){\/\/if the space bar is pressed\r\n\t\tvar bulletID:Number = Math.random(); \/\/create a variable that we'll use at the bullet's id\r\n\t\t\/\/then attach a bullet to the stage\r\n\t\t_root.attachMovie('mcBullet', 'Bullet'+bulletID,_root.getNextHighestDepth());\r\n\t\t\/\/setting the coordinates of the bullet to be the same as the main character\r\n\t\t_root['Bullet'+bulletID]._x = mcMain._x + mcMain._width\/2 - _root['Bullet'+bulletID]._width\/2;\r\n\t\t_root['Bullet'+bulletID]._y = mcMain._y;\r\n\t\t_root['Bullet'+bulletID].onEnterFrame = function(){\r\n\t\t\t\/\/giving the bullet some actions\r\n\t\t\tthis._y -= 10; \/\/moving the bullet\r\n\t\t\tif(this._y < -1 * this._height){\/\/if the bullet goes off stage\r\n\t\t\t\t\/\/then destroy it\r\n\t\t\t\tthis.removeMovieClip();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>Now, when the user presses the space bar, a bullet should appear and fly upwards! It'll also disappear once it leaves the screen so it doesn't slow the game down. Now, the only problem with this script is that it doesn't limit the amount of time between each bullet shot. In order to do this, we must first add a few variables at the top of the code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/BULLET TIMING VARIABLES\r\nvar cTime:Number = 0;\/\/the amount of frames that has elapsed since last bullet shot\r\nvar cLimit:Number = 12;\/\/amount of frames needed to shoot another bullet\r\nvar shootAllow:Boolean = false;\/\/whether or not main can shoot\r\n<\/pre>\n<p>Next, add this code to the <tt>onEnterFrame<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\tcTime ++;\/\/increment the time\r\n\tif(cTime == cLimit){\/\/if enough time has elapsed\r\n\t\tshootAllow = true;\/\/allow shooting again\r\n\t\tcTime = 0;\/\/reset the time\r\n\t}\r\n<\/pre>\n<p>Now, the last two things we have to do is first make it so you can only shoot when <tt>shootAllow<\/tt> is set to true and set <tt>shootAllow<\/tt> to false when we shoot a bullet. First, simply change the <tt>if(Key.isDown[...])<\/tt> statement for the space bar to <tt>if(Key.isDown(32) && shootAllow)<\/tt>. Next, add <tt>shootAllow = false;<\/tt> to the end of that statement.<\/p>\n<p>The final <tt>if<\/tt> statement should look a bit like this:<\/p>\n<pre lang=\"actionscript\">\r\nif(Key.isDown(32) && shootAllow){\/\/if the space bar is pressed\r\n\t\t[...CODE...]\r\n\t\tshootAllow = false;\r\n\t}\r\n<\/pre>\n<p>Now if you test your movie, you should be able to shoot! Hurrah!<\/p>\n<p>This concludes this part of the tutorial, stay tuned for the next one, creating enemies!<\/p>\n<h4>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\/pt2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as2\/pt2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as2\/pt2\/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 2: Programming the Character Part 2 &#8211; Making it Shoot Well, now that we&#8217;ve got the character moving, we have to make him able to shoot. The first step in doing this is [&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\/1116"}],"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=1116"}],"version-history":[{"count":6,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1116\/revisions"}],"predecessor-version":[{"id":1204,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1116\/revisions\/1204"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}