{"id":608,"date":"2008-08-23T08:01:19","date_gmt":"2008-08-23T12:01:19","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=608"},"modified":"2022-05-29T08:23:36","modified_gmt":"2022-05-29T12:23:36","slug":"tutorial-make-a-vertical-shooter-in-as3-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as3-part-2\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS3 &#8211; Part 2"},"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 class=\"c_chap\"><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><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 2: Programming the Character Pt 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;Bullet&#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 that we&#8217;ve got this down, we&#8217;re going to make a &#8220;Bullet&#8221; class. To do this, we have to make an external .as file. Press Command+N if you are on a Mac or Ctrl+N if you are on Windows and under &#8220;Type:&#8221;, select &#8220;ActionScript File&#8221;. Then, immediately save this file as &#8220;Bullet.as&#8221; in the same directory as your main game. Then, within it, type in the following code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/This is the basic skeleton that all classes must have\r\npackage{\r\n\t\/\/we have to import certain display objects and events\r\n\timport flash.display.MovieClip;\r\n\timport flash.events.*;\r\n\t\/\/this just means that Bullet will act like a MovieClip\r\n\tpublic class Bullet extends MovieClip{\r\n\t\t\/\/VARIABLES\r\n\t\t\/\/this will act as the root of the document\r\n\t\t\/\/so we can easily reference it within the class\r\n\t\tprivate var _root:Object;\r\n\t\t\/\/how quickly the bullet will move\r\n\t\tprivate var speed:int = 10;\r\n\t\t\/\/this function will run every time the Bullet is added\r\n\t\t\/\/to the stage\r\n\t\tpublic function Bullet(){\r\n\t\t\t\/\/adding events to this class\r\n\t\t\t\/\/functions that will run only when the MC is added\r\n\t\t\taddEventListener(Event.ADDED, beginClass);\r\n\t\t\t\/\/functions that will run on enter frame\r\n\t\t\taddEventListener(Event.ENTER_FRAME, eFrame);\r\n\t\t}\r\n\t\tprivate function beginClass(event:Event):void{\r\n\t\t\t_root = MovieClip(root);\r\n\t\t}\r\n\t\tprivate function eFrame(event:Event):void{\r\n\t\t\t\/\/moving the bullet up screen\r\n\t\t\ty -= speed;\r\n\t\t\t\/\/making the bullet be removed if it goes off stage\r\n\t\t\tif(this.y < -1 * this.height){\r\n\t\t\t\tremoveEventListener(Event.ENTER_FRAME, eFrame);\r\n\t\t\t\t_root.removeChild(this);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>This code really won't do anything by itself. We have to add it to stage whenever the player presses a button (the Space Bar). We also want to limit how quickly the user can shoot. This will be easy. Just add these variables to the top of the main code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/how much time before allowed to shoot again\r\nvar cTime:int = 0;\r\n\/\/the time it has to reach in order to be allowed to shoot (in frames)\r\nvar cLimit:int = 12;\r\n\/\/whether or not the user is allowed to shoot\r\nvar shootAllow:Boolean = true;\r\n<\/pre>\n<p>Next, we have to add a timer that will increment the <tt>cTime<\/tt> variable every frame. We won't use the built in <tt>Timer<\/tt> class that ActionScript has (if you even know what that is) because going by frames is much more efficient. Add this code to the <tt>moveChar()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/Incrementing the cTime\r\n\r\n\/\/checking if cTime has reached the limit yet\r\nif(cTime < cLimit){\r\n\tcTime ++;\r\n} else {\r\n\t\/\/if it has, then allow the user to shoot\r\n\tshootAllow = true;\r\n\t\/\/and reset cTime\r\n\tcTime = 0;\r\n}\r\n<\/pre>\n<p>Now we can finally allow the shooting to occur. Add this code to the <tt>checkKeysDown()<\/tt> function.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/checking if the space bar is pressed and shooting is allowed\r\nif(event.keyCode == 32 && shootAllow){\r\n\t\/\/making it so the user can't shoot for a bit\r\n\tshootAllow = false;\r\n\t\/\/declaring a variable to be a new Bullet\r\n\tvar newBullet:Bullet = new Bullet();\r\n\t\/\/changing the bullet's coordinates\r\n\tnewBullet.x = mcMain.x + mcMain.width\/2 - newBullet.width\/2;\r\n\tnewBullet.y = mcMain.y;\r\n\t\/\/then we add the bullet to stage\r\n\taddChild(newBullet);\r\n}\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>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\/pt2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt2\/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 2: Programming the Character Pt 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,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\/608"}],"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=608"}],"version-history":[{"count":8,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/608\/revisions"}],"predecessor-version":[{"id":649,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/608\/revisions\/649"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=608"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}