{"id":625,"date":"2008-08-23T08:03:06","date_gmt":"2008-08-23T12:03:06","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=625"},"modified":"2022-05-29T08:23:36","modified_gmt":"2022-05-29T12:23:36","slug":"tutorial-make-a-vertical-shooter-in-as3-part-4","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as3-part-4\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS3 &#8211; Part 4"},"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 class=\"c_chap\"><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 4: Programming the Enemies<\/h3>\n<p>Now we have to make it so the enemies can be shot. This probably will be the most complicated of the steps. But, it also is what makes this thing a game. There are two ways that I know of to accomplish this task. The first one is to have the bullet listen for hit testing the enemy, or the enemy listen for the bullet. It&#8217;s basically the same thing. However, I&#8217;m going to have the enemy listen for the bullet, just because.<\/p>\n<p>The first thing we need to do in order to accomplish this feat is to create a container for all of the bullets. This way, we can access all of the bullets through the container. I can&#8217;t really explain this too well, so I&#8217;ll just demonstrate it for you. At the top of the code, add the following:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/this movieclip will hold all of the bullets\r\nvar bulletContainer:MovieClip = new MovieClip();\r\naddChild(bulletContainer);\r\n<\/pre>\n<p>The <tt>bulletContainer<\/tt> is invisible and is just a container for all of the bullets. In order for this to be any use to us, we need to actually add the bullets to this container. This will be easy. Instead of the code where we add the bullet to the stage, <tt>addChild(newBullet);<\/tt>, we just replace it with <tt>bulletContainer.addChild(newBullet)<\/tt>. It&#8217;s as simple as that. But now arises a problem. When we remove the MovieClip from stage, it returns an error. This is simple to fix. Instead of saying just <tt>_root.removeChild(this)<\/tt> in the &#8220;Bullet.as&#8221; file, replace it with <tt>_root.bulletContainer.removeChild(this);<\/tt>. Just another simple quick fix.<\/p>\n<p>Now we enter complicated territory. Go to &#8220;Enemy.as&#8221; and add the following code to the <tt>eFrame()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/checking if it is touching any bullets\r\n\/\/we will have to run a for loop because there will be multiple bullets\r\nfor(var i:int = 0;i<_root.bulletContainer.numChildren;i++){\r\n\t\/\/numChildren is just the amount of movieclips within \r\n\t\/\/the bulletContainer.\r\n\t\r\n\t\/\/we define a variable that will be the bullet that we are currently\r\n\t\/\/hit testing.\r\n\tvar bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);\r\n\r\n\t\/\/now we hit test\r\n\tif(hitTestObject(bulletTarget)){\r\n\t\t\/\/remove this from the stage if it touches a bullet\r\n\t\tremoveEventListener(Event.ENTER_FRAME, eFrame);\r\n\t\t_root.removeChild(this);\r\n\t\t\/\/also remove the bullet and its listeners\r\n\t\t_root.bulletContainer.removeChild(bulletTarget);\r\n\t\tbulletTarget.removeListeners();\r\n\t}\r\n}\r\n<\/pre>\n<p>You might note that we have to run <tt>removeListeners()<\/tt> which isn't a preset ActionScript function. We have to define it in the <tt>Bullet<\/tt> class because there isn't any other way to remove the listeners. Just define the function in the \"Bullet.as\" file.<\/p>\n<pre lang=\"actionscript\">\r\npublic function removeListeners():void{\r\n\tremoveEventListener(Event.ENTER_FRAME, eFrame);\r\n}\r\n<\/pre>\n<p>Now, try out your game. It should work pretty well now. The only problem with it, however, is that it doesn't do anything when it touches the player. We aren't going to really do anything with this right now, but we will set it up so we can make a function for it in the next part of the tutorial, \"Scoring\". Just place the following code in the <tt>Enemy<\/tt>'s <tt>eFrame()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/hit testing with the user\r\nif(hitTestObject(_root.mcMain)){\r\n\t\/\/we'll add code here in the next part\r\n\t\/\/but for now, we'll just trace something\r\n\ttrace('You got hit!');\r\n}\r\n<\/pre>\n<p>Well, that's all for today. Join us next time when we score the game!<\/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\/pt4\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt4\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt4\/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 4: Programming the Enemies Now we have to make it so the enemies can be shot. This probably will be the most complicated of the steps. But, it also is what makes this [&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\/625"}],"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=625"}],"version-history":[{"count":3,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/625\/revisions"}],"predecessor-version":[{"id":651,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/625\/revisions\/651"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}