ActionScript 3 AS3 BianryTreeNode类 - 用于树数据集合

package
{
	import flash.display.Sprite;
	
	/**********************************************************************************************\
	* BinaryTreeNode Class - Designed to give generic object functionality to a binary tree        *
	* node type.                                                                                   *
	* @author : Richard Vacheresse /|\ http://www.rvacheresse.com /|\                              *
	* Licensed for free Commercial and Private use creative commons license agreement.             *
	* The provided code is in an "as-is" state. Richard Vacheresse makes no warranties             *
	* regarding the provided code, and disclaims liability for damages resulting from its use.     *
	* I am also not responsible for baby Smurfs being born only on a blue moon; the way your       *
	* father looks really creeepy like at one of your friends; or for your better half being home  *
	* late last Tuesday night, but man-oh-man she can sure dance. I am most importantly not        *
	* responsible for you reading this far in docs, really?                                        *
	* @version 1.0                                                                                 *
	\**********************************************************************************************/
	public class BinaryTreeNode extends Sprite
	{
		private var element:Sprite;
		private var left:BinaryTreeNode;
		private var right:BinaryTreeNode;
		
		/**
		* BinaryTreeNode(obj:Object):void Constructor. Creates a new
		* tree node with the specified data element being passed to it.
		* @param - Sprite Object
		**/
		public function BinaryTreeNode(obj:Sprite):void
		{
			element = obj;
			left = null;
			right = null;
			trace("\nBinary Tree Node Created");
		}
		
	    /**
        * numChildren():int - Returns the number of non-null children of this node.
        * This method may be able to be written more efficiently.
        * @return  the integer number of non-null children of this node 
        **/
		public function getNumChildren():int
		{
			var children:int = 0;
			
			if(left != null)
				children = (1 + left.getNumChildren());
				
			else if(right != null)
				children += (1 + right.getNumChildren());
			
			return children;
		}
		
		/**
		* toString():String function - Returns a string representation of the
		* node and its children.
		* @return - String Object
		**/
		override public function toString():String
		{
			var result:String = "";
			
			result = ("\nBinary Tree Node: " + 
					  "\n-----------------"+ 
					  "\nThe Main Element is: " +
					  this.element.toString());
			
			if(this.getNumChildren() > 0)
			{
				try
				{
					if(this.left != null)
						result += ("\nThe Left is: " + this.left.toString());
					if(this.right != null)
						result += ("\nThe Right is: " + this.right.toString());
				}
				catch(er:Error)
				{
					trace("Error Caught: " + er);
				}
			}
			
			return result;
		}
		
		//$-------------------------------------------
	    //$              $-SETTERS-$
	    //$-------------------------------------------
				
		/**
		* setElement(spr:Sprite):void function - Sets the element property to passed value.
			**/
		public function setElement(spr:Sprite):void
		{
			this.element = spr;			
			trace("\nMain Element Set to: " + spr);
		}
		
		/**
		* setLeftNode(btNode:BinaryTreeNode):void function - Sets the left binary tree node
		* property to passed value.
		**/
		public function setLeftNode(btNode:BinaryTreeNode):void
		{
			this.left = btNode;
			trace("\nLeft Node Set To: " + btNode);
		}		
		
		/**
		* setRightNode(btNode:BinaryTreeNode):void function - Sets the right binary tree node
		* property to passed value.
		**/
		public function setRightNode(btNode:BinaryTreeNode):void
		{
			this.right = btNode;
			trace("\nRight Node Set To: " + btNode);
		}
		
		//$-------------------------------------------
	    //$              $-GETTERS-$
	    //$-------------------------------------------
		
		/**
		* getElement():Sprite function - Returns the element property value.
		* @return - Sprite Object
	    **/
		public function getElement():Sprite
		{
			return this.element;			
			trace("\nMain Element Returned: " + spr);
		}
		
		/**
		* getLeftNode():BinaryTreeNode function - Returns the left binary tree node
		* property.
		* @return - BinaryTreeNode Object
		**/
		public function getLeftNode():BinaryTreeNode
		{
			reuturn this.left;
			trace("\nLeft Node Returned: " + btNode);
		}		
		
		/**
		* getRightNode():BinaryTreeNode function - Returns the right binary tree node
		* property.
		* @return - BinaryTreeNode Object
		**/
		public function getRightNode():BinaryTreeNode
		{
			return this.right;
			trace("\nRight Node Returned: " + btNode);
		}
	}
}

