{"id":2344,"date":"2012-09-14T18:28:34","date_gmt":"2012-09-15T01:28:34","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=2344"},"modified":"2012-10-29T14:57:19","modified_gmt":"2012-10-29T21:57:19","slug":"super-simple-script-snippets-enemy-projectiles","status":"publish","type":"post","link":"http:\/\/www.nathalielawhead.com\/candybox\/super-simple-script-snippets-enemy-projectiles","title":{"rendered":"Super Simple Script Snippets: Enemy Projectiles"},"content":{"rendered":"<p>I&#8217;m making a game!<br \/>\nThe premise of which is that there will be enemy sprites (like tanks) shooting at you (a spaceship). Enemy projectiles will be fired in short controlled bursts&#8230; that&#8217;s the basic idea. The real thing is a lot more complex&#8230; Imagine short controlled bursts exploding in fireworks that you have to dodge if you don&#8217;t manage to &#8220;deactivate&#8221; them in time with your beam.<\/p>\n<p><strong>Here&#8217;s the example (click &#8220;Fire!&#8221; button):<\/strong><br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/gravity_projectile.swf&#8221; height=&#8221;400&#8243; width=&#8221;480&#8243;]<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/\" target=\"_blank\">DOWNLOAD .FLA HERE<\/a><\/p>\n<p>At any rate I&#8217;m releasing code snippets as I progress.<br \/>\nHopefully these will come in handy for others developing awesome games.<\/p>\n<p><strong>Source code (Actionscript 3 only):<\/strong><\/p>\n<p>import flash.display.MovieClip;<br \/>\nimport flash.events.Event;<br \/>\nimport flash.events.MouseEvent;<\/p>\n<p>var num_stageWidth:Number = stage.stageWidth;<br \/>\nvar num_stageHeight:Number = stage.stageHeight;<\/p>\n<p>var num_fireAmnt:Number = 10;<br \/>\ntxt_amnt.text = num_fireAmnt.toString();<\/p>\n<p>function num_randrange(num_max:Number, num_min:Number = 0)<br \/>\n{<br \/>\n     return Math.ceil(Math.random()*(num_max-num_min)+num_min);<br \/>\n}<\/p>\n<p>\/\/vx = x direction (-n is left n is right)<br \/>\n\/\/vy = y direction (how high note: -n is down n is up)<br \/>\n\/\/gravity = gravity on the projectile<br \/>\n\/\/friction = how slow\/drug down the projectile is<br \/>\nfunction makeFire_boss_tank(vx:Number, vy:Number, gravity:Number, friction:Number){<br \/>\n\t\/\/var mc_boss_clip:MovieClip = MovieClip(stage.getChildByName(&#8220;mc_boss&#8221;));<br \/>\n\tvar mc_boss_clip:MovieClip = mc_boss;\/\/place holder &#8211; comment above in<br \/>\n\tvar mc_bomb:MovieClip = new Fire_Boss_Tank_Bomb();<br \/>\n\tvar arr_data:Array = new Array(vx, vy, gravity, friction);<br \/>\n\t\/\/put<br \/>\n\tstage.addChild(mc_bomb);<br \/>\n\tmc_bomb.data = arr_data;<br \/>\n\t\/\/placement<br \/>\n\tmc_bomb.x = mc_boss.x;<br \/>\n\tmc_bomb.y = mc_boss.y;<br \/>\n\t\/\/start<br \/>\n\tmc_bomb.addEventListener(Event.ENTER_FRAME,moveFire_boss_tank);<br \/>\n}<\/p>\n<p>\/\/trows it up, then down, in a gravity projectile sort of way<br \/>\nfunction moveFire_boss_tank(event:Event){<br \/>\n\tvar clip:MovieClip = MovieClip(event.currentTarget);<br \/>\n\tclip.x += (clip.data[0])\/clip.data[3];<br \/>\n\tclip.y += (clip.data[1])\/clip.data[3];<br \/>\n\tclip.data[1] += clip.data[2];<br \/>\n\t\/\/is it off the stage (even if this isn&#8217;t likely it&#8217;s a good idea&#8230;just incase)<br \/>\n\tif(clip.y>=num_stageHeight){<br \/>\n\t\tclip.removeEventListener(Event.ENTER_FRAME,moveFire_boss_tank);<br \/>\n\t\tstage.removeChild(clip);<br \/>\n\t};<br \/>\n}<\/p>\n<p>function initFire_boss_tank(){<br \/>\n\t\/\/<br \/>\n\tvar num_randFric:Number = num_randrange(1, 3)<br \/>\n\tvar num_randGrav:Number = num_randrange(1, 2);<br \/>\n\tvar num_randvy:Number = num_randrange(-30, -90);<br \/>\n\tvar num_randvx:Number = num_randrange(-15, 15);<br \/>\n\t\/\/<br \/>\n\tmakeFire_boss_tank(num_randvx, num_randvy, num_randGrav, num_randFric);<br \/>\n}<\/p>\n<p>function fire(event:MouseEvent){<br \/>\n\tnum_fireAmnt = int(txt_amnt.text);<br \/>\n\tfor(var i:Number = 0; i<num_fireamnt ; ++i){\n\t\tinitFire_boss_tank();\n\t}\n}\n\nbtn_fire.addEventListener(MouseEvent.CLICK, fire);\n\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/\" target=\"_blank\">Download Source .fla here&#8230;<\/p>\n<p>Or, <a href=\"http:\/\/tetrageddon.com\" target=\"_blank\">visit Tetrageddon Games<\/a> to play some of the end results!<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/noodles\/tetrageddon_0.png\" alt=\"\" \/><\/num_fireamnt><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m making a game! The premise of which is that there will be enemy sprites (like tanks) shooting at you (a spaceship). Enemy projectiles will be fired in short controlled bursts&#8230; that&#8217;s the basic idea. The real thing is a lot more complex&#8230; Imagine short controlled bursts exploding in fireworks that you have to dodge if you don&#8217;t manage to &#8220;deactivate&#8221; them in time with your beam. Here&#8217;s the example (click &#8220;Fire!&#8221; button): [swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/gravity_projectile.swf&#8221; height=&#8221;400&#8243; width=&#8221;480&#8243;] DOWNLOAD .FLA HERE At any rate I&#8217;m releasing code snippets as I progress. Hopefully these will come in handy for others developing awesome&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[30,12],"tags":[],"class_list":["post-2344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","category-resources"],"_links":{"self":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/comments?post=2344"}],"version-history":[{"count":7,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2344\/revisions"}],"predecessor-version":[{"id":2477,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2344\/revisions\/2477"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media?parent=2344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=2344"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=2344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}