Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

Dispenser, Deflector & Puller Particle Effect

1 Comment »

For a lack of a better description, this is the “Dispenser, Deflector & Puller” particle effect.

Dispenser dispenses particles, deflector deflects them, and the puller pulls particles towards itself.

The deflector code was added to my previous Reclaimer Effect by a very nice fellow named “Hey” from the Actionscript.org forums. I just cleaned it up and packaged it. Thanks Hey!

View Code ACTIONSCRIPT
/**
 * Dispenser, Deflector & Puller Particle Effect
 * -----------------------------------------------
 * VERSION: 1.0
 * DATE: 3/15/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package
{
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
 
    public class Main extends Sprite
    {
		// you can play around with these
        private var particlesTotal:Number = 50;
        private var particleSpeed:Number = 10;
        private var particleFadeoutSpeed:Number = .0175;
 
		// don't change these
		private var particlesCurrent:Number = 0;
        private var particlesArray:Array;
        private var radians:Number;
        private var dx:Number;
        private var dy:Number;
        private var distSQ:Number;
        private var forceX:Number;
        private var forceY:Number;
 
        public function Main():void
        {
            init();
        }
 
        private function init():void
        {
            radians = 180 / Math.PI;
            particlesArray = [];
 
            addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);
 
            dispenser_mc.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
            puller_mc.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
            deflector_mc.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
 
            dispenser_mc.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
            puller_mc.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
            deflector_mc.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 - deflector_mc.x;
                dy = tempParticle.y - deflector_mc.y;
                distSQ = ((dx * dx) + (dy * dy));
 
				//force = mass x acceleration
                forceX = tempParticle.mass * tempParticle.xSpeed;
                forceY = tempParticle.mass * tempParticle.ySpeed;
 
				var deflector_mcMass:Number = (deflector_mc.width / 2 + deflector_mc.height / 2);
 
                tempParticle.x += deflector_mcMass * (dx+forceX) / distSQ;
                tempParticle.y += deflector_mcMass * (dy+forceY) / distSQ;
 
                if(tempParticle.hitTestObject(puller_mc))
                {
                    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(dispenser_mc, puller_mc);
            updateParticle();
        }
 
    }
}
Download Fla Sample

Download Fla Sample

One Response

Hello thanks for example and i need something like not deflector but a boundary like a path.
I try to change it but couldn’t and maybe u guys can help me about it.

  • Leave a Reply

    You must be logged in to post a comment.