Revision: 19917
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 3, 2009 12:13 by nate63179
Initial Code
/* 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; } }
Initial URL
Initial Description
A simple class and event which can be used to load external assets
Initial Title
Flash: External asset class
Initial Tags
flash
Initial Language
ActionScript 3