{"id":1132,"date":"2009-01-17T08:04:34","date_gmt":"2009-01-17T12:04:34","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1132"},"modified":"2022-05-29T08:23:30","modified_gmt":"2022-05-29T12:23:30","slug":"tutorial-make-a-vertical-shooter-in-as2-part-4","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as2-part-4\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS2 &#8211; Part 4"},"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 class=\"c_chap\"><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 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 I feel like it.<\/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\n_root.createEmptyMovieClip('bulletHolder', _root.getNextHighestDepth());\r\n<\/pre>\n<p>The <tt>bulletHolder<\/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. Find where we add the bullet to the stage:<\/p>\n<pre lang=\"actionscript\">\r\n_root.attachMovie('mcBullet', 'Bullet'+bulletID,_root.getNextHighestDepth());\r\n\/\/setting the coordinates of the bullet to be the same as the main character\r\n_root['Bullet'+bulletID]._x = mcMain._x + mcMain._width\/2 - _root['Bullet'+bulletID]._width\/2;\r\n_root['Bullet'+bulletID]._y = mcMain._y;\r\n\t_root['Bullet'+bulletID].onEnterFrame = function(){\r\n\t\/\/giving the bullet some actions\r\n\tthis._y -= 10; \/\/moving the bullet\r\n\tif(this._y < -1 * this._height){\/\/if the bullet goes off stage\r\n\t\t\/\/then destroy it\r\n\t\tthis.removeMovieClip();\r\n\t}\r\n}\r\n<\/pre>\n<p>Change it to this:<\/p>\n<pre lang=\"actionscript\">\r\nbulletHolder.attachMovie('mcBullet', 'Bullet'+bulletID,_root.getNextHighestDepth());\r\n\/\/setting the coordinates of the bullet to be the same as the main character\r\nbulletHolder['Bullet'+bulletID]._x = mcMain._x + mcMain._width\/2 - bulletHolder['Bullet'+bulletID]._width\/2;\r\nbulletHolder['Bullet'+bulletID]._y = mcMain._y;\r\nbulletHolder['Bullet'+bulletID].onEnterFrame = function(){\r\n\t\/\/giving the bullet some actions\r\n\tthis._y -= 10; \/\/moving the bullet\r\n\tif(this._y < -1 * this._height){\/\/if the bullet goes off stage\r\n\t\t\/\/then destroy it\r\n\t\tthis.removeMovieClip();\r\n\t}\r\n}\r\n<\/pre>\n<p>Now we enter complicated territory. Find the <tt>onEnterFrame<\/tt> function we added to the Enemy. Add the following to it:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/run a loop checking if it's touching any bullets\r\nfor(var cBullet:String in _root.bulletHolder){\r\n\t\/\/if it's touching the bullet\r\n\t\/\/we have to use coordinates because hit testing doesn't seem to work\r\n\tif(this._y >= _root.bulletHolder[cBullet]._y-30 && this._y <= _root.bulletHolder[cBullet]._y){\r\n\t\tif(this._x <= _root.bulletHolder[cBullet]._x+5 &#038;&#038; this._x >= _root.bulletHolder[cBullet]._x -35){\r\n\t\t\t\/\/then destroy this guy\r\n\t\t\t_root['en'+enID].removeMovieClip();\r\n\t\t\t\/\/and destroy the bullet\r\n\t\t\t_root.bulletHolder[cBullet].removeMovieClip();\r\n\t\t}\r\n\t}\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>onEnterFrame()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\n\t\t\t\/\/hit testing with the user\r\n\t\t\tif(this._y >= _root.mcMain._y-30 && this._y <= _root.mcMain._y){\r\n\t\t\t\tif(this._x <= _root.mcMain._x+30 &#038;&#038; this._x >= _root.mcMain._x -30){\r\n\t\t\t\t\t\/\/we'll add code here in the next part\r\n\t\t\t\t\t\/\/but for now, we'll just trace something\r\n\t\t\t\t\ttrace('You got hit!');\r\n\t\t\t\t}\r\n\t\t\t}\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-as2\/pt4\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as2\/pt4\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as2\/pt4\/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 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,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\/1132"}],"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=1132"}],"version-history":[{"count":6,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1132\/revisions"}],"predecessor-version":[{"id":1411,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1132\/revisions\/1411"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1132"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}