ActionScript 3 将选择设置为文本字段

// To set selection a textfield, use the following;

var _textInput1:TextField = new TextField();
_textInput1.addEventListener(Event.CHANGE, handleTextChange, false, 0, true);

function handleTextChange(event:Event):void {
			
	switch(event.target){				
		case _textInput1:
			trace("input 1");
			if(_textInput1.text.length == 2) {
				stage.focus = _textInput2;
				_textInput2.setSelection(0, _textInput2.text.length); // or second param equals 0 -> 0,0
			}
		break;
		}
	}

ActionScript 3 定时器与简历

package inca.utils {

import flash.utils.Timer;

public class TimerAdv extends Timer {

private var $__initDelay:Number = 0;
private var $__pauseTime:Number = 0;
private var $__acumulado:Number = 0;
private var $__startingTime:Number = NaN;
private var $__paused:Boolean = false;

public function TimerAdv(delay:Number, repeatCount:uint = 0){
$__initDelay = delay;
super(delay, repeatCount);
}

public function get paused():Boolean{ return $__paused; }

override public function start():void{
if($__paused){
resume();
}else{
$__startingTime = new Date().getTime();
super.start();
}
}

override public function reset():void{
delay = $__initDelay;
$__acumulado = 0;
$__pauseTime = 0;
super.reset();
}

public function pause():void{
if(isNaN($__startingTime)) throw new Error("You can't call TimerAdv.pause() without calling TimerAdv.start() first.")
super.stop();
$__paused = true;
$__pauseTime = new Date().getTime();
}

public function resume():void{
$__paused = false;
delay = Math.max(0, ($__initDelay - (($__pauseTime - $__startingTime) + $__acumulado)));
$__acumulado += ($__pauseTime - $__startingTime);
start();
}

}

}

ActionScript 3 As3初始上限

public static function initialize(t:TextField, normSize:int = 12, largeSize:int = 17, auto_size:Boolean = true):void {
			var upArray:Array = new Array();
			var firstCharFormat:TextFormat = new TextFormat();
			firstCharFormat.size = largeSize;
			var normalFormat:TextFormat = new TextFormat();
			normalFormat.size = normSize;
			if(auto_size){
				t.autoSize = TextFieldAutoSize.LEFT;
			}
			var str:String = t.text;
		    var length:Number = t.text.length;
			for (var j:int = 0; j < length; j++) {
				if (str.charCodeAt(j) > 64 && str.charCodeAt(j) < 91) {
					upArray.push(true); 
				}else {
					upArray.push(false);
				}
			}
			t.text = t.text.toUpperCase();
		    for (var i:int=0; i < length; i++) 
		    {
		      	if (upArray[i] || str.charAt(i) == " ") {
					t.setTextFormat(firstCharFormat, i);
				}else {
					t.setTextFormat(normalFormat, i);
				}
		    }
		}

ActionScript 3 PrintJob基础知识

import flash.printing.*;
import flash.geom.Rectangle;


private function doPrint():void 
{
	var _print:PrintJob = new PrintJob();
	if(_print.start()){
		var contentArea:Rectangle;
		//Use this to specify an area of the MC
		//contentArea = new Rectangle(0,0,550,550);
		_print.addPage(projLayer, contentArea);
		_print.send();
	}
}

ActionScript 3 拖动图层基础知识

private function init():void {		
	theDragClip.addEventListener( MouseEvent.MOUSE_DOWN, handleStartDrag);
	theDragClip.addEventListener( MouseEvent.MOUSE_UP, handleStopDrag);
}

private function handleStartDrag(e:MouseEvent) {
	//if (e.target == stage) { 
		theDragClip.startDrag();
		stage.addEventListener(MouseEvent.MOUSE_UP, handleStopDrag);
	//}
}

private function handleStopDrag(e:MouseEvent){
	theDragClip.stopDrag();
	stage.removeEventListener(MouseEvent.MOUSE_UP, handleStopDrag);
}

ActionScript 3 基本类结构

package {

	// IMPORTS
	import flash.display.*;
	
	// CLASS DEFINITION
	// must match file name (MyClass.as)
	// convention says start with a capital
	public class MyClass extends MovieClip {
		
		// INSTANCE VARIABLES or PROPERTIES
		private var instanceVariable:String = "I'm an instance variable";
		public var myProperty:String = "I could be a property";
		
		// CONSTRUCTOR
		// runs when an instance of MyClass is created
		// must match class name
		public function MyClass():void {
			trace("instanceVariable: "+instanceVariable);
		}
		
		// METHODS
		private function doSomething():void {
			trace("myProperty: "+myProperty);
		}
	}	
}