ActionScript 3 Away3D对象悬停相机功能

//in enterframe function 
         hoverCamera(currentObject); 
          
         //function 
         private function hoverCamera(_currentObject:ObjectContainer3D):void 
         { 
              var mX:Number = this.mouseX > 0 ? this.mouseX : 0; 
              var mZ:Number = this.mouseY > 0 ? this.mouseY : 0; 
               
              var tarX:Number = 3*(mX - stage.stageWidth/2); 
              var tarZ:Number = -2*(mZ - stage.stageHeight/2); 
               
              var dX:Number = camera.x - tarX; 
              var dZ:Number = camera.z - tarZ; 
               
              camera.x -= dX*0.25; 
              camera.z -= dZ*0.25; 
              camera.lookAt(new Number3D(_currentObject.x, 50, _currentObject.z)); 
         }

ActionScript 3 ImageLoader的

package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	public class ImageLoader extends Sprite implements IEventDispatcher
	{
		public var newLoader :			Loader;
		private var newRequest :		URLRequest;
		private var assetsFolder : 		String;
		public var  allowDomainUrl : 	String;
		private var percent: 			Number = 0;
		
		public function ImageLoader()
		{
			
		}
		public function load ( thisURL: String ):void{
			//
			//Security.allowDomain( allowDomainUrl );
			this.newLoader = new Loader();
			
			//Security.allowDomain("*");
			this.newRequest = new URLRequest( thisURL );
			
			this.newLoader.load(this.newRequest);
			/*
				"Grasshopper - Remember to refer to the obj contentLoaderInfo which holds the data as it loads"
			*/
			this.newLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
			this.newLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, checkProgress);
			
		}
		
		public function checkProgress(e:ProgressEvent):void{
			//
			percent = Math.ceil( e.bytesLoaded / e.bytesTotal ) * 100;
			
		}
		public function get progress ( ) : Number {
			return percent;
		}
		public function loaded(e:Event): void {
			//addChild( this.newLoader.content );
		}
	}
}

ActionScript 3 AS3 | ArrayQueue

package kc.tda {
	import kc.api.IQueue;
	import kc.events.CollectionEvent;

	[Event( name="addAll", type="kc.events.CollectionEvent" )]
	[Event( name="clear", type="kc.events.CollectionEvent" )]
	[Event( name="remove", type="kc.events.CollectionEvent" )]
	[Event( name="removeAll", type="kc.events.CollectionEvent" )]
	[Event( name="retain", type="kc.events.CollectionEvent" )]	
	[Event( name="enqueue", type="kc.events.CollectionEvent" )]
	[Event( name="dequeue", type="kc.events.CollectionEvent" )]

	public class ArrayQueue extends ArrayCollection implements IQueue {
		
		// @constructor

		public function ArrayQueue( capacity:int = undefined, expandableCapacity:Boolean = false, loadFactor:Number = NaN ) {
			
			super( capacity, expandableCapacity, loadFactor );
			
			_events = [
				CollectionEvent.ADD_ALL,
				CollectionEvent.CLEAR,
				CollectionEvent.REMOVE,
				CollectionEvent.REMOVE_ALL,
				CollectionEvent.RETAIN,
				CollectionEvent.ENQUEUE,
				CollectionEvent.DEQUEUE				
			];
			
		}

		// @override
		
		override public function add( value:* ):Boolean {			
			return enqueue( value );			
		}
		
		override public function remove( value:* ):Boolean {
			return ( value != null )
				? super.remove( value )
				: ( dequeue() != null )
					? true
					: false;
		}

		// @methods
		
		public function element():* {
			ThrowIsEmpty();
			return _records[0];
		}
		
		public function peek():* {
			try{
				var value:* = element();
			}catch(e:Error){
				return null;
			} return value;
		}
		
		public function enqueue( value:* ):Boolean {
			
			if( ! ResolveStatus() ) {
				return false;
			}
				
			var quantity:int = size();
			_records.push( value ); 
			
			if( size() != quantity ){
				ResolveDispatchEvent( CollectionEvent.ENQUEUE ); 
				return true;
			} return false;
			
		}
		
		public function dequeue():* {
			var quantity:int = size();
			var value:* = _records.shift();
			if( size() != quantity ) {
				ResolveDispatchEvent( CollectionEvent.DEQUEUE );
				return value;
			} return null;
		}
		
	}
	
}

