// 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;
}
}
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();
}
}
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);
}
}
}
/* 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;
}
}