ActionScript 3 AS3 HTML清理

var copyHTML:String = some_value_from_a_database_or_something_containing_html;
copyHTML = copyHTML.replace(/
/gm, "\n"); // fix double carriage returns 
copyHTML = copyHTML.replace(/’/g, "’"); // fix nasty apostrophes 
copyHTML = copyHTML.replace(/—/g, "—"); // fix evil en-dashes
copyHTML = copyHTML.replace(/–/g, "–"); // fix abominable em-dashes


var tf:TextField = new TextField();
tf.htmlText = copyHTML; // drink a beer

ActionScript 3 Flash:外部资产类别

/* AS3
	Copyright 2009 urbanINFLUENCE.
*/
package org.natemiller.types {
	import fl.motion.easing.*;
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.Timer;
	
	import org.natemiller.events.AssetEvent;
	/*
	*	Generic base class for a loadable asset
	*
	*	@langversion ActionScript 3.0
	*	@playerversion Flash 9.0
	*
	*	@author 
	*	@since  13.02.2009
	*/
	public class Asset implements IEventDispatcher {
		
		//--------------------------------------
		// CLASS CONSTANTS
		//--------------------------------------
		
		//--------------------------------------
		//  CONSTRUCTOR
		//--------------------------------------
		
		public function Asset() {
		}
		
		//--------------------------------------
		//  VARIABLES
		//--------------------------------------
		
		public static var base_path:String = "";
		protected static var _assets:Array = [];
		protected static var _hshAssets:Object = {};
		
		protected var dispatcher:EventDispatcher;
		
		public var data:Object = {};	// public object for dynamic property setting
		private var _assetLoader:Loader;
		private var _path:String;
		private var _hasLoadError:Boolean = false;		// whether an error was triggered while loading
		private var _isLoaded:Boolean = false;				// if the image successfully loaded
		private var _percentLoaded:int = 0;				// the percentage loaded
		private var _timerMonitorProcess:Timer;		// a timer used to monitor load progress
		
		//--------------------------------------
		//  GETTER/SETTERS
		//--------------------------------------
		
		
		public function get content() : Loader { 
			return _assetLoader; 
		}
		
		public function get isLoaded() : Boolean { 
			return _isLoaded; 
		}
		
		public function get percentLoaded() : int { 
			return _percentLoaded; 
		}
		
		//--------------------------------------
		//  PUBLIC METHODS
		//--------------------------------------
		
		/**
		* Initializes the asset instance
		*/
		public function initialize(path:String) : void {
			_path = path;
			_assetLoader = new Loader();
			dispatcher = new EventDispatcher(this);
		} // end initialize
		
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
       dispatcher.addEventListener(type, listener, useCapture, priority);
    }
           
    public function dispatchEvent(evt:Event):Boolean{
       return dispatcher.dispatchEvent(evt);
    }
    
    public function hasEventListener(type:String):Boolean{
       return dispatcher.hasEventListener(type);
    }
    
    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
       dispatcher.removeEventListener(type, listener, useCapture);
    }
                   
    public function willTrigger(type:String):Boolean {
       return dispatcher.willTrigger(type);
    }

		/**
		* public string method
		*/
		public function toString() : String {
			return "[Asset path: " + _path + "]";
		} // end toString
		
		/**
		* Loads the asset into mem
		*/
		public function load() : void {
			if (_isLoaded || _hasLoadError) {
				trace("[Asset.load]: Warning - you've already attempted to load this asset. Halting load execution.");
				return;
			}
			
			
			_assetLoader.contentLoaderInfo.addEventListener(Event.INIT, onAssetLoaded, false, 0, true);
			_assetLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onAssetLoadError, false, 0, true);
			beginAssetLoadProgressMonitor();
			_assetLoader.load( new URLRequest(_path));
		} // end load
		

		//--------------------------------------
		//  EVENT HANDLERS
		//--------------------------------------
		
		/**
		* Called when the photo has completed loading
		*/
		protected function onAssetLoaded(e:Event) : void {
			killAssetLoadProgressMonitor();
			_percentLoaded = 100;
			_isLoaded = true;
			dispatchEvent(new AssetEvent(AssetEvent.LOADED, true, false, this));
			disposeListeners();
		} // end onAssetLoaded
		
		/**
		* Called when the photo has failed to load
		*/
		protected function onAssetLoadError(e:IOErrorEvent) : void {
			trace(e);
			_hasLoadError = true;
			disposeListeners();
		} // end onAssetLoadError
		
		/**
		* Called each time the load progress monitor ticks, updates the load percentage
		*/
		protected function onMonitorLoadTick(e:TimerEvent = null) : void {
			var bLoaded:Number = _assetLoader.contentLoaderInfo.bytesLoaded;
			var bTotal:Number = _assetLoader.contentLoaderInfo.bytesTotal;
			if (bLoaded > 0 && bTotal > 0) {
				_percentLoaded = Math.round(bLoaded / bTotal * 100);
			} else _percentLoaded = 0;
		} // end onMonitorLoadTick
		
		//--------------------------------------
		//  PRIVATE & PROTECTED INSTANCE METHODS
		//--------------------------------------
		
		/**
		* Trashes any listeners no longer needed. Triggered by either an error or load complete event
		*/
		protected function disposeListeners() : void {
			_assetLoader.removeEventListener(Event.INIT, onAssetLoaded);
			_assetLoader.removeEventListener(IOErrorEvent.IO_ERROR, onAssetLoadError);
		} // end disposeListeners
		
		
		/**
		* Starts a monitor process to update the load amount for the photo
		*/
		protected function beginAssetLoadProgressMonitor() : void {
			_timerMonitorProcess = new Timer(100);	// execute check every 100 ms
			_timerMonitorProcess.addEventListener(TimerEvent.TIMER, onMonitorLoadTick, false, 0, true);
			onMonitorLoadTick(); // fires to make sure that this is called at least once
			_timerMonitorProcess.start();
		} // end beginPhotoLoadProgressMonitor
		
		/**
		* Destroys the photo progress monitor
		*/
		protected function killAssetLoadProgressMonitor() : void {
			_timerMonitorProcess.stop();
			_timerMonitorProcess.removeEventListener(TimerEvent.TIMER, onMonitorLoadTick);
		} // end killPhotoLoadProgressMonitor
		
	} // end Asset
	
} // end package