// @INTERFACE


package kc.api {

	public interface IQueue  extends ICollection {
	
		// @methods
	
		function element():*;
		function peek():*;
		function enqueue(value:*):Boolean;
		function dequeue():*;
	
	}
	
}

ActionScript 3 AS3 | ArrayStack

package kc.tda {
	import kc.api.IStack;
	import kc.events.CollectionEvent;

	[Event( name="addAll", type="kc.events.CollectionEvent" )]
	[Event( name="clear", type="kc.events.CollectionEvent" )]
	[Event( name="remove", type="kc.events.CollectionEvent" )]
	[Event( name="removeAll", type="kc.events.CollectionEvent" )]
	[Event( name="retain", type="kc.events.CollectionEvent" )]	
	[Event( name="push", type="kc.events.CollectionEvent" )]
	[Event( name="pop", type="kc.events.CollectionEvent" )]

	public class ArrayStack extends ArrayCollection implements IStack {
		
		// @constructor

		public function ArrayStack( capacity:int = undefined, expandableCapacity:Boolean = false, loadFactor:Number = NaN ) {
			
			super( capacity, expandableCapacity, loadFactor );
			
			_events = [
				CollectionEvent.ADD_ALL,
				CollectionEvent.CLEAR,
				CollectionEvent.REMOVE,
				CollectionEvent.REMOVE_ALL,
				CollectionEvent.RETAIN,
				CollectionEvent.PUSH,
				CollectionEvent.POP				
			];
			
		}
		
		// @override
		
		override public function add( value:* ):Boolean {			
			return push( value );			
		}
		
		override public function remove( value:* ):Boolean {
			return ( value != null )
				? super.remove( value )
				: ( pop() != null )
					? true
					: false;
		}
		
		// @methods
		
		public function element():* {
			ThrowIsEmpty();
			return _records[0];
		}
		
		public function peek():* {
			try{
				var value:* = element();
			}catch(e:Error){
				return null;
			} return value;
		}
		
		public function push( value:* ):Boolean {
			
			if( ! ResolveStatus() ) {
				return false;
			}
				
			var quantity:int = size();
			_records.unshift( value ); 
			
			if( size() != quantity ){
				ResolveDispatchEvent( CollectionEvent.PUSH ); 
				return true;
			} return false;
			
		}
		
		public function pop():* {
			var quantity:int = size();
			var value:* = _records.shift();
			if( size() != quantity ) {
				ResolveDispatchEvent( CollectionEvent.POP );
				return value;
			} return null;
		}
		
	}
	
}

// @INTERFACE

package kc.api {

	public interface IStack extends ICollection {
	
		// @methods
	
		function element():*;
		function peek():*;
		function push(value:*):Boolean;
		function pop():*;
		
	}
	
}

ActionScript 3 AS3 | ArraySet

