{"id":616,"date":"2008-08-23T08:02:19","date_gmt":"2008-08-23T12:02:19","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=616"},"modified":"2022-05-29T08:23:36","modified_gmt":"2022-05-29T12:23:36","slug":"tutorial-make-a-vertical-shooter-in-as3-part-3","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as3-part-3\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS3 &#8211; Part 3"},"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 class=\"c_chap\"><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 3: Creating the Enemies<\/h3>\n<p>Well, now that we can make our lil&#8217; guy shoot, we have to make something for him to shoot at! I&#8217;m going to first start by just drawing a little enemy guy. It won&#8217;t be too artistic.<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt3\/enemy.gif\" alt=\"My enemy\" \/><br \/>\nIts dimensions are 30&#215;30 if you wanted to know.<\/p>\n<p>Now, do the same thing we did for the bullet, convert it to a MovieClip, and Export it for ActionScript. This time, give it a class of &#8220;Enemy&#8221;. I think you know what&#8217;s next. Now, we have to create another external file for the enemy. Once you&#8217;ve done that, place 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 Eullet will act like a MovieClip\r\n\tpublic class Enemy 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 enemy will move\r\n\t\tprivate var speed:int = 5;\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 Enemy(){\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 > stage.stageHeight){\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>If you didn&#8217;t notice, I just copied and pasted the code from &#8220;Bullet.as&#8221; and edited it around a little bit. You&#8217;ll find that you will do this often when making classes. This code will just make the Enemy just move down the screen. Now, we have to add code to the main timeline that will create it and add it to the stage.<\/p>\n<p>We first have to add 2 variables at the top of our code. They will be similar to <tt>cTime<\/tt> and <tt>cLimit<\/tt> but instead they are for creating enemies. Here&#8217;s the code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/how much time before another enemy is made\r\nvar enemyTime:int = 0;\r\n\/\/how much time needed to make an enemy\r\n\/\/it should be more than the shooting rate\r\n\/\/or else killing all of the enemies would\r\n\/\/be impossible :O\r\nvar enemyLimit:int = 16;\r\n<\/pre>\n<p>Next, we have to create a function that will add the enemies to the stage. We don&#8217;t really want too many <tt>enterFrame<\/tt> functions so we can just add to the <tt>moveChar<\/tt> function. If you want, you can rename it to <tt>eFrameFunctions<\/tt> so that you don&#8217;t get confused on the purpose of the code. Add this to the bottom of that function.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/adding enemies to stage\r\nif(enemyTime < enemyLimit){\r\n\t\/\/if time hasn't reached the limit, then just increment\r\n\tenemyTime ++;\r\n} else {\r\n\t\/\/defining a variable which will hold the new enemy\r\n\tvar newEnemy = new Enemy();\r\n\t\/\/making the enemy offstage when it is created\r\n\tnewEnemy.y = -1 * newEnemy.height;\r\n\t\/\/making the enemy's x coordinates random\r\n\t\/\/the \"int\" function will act the same as Math.floor but a bit faster\r\n\tnewEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));\r\n\t\/\/then add the enemy to stage\r\n\taddChild(newEnemy);\r\n\t\/\/and reset the enemyTime\r\n\tenemyTime = 0;\r\n}\r\n<\/pre>\n<p>This code will constantly add the enemies to the stage, which is exactly what we want. This ends this part of the tutorial. Next time, we'll program the enemies so they get shot!<\/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\/pt3\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt3\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt3\/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 3: Creating the Enemies Well, now that we can make our lil&#8217; guy shoot, we have to make something for him to shoot at! I&#8217;m going to first start by just drawing a [&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\/616"}],"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=616"}],"version-history":[{"count":8,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":1124,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions\/1124"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}