/* AS3
	Copyright 2009 urbanINFLUENCE.
*/
package org.natemiller.events {
	import flash.events.Event;
	import org.natemiller.types.Asset;
	
	/*
	*	Event sublcass used for assets
	*
	*	@langversion ActionScript 3.0
	*	@playerversion Flash 9.0
	*
	*	@author 
	*	@since  13.02.2009
	*/
	public class AssetEvent extends Event {
		
		//--------------------------------------
		// CLASS CONSTANTS
		//--------------------------------------
		
		public static const LOADED : String = "assetLoaded";
		
		//--------------------------------------
		//  CONSTRUCTOR
		//--------------------------------------
		
		public function AssetEvent( type:String, bubbles:Boolean=true, cancelable:Boolean=false, asset:Asset = null ){
			super(type, bubbles, cancelable);		
			_asset = asset;
		}
		
		
		public function get asset() : Asset { 
			return _asset; 
		}
		

		override public function clone() : Event {
			return new AssetEvent(type, bubbles, cancelable, _asset);
		}
		
		private var _asset:Asset;
		
	}
	
}

ActionScript 3 AS3将百分比转换为值

private function percentToValue($percent:Number, $min:Number, $max:Number):Number {
	var myValue:Number;
	var difference:Number = $max - $min;
	myValue = (difference * $percent) + $min;
	return myValue;
}

ActionScript 3 常用颜色值作为常量

private static const RED:uint = 0xFF0000;
private static const GREEN:uint = 0x00FF00;
private static const BLUE:uint = 0x0000FF;
private static const BLACK:uint = 0x000000;
private static const WHITE:uint = 0xFFFFFF;
private static const CYAN:uint = 0x00FFFF;
private static const MAGENTA:uint = 0xFF00FF;
private static const YELLOW:uint = 0xFFFF00;
private static const LIGHT_GREY:uint = 0xCCCCCC;
private static const MID_GREY:uint = 0x999999;
private static const DARK_GREY:uint = 0x666666;