package kc.tda {
	import kc.api.ICollection;

	[Event( name="add", type="kc.events.CollectionEvent" )]
	[Event( name="addAll", type="kc.events.CollectionEvent" )]
	[Event( name="clear", type="kc.events.CollectionEvent" )]
	[Event( name="remove", type="kc.events.CollectionEvent" )]
	[Event( name="removeAll", type="kc.events.CollectionEvent" )]
	[Event( name="retain", type="kc.events.CollectionEvent" )]

	public class ArraySet extends ArrayCollection implements ICollection {

		// @constructor
		
		public function ArraySet( capacity:int = undefined, expandableCapacity:Boolean = false, loadFactor:Number = NaN ) {
			
			super( capacity, expandableCapacity, loadFactor );
			
		}
		
		// @override
		
		override public function add( value:* ):Boolean {
			return ( contains( value ) == ArrayCollection.NOT_FOUND ) 
				? super.add( value )
				: false;
		}
		
	}
	
}

ActionScript 3 AS3 | KCSingleton

package kc.core {
	import flash.errors.IllegalOperationError;
	import flash.utils.Dictionary;

	public class KCSingleton extends Object {
		
		// @const
		
		public static const ERROR:String = "Illegal instantiation attempted on class of singleton type.";
		
		// @private
			 
		private static var _records:Dictionary;
		
		// @constructor
		
		public function KCSingleton() {
			throw new IllegalOperationError( ERROR );
		}
		
		// @methods
		
		public static function register( api:String, value:Class ):void {
			if( _records == null ) {
				_records = new Dictionary(true);
			} if( ! KCSingleton.getClass( api ) ) {
				_records[api] = value;
			}		
		}
		
		public static function getClass( api:String ):Class {
			if( _records == null ){
				throw new IllegalOperationError("No records.");
			} return _records[api];
		}
		
		public static function getInstance( api:String ):* {
			var instance:Class = KCSingleton.getClass(api);
			if( ! instance ){
				throw new Error("No class registered for interface \""+api+"\".");	
			} return instance["getInstance"]();
		}
		
		public static function purge(...rest):void {
			_records = null;
		}
		
	}
	
}

ActionScript 3 Tweenlite时间线设置

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
OverwriteManager.init(OverwriteManager.AUTO);
TweenPlugin.activate([GlowFilterPlugin, TintPlugin]);

// create some variables used in tweens
var tweenTime:Number = 0.5;
var pauseTime:Number = 0.2;
// create timeline
var myTimeline:TimelineLite = new TimelineLite();
// .append adds tweens onto the end of existing ones - like TweenGroup.ALIGN_SEQUENCE
myTimeline.append(new TweenLite(mc1, tweenTime, {x:100}));
myTimeline.append(new TweenLite(mc2, tweenTime, {y:100}));
myTimeline.append(new TweenLite(nul, pauseTime, {})); // simple PAUSE - use any mc in the tween 
myTimeline.append(new TweenLite(mc1, tweenTime, {x:150}));
myTimeline.append(new TweenLite(mc1, tweenTime, {x:250, delay:-tweenTime})); // use negative delays for simultaneous tweens

ActionScript 3 AS3:分钟到毫秒助手

var fifteenMinutes:Number = minutesToMilliSecs( 15 );

var timer = new Timer( fifteenMinutes );
	timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true );
	timer.start();

private function minutesToMilliSecs( mins:Number ):Number
{
	var milliSecs:Number = mins * 60 * 1000;
	return milliSecs;
}

private function timerHandler( e:TimerEvent ):void
{
	trace( "timerHandler:" )
}

ActionScript 3 最顶级的父装载机信息

var topParent :DisplayObject;
var paramObj  :Object = getLoaderInfo(this).parameters;
for(var varName:String in paramObj) {
	trace("varName: " + varName + " Value: " + paramObj[varName]);
}

function getLoaderInfo(dispObj:DisplayObject):LoaderInfo {
	var root:DisplayObject = getRootDisplayObject(dispObj);
	if (root!=null) {
		return root.loaderInfo;
	}
	return null;
}

function getRootDisplayObject(dispObj:DisplayObject):DisplayObject {
	if (topParent == undefined) {
		if (dispObj.parent!=null) {
			return getRootDisplayObject(dispObj.parent);
		}
		else {
			topParent = dispObj;
			return topParent;
		}
	}
	else {
		return topParent;
	}
}