Free ActionScript

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

Reclaimer Effect

This is the result of a little experiment I was trying. Calling this one a “reclaimer” effect. One object pulling another object, one particle at a time.

Sorry but the code is uncommented.

View Code ACTIONSCRIPT
/**
 * VERSION: 1.0
 * DATE: 3/12/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 Reclaimer extends Sprite
	{
		private var particlesCurrent:Number = 0;
		private var particlesTotal:Number = 50;
		private var particleSpeed:Number = 10;
		private var particleFadeoutSpeed:Number = .05;
 
		private var particlesArray:Array;		
		private var radians:Number;
 
		public function Reclaimer():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);
 
			thing1.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
			thing2.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;
 
			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;
 
				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();
		}
 
	}	
}
Download Fla Sample

Download Fla Sample

OOP: XML Loader & Display Manager

Here is an OOP XML Loader & Display Manager class that can be used in your projects to load and display XML data.

The XML comes in the following form:

<?xml version="1.0" encoding="utf-8" ?>
<data>
	<person name="Bob"></person>
	<person name="Jack"></person>
	<person name="Jill"></person>
</data>

Here is the SWF that loads it:

Class structure:
XML Loader & Display Manager

Download Fla Sample

Download Fla Sample

Center MovieClip based on browser size

Put the code below on the first frame of your flash movie. Add a movieclip to stage and give it the instance name “myMovie”. This AS3 code will dynamically center your movieclip based on browser size.

View Code ACTIONSCRIPT
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
 
function resizeHandler(e:Event):void
{
  myMovie.x = (myMovie.stage.stageWidth / 2) - (myMovie.width / 2);
  myMovie.y = (myMovie.stage.stageHeight / 2) - (myMovie.height / 2);
}
 
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, resizeHandler);
 
stage.dispatchEvent(new Event(Event.RESIZE));
//the end

Simple Accordion Menu

This is a very simple accordion menu. It uses the free TweenLite tweener for movement.

Read the rest of this entry »