View Single Post
Old 03-12-2010, 10:13 PM   #4
hey
Senior Member
 
Join Date: Jul 2009
Location: PA
Posts: 132
Default

My idea was to have a third object (titled thing3) deflect the particles around it. This actually wasn't as hard as I'd thought it be, and all I did was reuse some code from an old orbiting project.

It doesn't really work quite as it should yet, but it still looks cool. At the very least, you can get some interesting effects by messing around with the particle's mass. Here's what I did:

ActionScript Code:
package {     import flash.display.BitmapData;     import flash.display.Sprite;     import flash.display.MovieClip;     import flash.events.Event;     import flash.events.MouseEvent;         public class Reclaimer2 extends Sprite     {         private var particlesCurrent:Number = 0;         private var particlesTotal:Number = 30;         private var particleSpeed:Number = 10;         private var particleFadeoutSpeed:Number = .02;                 private var particlesArray:Array;               private var radians:Number;                 private var dx:Number;         private var dy:Number;         private var distSQ:Number;         private var dist:Number;         private var force:Number;         private var thing3Mass:Number = 5000;                 public function Reclaimer2():void         {             init();         }                 private function init():void         {             radians = 180 / Math.PI;             particlesArray = [];                         addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);                         thing1.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);             thing2.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);             //thing 3 in mine is essensially a bigger version of the above two             thing3.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);                         thing1.addEventListener(MouseEvent.MOUSE_UP, onThingUp);             thing2.addEventListener(MouseEvent.MOUSE_UP, onThingUp);             thing3.addEventListener(MouseEvent.MOUSE_UP, onThingUp);         }                 private function onThingDown(event:MouseEvent):void         {             event.currentTarget.startDrag();         }         private function onThingUp(event:MouseEvent):void         {             event.currentTarget.stopDrag();         }                 private function createThing(target1:MovieClip, target2:MovieClip):void         {                   if(particlesTotal <= particlesCurrent)             {                 return;             }             particlesCurrent++;                         var tempParticle:Particle = new Particle();                         tempParticle.x = target1.x + Math.random() * target1.width;             tempParticle.y = target1.y + Math.random() * target1.height;             tempParticle.rotation = Math.random()*360;                         tempParticle.rot = Math.atan2(target1.y - target2.y, target1.x - target2.x);             tempParticle.xSpeed = Math.cos(tempParticle.rot) * radians / particleSpeed;             tempParticle.ySpeed = Math.sin(tempParticle.rot) * radians / particleSpeed;             tempParticle.mass = 1;                         particlesArray.push(tempParticle);                         addChild(tempParticle);                     }                 private function updateParticle():void         {             for (var i = 0; i < particlesArray.length; i++)             {                 var tempParticle:MovieClip = particlesArray[i];                                 tempParticle.x -= tempParticle.xSpeed;                 tempParticle.y -= tempParticle.ySpeed;                  tempParticle.alpha -= particleFadeoutSpeed;                                 dx = tempParticle.x - thing3.x;                 dy = tempParticle.y - thing3.y;                 distSQ = ((dx * dx) + (dy * dy));                 dist = Math.sqrt(distSQ);                 force = thing3Mass * tempParticle.mass/ distSQ;                 tempParticle.x += force * dx / dist;                 tempParticle.y += force * dy / dist;                                 if(tempParticle.hitTestObject(thing2))                 {                     destroyParticle(tempParticle);                 }                 else if (tempParticle.alpha <= 0)                 {                     destroyParticle(tempParticle);                 }                 else if (tempParticle.x < 0)                 {                     destroyParticle(tempParticle);                 }                 else if (tempParticle.x > stage.stageWidth)                 {                     destroyParticle(tempParticle);                 }                 else if (tempParticle.y < 0)                 {                     destroyParticle(tempParticle);                 }                 else if (tempParticle.y > stage.stageHeight)                 {                     destroyParticle(tempParticle);                 }             }         }                 private function destroyParticle(particle:MovieClip):void         {             for (var i = 0; i < particlesArray.length; i++)             {                 var tempParticle:MovieClip = particlesArray[i];                 if (tempParticle == particle)                 {                     particlesCurrent--;                     particlesArray.splice(i,1);                     removeChild(tempParticle);                 }             }         }                 private function onEnterFrameLoop(event:Event):void         {             createThing(thing1, thing2);             updateParticle();         }             }    }

Last edited by hey; 03-13-2010 at 03:21 PM.
hey is offline   Reply With Quote