ActionScript.org Forums

ActionScript.org Forums (http://www.actionscript.org/forums/index.php3)
-   Gaming and Game Development (http://www.actionscript.org/forums/forumdisplay.php3?f=91)
-   -   [AS3] Reclaimer Effect (http://www.actionscript.org/forums/showthread.php3?t=233361)

pradvan 03-12-2010 05:09 PM

[AS3] Reclaimer Effect
 
Was going thru my files and found this little reclaimer effect that I haven't published before. It's basically one object pulling the particles from another object.

Looks something like this:
http://img684.imageshack.us/img684/7387/78398478.jpg


Although you really need to see it in action to understand what it is. Preview & source download here:
http://www.freeactionscript.com/2010...laimer-effect/

hey 03-12-2010 09:16 PM

Looks really interesting! I think I've got a nice idea for something to add to it. Do you mind if use your code to try to make it?

pradvan 03-12-2010 09:19 PM

Quote:

Originally Posted by hey (Post 980492)
Looks really interesting! I think I've got a nice idea for something to add to it. Do you mind if use your code to try to make it?

Use it as you like. If you want to, show us what you come up with :)

hey 03-12-2010 10:13 PM

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();         }             }    }

pradvan 03-13-2010 12:19 PM

Tried your code but didn't see any difference. What am I missing?

hey 03-13-2010 12:31 PM

Should've explained what I did better, but I created a new symbol which was named symbol 2, which was for the most the same as symbol 1 except that I had its x,y cordinate in the center of itself. Then I added one of these to the stage, gave it the instance name of thing3 and set its width and height to 64. When its dragged around into the flow of particles, the particles will appear to be deflected around it.

pradvan 03-13-2010 02:07 PM

That's pretty much what I did, aside from sizes. Problem was, I forgot to save the code after I replaced it with yours! :o Doh!

I love what you did. Looks really cool :)

Would you mind if I release your version on my site as well? Of course with credit for your code.

hey 03-13-2010 02:52 PM

Of course you can. I might mess around with it a bit latter and see what else I can do with it.

Thanks! :)

Edit: Oh yeah, to get it to look right you have to find the right mass to go with the size of thing3. I'll try to figure out a way to relate its mass to the size of it.

Edit2: I changed my code so that it's a bit cleaner.

pradvan 03-13-2010 06:49 PM

I think i figured out how to calculate mass/force based on size. Also changed the center point of all movieclips.

Going to optimize this later and post it:

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 Reclaimer3 extends Sprite     {         private var particlesCurrent:Number = 0;         private var particlesTotal:Number = 50;         private var particleSpeed:Number = 10;         private var particleFadeoutSpeed:Number = .0175;                 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 forceX:Number;         private var forceY:Number;                 public function Reclaimer3():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);             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 createParticle(target1:MovieClip, target2:MovieClip):void         {                  if(particlesTotal <= particlesCurrent)             {                 return;             }             particlesCurrent++;                         var tempParticle:Particle = new Particle();                         tempParticle.x = (target1.x - target1.width / 2) + (Math.random() * target1.width);             tempParticle.y = (target1.y - target1.height / 2) + (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 = tempParticle.width / 2 + tempParticle.height / 2;                         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));                                 //force = mass x acceleration                 forceX = tempParticle.mass * tempParticle.xSpeed;                 forceY = tempParticle.mass * tempParticle.ySpeed;                                 var thing3Mass:Number = (thing3.width / 2 + thing3.height / 2);                                 tempParticle.x += thing3Mass * (dx+forceX) / distSQ;                 tempParticle.y += thing3Mass * (dy+forceY) / distSQ;                                 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         {             createParticle(thing1, thing2);             updateParticle();         }             }    }

pradvan 03-15-2010 03:05 PM

Posted it on my site:
Dispenser, deflector & puller particle effect

http://img532.imageshack.us/img532/8044/95039187.jpg


All times are GMT. The time now is 05:48 AM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger