{"id":600,"date":"2008-08-23T08:00:18","date_gmt":"2008-08-23T12:00:18","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=600"},"modified":"2022-05-29T08:23:36","modified_gmt":"2022-05-29T12:23:36","slug":"tutorial-make-a-vertical-shooter-in-as3","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-vertical-shooter-in-as3\/","title":{"rendered":"Tutorial: Make a Vertical Shooter in AS3"},"content":{"rendered":"<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<ol>\n<li class=\"c_chap\"><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><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 1: Programming the Character<\/h3>\n<p>Today, we&#8217;re going to make a classic vertical shooter game in ActionScript 3. I hope you learn from it! Let us begin.<\/p>\n<p>The first thing that I&#8217;m going to do is make the background of my game black, so it looks more retro. Then, we&#8217;re going to have to make the frame rate faster, mine will be 24 fps. Also, this will be a vertical shooter, so we probably should make the playing screen less wide. My new dimensions are at 300&#215;400 pixels.<\/p>\n<p>Next, we&#8217;re going to draw a character. Mine will be simple, just a triangle pointing upwards.<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt1\/character.gif\" alt=\"My character\" \/><br \/>\nThe dimensions for it are 30&#215;35 pixels.<\/p>\n<p>Then, we&#8217;re going to turn it into a symbol. After that, we&#8217;re going to call it <tt>mcMain<\/tt> for main character. We will need this so we can reference the guy later. Now we&#8217;re ready to code this sucker. Make a new layer called &#8220;actions&#8221; and add the following code:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/these booleans will check which keys are down\r\nvar leftDown:Boolean = false;\r\nvar upDown:Boolean = false;\r\nvar rightDown:Boolean = false;\r\nvar downDown:Boolean = false;\r\n\/\/how fast the character will be able to go\r\nvar mainSpeed:int = 5;\r\n\r\n\/\/adding a listener to mcMain that will move the character\r\nmcMain.addEventListener(Event.ENTER_FRAME, moveChar);\r\nfunction moveChar(event:Event):void{\r\n\t\/\/checking if the key booleans are true then moving\r\n\t\/\/the character based on the keys\r\n\tif(leftDown){\r\n\t\tmcMain.x -= mainSpeed;\r\n\t}\r\n\tif(upDown){\r\n\t\tmcMain.y -= mainSpeed;\r\n\t}\r\n\tif(rightDown){\r\n\t\tmcMain.x += mainSpeed;\r\n\t}\r\n\tif(downDown){\r\n\t\tmcMain.y += mainSpeed;\r\n\t}\r\n\t\/\/keeping the main character within bounds\r\n\tif(mcMain.x <= 0){\r\n\t\tmcMain.x += mainSpeed;\r\n\t}\r\n\tif(mcMain.y <= 0){\r\n\t\tmcMain.y += mainSpeed;\r\n\t}\r\n\tif(mcMain.x >= stage.stageWidth - mcMain.width){\r\n\t\tmcMain.x -= mainSpeed;\r\n\t}\r\n\tif(mcMain.y >= stage.stageHeight - mcMain.height){\r\n\t\tmcMain.y -= mainSpeed;\r\n\t}\r\n}\r\n\/\/this listener will listen for down keystrokes\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);\r\nfunction checkKeysDown(event:KeyboardEvent):void{\r\n\t\/\/making the booleans true based on the keycode\r\n\t\/\/WASD Keys or arrow keys\r\n\tif(event.keyCode == 37 || event.keyCode == 65){\r\n\t\tleftDown = true;\r\n\t}\r\n\tif(event.keyCode == 38 || event.keyCode == 87){\r\n\t\tupDown = true;\r\n\t}\r\n\tif(event.keyCode == 39 || event.keyCode == 68){\r\n\t\trightDown = true;\r\n\t}\r\n\tif(event.keyCode == 40 || event.keyCode == 83){\r\n\t\tdownDown = true;\r\n\t}\r\n}\r\n\/\/this listener will listen for keys being released\r\nstage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);\r\nfunction checkKeysUp(event:KeyboardEvent):void{\r\n\t\/\/making the booleans false based on the keycode\r\n\tif(event.keyCode == 37 || event.keyCode == 65){\r\n\t\tleftDown = false;\r\n\t}\r\n\tif(event.keyCode == 38 || event.keyCode == 87){\r\n\t\tupDown = false;\r\n\t}\r\n\tif(event.keyCode == 39 || event.keyCode == 68){\r\n\t\trightDown = false;\r\n\t}\r\n\tif(event.keyCode == 40 || event.keyCode == 83){\r\n\t\tdownDown = false;\r\n\t}\r\n}\r\n<\/pre>\n<p>Yes, sometimes with arrow keys, the code does get very repetitive.<\/p>\n<p>Actually, this is all that this part of the tutorial actually requires. Next time, we&#8217;ll make the main character shoot!<\/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-as3\/pt1\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"300\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt1\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/vert-shooter-as3\/pt1\/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 1: Programming the Character Today, we&#8217;re going to make a classic vertical shooter game in ActionScript 3. I hope you learn from it! Let us begin. The first thing that I&#8217;m going to [&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\/600"}],"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=600"}],"version-history":[{"count":9,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":659,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions\/659"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}