{"id":1230,"date":"2011-02-28T21:30:25","date_gmt":"2011-03-01T04:30:25","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=1230"},"modified":"2014-03-18T23:28:54","modified_gmt":"2014-03-19T06:28:54","slug":"super-simple-particle-effect-actionscript-3-0","status":"publish","type":"post","link":"http:\/\/www.nathalielawhead.com\/candybox\/super-simple-particle-effect-actionscript-3-0","title":{"rendered":"Super Stupidly Simple Particle Effect (Actionscript 3.0)"},"content":{"rendered":"<p>Here&#8217;s a quick particle effect I threw together. Thought I&#8217;d put it up here&#8230; figuring it might help some hapless googler searching for a quick fix, since it&#8217;s a super simple class you can easily reuse or (quite possibly) extend&#8230; probably.<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Mouse_Particle_Effect\/silly_little_particle.zip\" target=\"_blank\">Download source here.<\/a><\/p>\n<p>[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Mouse_Particle_Effect\/silly_little_particle.swf&#8221; height=&#8221;400&#8243; width=&#8221;480&#8243;]<br \/>\n<strong>Mouse over the above area to preview.<\/strong><\/p>\n<p>The AS (Particle.as)<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">package<\/span> \r\n<span style=\"color: #333333\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">flash.utils.Timer<\/span><span style=\"color: #333333\">;<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">flash.events.TimerEvent<\/span><span style=\"color: #333333\">;<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">flash.geom.Point<\/span><span style=\"color: #333333\">;<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">flash.display.MovieClip<\/span><span style=\"color: #333333\">;<\/span>\r\n\r\n\r\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> Particle <span style=\"color: #008800; font-weight: bold\">extends<\/span> MovieClip\r\n\t<span style=\"color: #333333\">{<\/span>\r\n\r\n\t\t<span style=\"color: #008800; font-weight: bold\">protected<\/span> <span style=\"color: #008800; font-weight: bold\">var<\/span> position:<span style=\"color: #333399; font-weight: bold\">Point<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">protected<\/span> <span style=\"color: #008800; font-weight: bold\">var<\/span> vector:<span style=\"color: #333399; font-weight: bold\">Point<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">private<\/span> <span style=\"color: #008800; font-weight: bold\">var<\/span> gravity:<span style=\"color: #333399; font-weight: bold\">int<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">private<\/span> <span style=\"color: #008800; font-weight: bold\">var<\/span> friction:<span style=\"color: #333399; font-weight: bold\">Number<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">private<\/span> <span style=\"color: #008800; font-weight: bold\">var<\/span> maxIterationsAlive:<span style=\"color: #333399; font-weight: bold\">Number<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">private<\/span> <span style=\"color: #008800; font-weight: bold\">var<\/span> iterationsAlive:<span style=\"color: #333399; font-weight: bold\">Number<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">protected<\/span> <span style=\"color: #008800; font-weight: bold\">var<\/span> update_i:<span style=\"color: #333399; font-weight: bold\">Timer<\/span><span style=\"color: #333333\">;<\/span>\r\n\r\n\t\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">function <\/span><span style=\"color: #0066BB; font-weight: bold\">Particle<\/span><span style=\"color: #333333\">(<\/span>p<span style=\"color: #333333\">:<\/span><span style=\"color: #333399; font-weight: bold\">Point<\/span><span style=\"color: #333333\">,<\/span> v<span style=\"color: #333333\">:<\/span><span style=\"color: #333399; font-weight: bold\">Point<\/span><span style=\"color: #333333\">,<\/span> g<span style=\"color: #333333\">:<\/span><span style=\"color: #333399; font-weight: bold\">int<\/span><span style=\"color: #333333\">,<\/span> f<span style=\"color: #333333\">:<\/span><span style=\"color: #333399; font-weight: bold\">Number<\/span><span style=\"color: #333333\">)<\/span>\r\n\t\t<span style=\"color: #333333\">{<\/span>\r\n\t\t\t\r\n\t\t\tposition <span style=\"color: #333333\">=<\/span> p<span style=\"color: #333333\">;<\/span>\r\n\t\t\tvector <span style=\"color: #333333\">=<\/span> v<span style=\"color: #333333\">;<\/span>\r\n\t\t\tgravity <span style=\"color: #333333\">=<\/span> g<span style=\"color: #333333\">;<\/span>\r\n\t\t\tfriction <span style=\"color: #333333\">=<\/span> f<span style=\"color: #333333\">;<\/span>\r\n\t\t\tmaxIterationsAlive <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">10<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t\titerationsAlive <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span><span style=\"color: #333333\">;<\/span>\r\n\r\n\t\t\tupdate_i <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> <span style=\"color: #333399; font-weight: bold\">Timer<\/span><span style=\"color: #333333\">(<\/span><span style=\"color: #0000DD; font-weight: bold\">25<\/span><span style=\"color: #333333\">);<\/span>\r\n\r\n\t\t\tupdate_i<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">addEventListener<\/span><span style=\"color: #333333\">(<\/span>TimerEvent<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">TIMER<\/span><span style=\"color: #333333\">,<\/span> update<span style=\"color: #333333\">);<\/span>\r\n\t\t\tupdate_i<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">start<\/span><span style=\"color: #333333\">();<\/span>\r\n\t\t<span style=\"color: #333333\">}<\/span>\r\n\r\n\t\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">function <\/span><span style=\"color: #0066BB; font-weight: bold\">update<\/span><span style=\"color: #333333\">(<\/span>evt<span style=\"color: #333333\">:<\/span><span style=\"color: #333399; font-weight: bold\">TimerEvent<\/span><span style=\"color: #333333\">):<\/span><span style=\"color: #333399; font-weight: bold\">void<\/span>\r\n\t\t<span style=\"color: #333333\">{<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> <span style=\"color: #333333\">(<\/span>iterationsAlive <span style=\"color: #333333\">&gt;<\/span> maxIterationsAlive<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\r\n\t\t\t\tupdate_i<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">removeEventListener<\/span><span style=\"color: #333333\">(<\/span>TimerEvent<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">TIMER<\/span><span style=\"color: #333333\">,<\/span> update<span style=\"color: #333333\">);<\/span>\r\n\t\t\t\tupdate_i<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">stop<\/span><span style=\"color: #333333\">();<\/span>\r\n\t\t\t\tparent<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">removeChild<\/span><span style=\"color: #333333\">(<\/span><span style=\"color: #008800; font-weight: bold\">this<\/span><span style=\"color: #333333\">);<\/span>\r\n\t\t\t<span style=\"color: #333333\">}<\/span>\r\n\r\n\t\t\t<span style=\"color: #888888\">\/\/the * 10  determines what direction it shoots out of - in this case the side<\/span>\r\n\t\t\tposition<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">x<\/span> <span style=\"color: #333333\">+=<\/span>  vector<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">x<\/span> <span style=\"color: #333333\">+<\/span> Math<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">random<\/span><span style=\"color: #333333\">()<\/span> <span style=\"color: #333333\">*<\/span> <span style=\"color: #0000DD; font-weight: bold\">10<\/span><span style=\"color: #333333\">;<\/span> \r\n\t\t\tposition<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">y<\/span> <span style=\"color: #333333\">+=<\/span>  vector<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">y<\/span> <span style=\"color: #333333\">+<\/span> Math<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">random<\/span><span style=\"color: #333333\">()<\/span> <span style=\"color: #333333\">*<\/span> <span style=\"color: #0000DD; font-weight: bold\">10<\/span><span style=\"color: #333333\">;<\/span>\r\n\r\n\t\t\tvector<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">y<\/span> <span style=\"color: #333333\">+=<\/span>  gravity<span style=\"color: #333333\">;<\/span>\r\n\t\t\tvector<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">x<\/span> <span style=\"color: #333333\">*=<\/span>  friction<span style=\"color: #333333\">;<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888\">\/\/apply positions to the clip<\/span>\r\n\t\t\tx <span style=\"color: #333333\">=<\/span> position<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">x<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t\ty <span style=\"color: #333333\">=<\/span> position<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">y<\/span><span style=\"color: #333333\">;<\/span>\r\n\t\t\t\r\n\t\t\talpha <span style=\"color: #333333\">-=<\/span> <span style=\"color: #333333\">.<\/span><span style=\"color: #0000DD; font-weight: bold\">01<\/span><span style=\"color: #333333\">;<\/span> <span style=\"color: #888888\">\/\/fade<\/span>\r\n\r\n\t\t\titerationsAlive<span style=\"color: #333333\">++;<\/span>\r\n\t\t<span style=\"color: #333333\">}<\/span>\t\t\r\n\t\t\r\n\t<span style=\"color: #333333\">}<\/span>\r\n<span style=\"color: #333333\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>And simply link it to a movieclip instance:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Mouse_Particle_Effect\/silly_little_particle_linkage.jpg\" alt=\"Particle Effect Linkage\" \/><\/p>\n<p>There! A stupid simple particle effect.<\/p>\n<p><a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Mouse_Particle_Effect\/silly_little_particle.zip\" target=\"_blank\">GRAB THE SOURCE HERE<\/a><br \/>\nVisit the <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/\">sourcefiles directory here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a quick particle effect I threw together. Thought I&#8217;d put it up here&#8230; figuring it might help some hapless googler searching for a quick fix, since it&#8217;s a super simple class you can easily reuse or (quite possibly) extend&#8230; probably. Download source here. [swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Mouse_Particle_Effect\/silly_little_particle.swf&#8221; height=&#8221;400&#8243; width=&#8221;480&#8243;] Mouse over the above area to preview. The AS (Particle.as) package { import flash.utils.Timer; import flash.events.TimerEvent; import flash.geom.Point; import flash.display.MovieClip; public class Particle extends MovieClip { protected var position:Point; protected var vector:Point; private var gravity:int; private var friction:Number; private var maxIterationsAlive:Number; private var iterationsAlive:Number; protected var update_i:Timer; public function Particle(p:Point, v:Point, g:int,&#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":[12],"tags":[],"class_list":["post-1230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-resources"],"_links":{"self":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/1230","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=1230"}],"version-history":[{"count":15,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/1230\/revisions"}],"predecessor-version":[{"id":3028,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/1230\/revisions\/3028"}],"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=1230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=1230"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}