Free ActionScript

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

Scroller – Dynamic Scrollbar

Here is a scroller widget. Sized dynamically based on content movieclip size.

Download Fla Sample

Download Fla Sample

AS3 Dynamic Mouse Trailer with Gravity

This is a basic AS3 mouse trailer sparkle effect with gravity.

View Code ACTIONSCRIPT
/**
 * Dynamic Mouse Trailer with Gravity
 * ---------------------
 * VERSION: 1.0
 * DATE: 3/13/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package com.freeactionscript 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	public class MouseTrailer extends Sprite
	{
		// Vars
		private var particleArray:Array;
 
		// Settings
		private var particleMaxSpeed:Number = 1;
		private var particleFadeSpeed:Number = .025;
		private var particleTotal:Number = 3;
		private var particleRange:Number = 25;
		private var particleCurrentAmount:Number = 0;
 
		public function MouseTrailer() 
		{
 
		}
 
		public function init():void
		{
			particleArray = [];
 
			addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		}
 
		private function onEnterFrameLoop(event:Event):void
		{
			updateParticle();
		}
 
		private function mouseMoveHandler(event:Event):void
		{
			createParticle(stage.mouseX, stage.mouseY);
		}
 
		/**
		 * createParticle(target X position, target Y position)
		 */
		function createParticle(targetX:Number, targetY:Number):void
		{
			//run for loop based on particleTotal
			for (var i:Number = 0; i < particleTotal; i++) 
			{
				var particle_mc:MovieClip = new Particle();
 
				//set position & rotation, alpha
				particle_mc.x = targetX
				particle_mc.y = targetY
				particle_mc.rotation = Math.random() * 360;
				particle_mc.alpha = Math.random() * .5 + .5;
 
				//set particle boundry            
				particle_mc.boundyLeft = targetX - particleRange;
				particle_mc.boundyTop = targetY - particleRange;
				particle_mc.boundyRight = targetX + particleRange;
				particle_mc.boundyBottom = targetY + particleRange;
 
				//set speed/direction of fragment
				particle_mc.speedX = Math.random() * particleMaxSpeed - Math.random() * particleMaxSpeed;
				particle_mc.speedY = Math.random() * particleMaxSpeed + Math.random() * particleMaxSpeed;
				particle_mc.speedX *= particleMaxSpeed
				particle_mc.speedY *= particleMaxSpeed
 
				//set fade out speed
				particle_mc.fadeSpeed = Math.random()*particleFadeSpeed;
 
				//just a visual particle counter
				particleCurrentAmount++;
 
				// add to array
				particleArray.push(particle_mc);
 
				// add to display list
				addChild(particle_mc);
			}
		}
 
		private function updateParticle():void
		{
			for (var i = 0; i < particleArray.length; i++)
			{
				var tempParticle:MovieClip = particleArray[i];
 
				//update alpha, x, y
				tempParticle.alpha -= tempParticle.fadeSpeed;
				tempParticle.x += tempParticle.speedX;
				tempParticle.y += tempParticle.speedY;
 
				// if fragment is invisible remove it				
				if (tempParticle.alpha <= 0)
				{
					destroyParticle(tempParticle);
				}
				// if fragment is out of bounds, increase fade out speed
				else if (tempParticle.x < tempParticle.boundyLeft || 
						tempParticle.x > tempParticle.boundyRight || 
						tempParticle.y < tempParticle.boundyTop || 
						tempParticle.y > tempParticle.boundyBottom)
				{
					tempParticle.fadeSpeed += .05;
				}
			}
		}
 
		private function destroyParticle(particle:MovieClip):void
		{
			for (var i = 0; i < particleArray.length; i++)
			{
				var tempParticle:MovieClip = particleArray[i];
				if (tempParticle == particle)
				{
					particleCurrentAmount--;
					particleArray.splice(i,1);
					removeChild(tempParticle);
				}
			}
		}
 
	}
 
}
Download Fla Sample

Download Fla Sample

AS3 Dynamic Mouse Trailer

This is a basic AS3 mouse trailer sparkle effect.

