as3

Reclaimer Effect

Friday, March 12th, 2010 | AS3 Effects | No Comments

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

Tags: , , , , ,

OOP: XML Loader & Display Manager

Monday, February 15th, 2010 | AS3 Gadgets | No Comments

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

Tags: , , , , , ,

Center MovieClip based on browser size

Monday, October 19th, 2009 | AS3 Gadgets | No Comments

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

Tags: , , ,

Simple Accordion Menu

Thursday, October 15th, 2009 | AS3 Gadgets | 4 Comments

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

› Continue reading

Tags: , , , ,

Custom number range sliders

Friday, October 9th, 2009 | AS3 Gadgets | No Comments

I needed a custom range slider for selecting prices. It needed to have two draggers. One of low price, one for high price. Here is the result.

Low and High value is calculated based on the size of the slider track.

Easy to use and easy to customize. Only 3 movieclips make up the whole thing. Doesn’t use annoying built in flash components.

› Continue reading

Tags: , , , , , , , ,