View Code ACTIONSCRIPT
/**
 * Dynamic Mouse Trailer
 * ---------------------
 * VERSION: 1.0
 * DATE: 3/13/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package com.freeactionscript
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
 
	public class MouseTrailer extends Sprite
	{
		// Vars
		private var particleArray:Array;
 
		// Settings
		private var particleMaxSpeed:Number = 1;
		private var particleFadeSpeed:Number = .05;
		private var particleTotal:Number = 5;
		private var particleRange:Number = 25;
		private var particleCurrentAmount:Number = 0;
 
		public function MouseTrailer()
		{
 
		}
 
		public function init():void
		{
			particleArray = [];
 
			addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);
		}
 
		private function onEnterFrameLoop(event:Event):void
		{
			createParticle(stage.mouseX, stage.mouseY);
			updateParticle();
		}
 
		/**
		 * createParticle(target X position, target Y position)
		 */
		function createParticle(targetX:Number, targetY:Number):void
		{
			//run for loop based on particleTotal
			for (var i:Number = 0; i &lt; particleTotal; i++)
			{
				var particle_mc:MovieClip = new Particle();
 
				//set position &amp; rotation, alpha
				particle_mc.x = targetX
				particle_mc.y = targetY
				particle_mc.rotation = Math.random() * 360;
				particle_mc.alpha = Math.random() * .5 + .5;
 
				//set particle boundry
				particle_mc.boundyLeft = targetX - particleRange;
				particle_mc.boundyTop = targetY - particleRange;
				particle_mc.boundyRight = targetX + particleRange;
				particle_mc.boundyBottom = targetY + particleRange;
 
				//set speed/direction of fragment
				particle_mc.speedX = Math.random() * particleMaxSpeed - Math.random() * particleMaxSpeed;
				particle_mc.speedY = Math.random() * particleMaxSpeed - Math.random() * particleMaxSpeed;
				particle_mc.speedX *= particleMaxSpeed
				particle_mc.speedY *= particleMaxSpeed
 
				//set fade out speed
				particle_mc.fadeSpeed = Math.random()*particleFadeSpeed;
 
				//just a visual particle counter
				particleCurrentAmount++;
 
				// add to array
				particleArray.push(particle_mc);
 
				// add to display list
				addChild(particle_mc);
			}
		}
 
		private function updateParticle():void
		{
			for (var i = 0; i &lt; particleArray.length; i++)
			{
				var tempParticle:MovieClip = particleArray[i];
 
				//update alpha, x, y
				tempParticle.alpha -= tempParticle.fadeSpeed;
				tempParticle.x += tempParticle.speedX;
				tempParticle.y += tempParticle.speedY;
 
				// if fragment is invisible remove it
				if (tempParticle.alpha &lt;= 0)
				{
					destroyParticle(tempParticle);
				}
				// if fragment is out of bounds, increase fade out speed
				else if (tempParticle.x &lt; tempParticle.boundyLeft ||
						tempParticle.x &gt; tempParticle.boundyRight ||
						tempParticle.y &lt; tempParticle.boundyTop ||
						tempParticle.y &gt; tempParticle.boundyBottom)
				{
					tempParticle.fadeSpeed += .05;
				}
			}
		}
 
		private function destroyParticle(particle:MovieClip):void
		{
			for (var i = 0; i &lt; particleArray.length; i++)
			{
				var tempParticle:MovieClip = particleArray[i];
				if (tempParticle == particle)
				{
					particleCurrentAmount--;
					particleArray.splice(i,1);
					removeChild(tempParticle);
				}
			}
		}
 
	}
 
}
Download Fla Sample

Download Fla Sample

Dispenser, Deflector & Puller Particle Effect

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 &amp; 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 &lt;= 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 &lt; 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 &lt;= 0)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.x &lt; 0)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.x &gt; stage.stageWidth)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.y &lt; 0)
                {
                    destroyParticle(tempParticle);
                }
                else if (tempParticle.y &gt; stage.stageHeight)
                {
                    destroyParticle(tempParticle);
                }
            }
        }
 
        private function destroyParticle(particle:MovieClip):void
        {
            for (var i = 0; i &lt; 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