Flash: External asset class


/ Published in: ActionScript 3
Save to your folder(s)

A simple class and event which can be used to load external assets


Copy this code and paste it in your HTML
  1. /* AS3
  2. Copyright 2009 urbanINFLUENCE.
  3. */
  4. package org.natemiller.types {
  5. import fl.motion.easing.*;
  6. import flash.display.*;
  7. import flash.events.*;
  8. import flash.net.*;
  9. import flash.utils.Timer;
  10.  
  11. import org.natemiller.events.AssetEvent;
  12. /*
  13. * Generic base class for a loadable asset
  14. *
  15. * @langversion ActionScript 3.0
  16. * @playerversion Flash 9.0
  17. *
  18. * @author
  19. * @since 13.02.2009
  20. */
  21. public class Asset implements IEventDispatcher {
  22.  
  23. //--------------------------------------
  24. // CLASS CONSTANTS
  25. //--------------------------------------
  26.  
  27. //--------------------------------------
  28. // CONSTRUCTOR
  29. //--------------------------------------
  30.  
  31. public function Asset() {
  32. }
  33.  
  34. //--------------------------------------
  35. // VARIABLES
  36. //--------------------------------------
  37.  
  38. public static var base_path:String = "";
  39. protected static var _assets:Array = [];
  40. protected static var _hshAssets:Object = {};
  41.  
  42. protected var dispatcher:EventDispatcher;
  43.  
  44. public var data:Object = {}; // public object for dynamic property setting
  45. private var _assetLoader:Loader;
  46. private var _path:String;
  47. private var _hasLoadError:Boolean = false; // whether an error was triggered while loading
  48. private var _isLoaded:Boolean = false; // if the image successfully loaded
  49. private var _percentLoaded:int = 0; // the percentage loaded
  50. private var _timerMonitorProcess:Timer; // a timer used to monitor load progress
  51.  
  52. //--------------------------------------
  53. // GETTER/SETTERS
  54. //--------------------------------------
  55.  
  56.  
  57. public function get content() : Loader {
  58. return _assetLoader;
  59. }
  60.  
  61. public function get isLoaded() : Boolean {
  62. return _isLoaded;
  63. }
  64.  
  65. public function get percentLoaded() : int {
  66. return _percentLoaded;
  67. }
  68.  
  69. //--------------------------------------
  70. // PUBLIC METHODS
  71. //--------------------------------------
  72.  
  73. /**
  74. * Initializes the asset instance
  75. */
  76. public function initialize(path:String) : void {
  77. _path = path;
  78. _assetLoader = new Loader();
  79. dispatcher = new EventDispatcher(this);
  80. } // end initialize
  81.  
  82.  
  83. public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
  84. dispatcher.addEventListener(type, listener, useCapture, priority);
  85. }
  86.  
  87. public function dispatchEvent(evt:Event):Boolean{
  88. return dispatcher.dispatchEvent(evt);
  89. }
  90.  
  91. public function hasEventListener(type:String):Boolean{
  92. return dispatcher.hasEventListener(type);
  93. }
  94.  
  95. public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
  96. dispatcher.removeEventListener(type, listener, useCapture);
  97. }
  98.  
  99. public function willTrigger(type:String):Boolean {
  100. return dispatcher.willTrigger(type);
  101. }
  102.  
  103. /**
  104. * public string method
  105. */
  106. public function toString() : String {
  107. return "[Asset path: " + _path + "]";
  108. } // end toString
  109.  
  110. /**
  111. * Loads the asset into mem
  112. */
  113. public function load() : void {
  114. if (_isLoaded || _hasLoadError) {
  115. trace("[Asset.load]: Warning - you've already attempted to load this asset. Halting load execution.");
  116. return;
  117. }
  118.  
  119.  
  120. _assetLoader.contentLoaderInfo.addEventListener(Event.INIT, onAssetLoaded, false, 0, true);
  121. _assetLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onAssetLoadError, false, 0, true);
  122. beginAssetLoadProgressMonitor();
  123. _assetLoader.load( new URLRequest(_path));
  124. } // end load
  125.  
  126.  
  127. //--------------------------------------
  128. // EVENT HANDLERS
  129. //--------------------------------------
  130.  
  131. /**
  132. * Called when the photo has completed loading
  133. */
  134. protected function onAssetLoaded(e:Event) : void {
  135. killAssetLoadProgressMonitor();
  136. _percentLoaded = 100;
  137. _isLoaded = true;
  138. dispatchEvent(new AssetEvent(AssetEvent.LOADED, true, false, this));
  139. disposeListeners();
  140. } // end onAssetLoaded
  141.  
  142. /**
  143. * Called when the photo has failed to load
  144. */
  145. protected function onAssetLoadError(e:IOErrorEvent) : void {
  146. trace(e);
  147. _hasLoadError = true;
  148. disposeListeners();
  149. } // end onAssetLoadError
  150.  
  151. /**
  152. * Called each time the load progress monitor ticks, updates the load percentage
  153. */
  154. protected function onMonitorLoadTick(e:TimerEvent = null) : void {
  155. var bLoaded:Number = _assetLoader.contentLoaderInfo.bytesLoaded;
  156. var bTotal:Number = _assetLoader.contentLoaderInfo.bytesTotal;
  157. if (bLoaded > 0 && bTotal > 0) {
  158. _percentLoaded = Math.round(bLoaded / bTotal * 100);
  159. } else _percentLoaded = 0;
  160. } // end onMonitorLoadTick
  161.  
  162. //--------------------------------------
  163. // PRIVATE & PROTECTED INSTANCE METHODS
  164. //--------------------------------------
  165.  
  166. /**
  167. * Trashes any listeners no longer needed. Triggered by either an error or load complete event
  168. */
  169. protected function disposeListeners() : void {
  170. _assetLoader.removeEventListener(Event.INIT, onAssetLoaded);
  171. _assetLoader.removeEventListener(IOErrorEvent.IO_ERROR, onAssetLoadError);
  172. } // end disposeListeners
  173.  
  174.  
  175. /**
  176. * Starts a monitor process to update the load amount for the photo
  177. */
  178. protected function beginAssetLoadProgressMonitor() : void {
  179. _timerMonitorProcess = new Timer(100); // execute check every 100 ms
  180. _timerMonitorProcess.addEventListener(TimerEvent.TIMER, onMonitorLoadTick, false, 0, true);
  181. onMonitorLoadTick(); // fires to make sure that this is called at least once
  182. _timerMonitorProcess.start();
  183. } // end beginPhotoLoadProgressMonitor
  184.  
  185. /**
  186. * Destroys the photo progress monitor
  187. */
  188. protected function killAssetLoadProgressMonitor() : void {
  189. _timerMonitorProcess.stop();
  190. _timerMonitorProcess.removeEventListener(TimerEvent.TIMER, onMonitorLoadTick);
  191. } // end killPhotoLoadProgressMonitor
  192.  
  193. } // end Asset
  194.  
  195. } // end package
  196.  
  197.  
  198. /* AS3
  199. Copyright 2009 urbanINFLUENCE.
  200. */
  201. package org.natemiller.events {
  202. import flash.events.Event;
  203. import org.natemiller.types.Asset;
  204.  
  205. /*
  206. * Event sublcass used for assets
  207. *
  208. * @langversion ActionScript 3.0
  209. * @playerversion Flash 9.0
  210. *
  211. * @author
  212. * @since 13.02.2009
  213. */
  214. public class AssetEvent extends Event {
  215.  
  216. //--------------------------------------
  217. // CLASS CONSTANTS
  218. //--------------------------------------
  219.  
  220. public static const LOADED : String = "assetLoaded";
  221.  
  222. //--------------------------------------
  223. // CONSTRUCTOR
  224. //--------------------------------------
  225.  
  226. public function AssetEvent( type:String, bubbles:Boolean=true, cancelable:Boolean=false, asset:Asset = null ){
  227. super(type, bubbles, cancelable);
  228. _asset = asset;
  229. }
  230.  
  231.  
  232. public function get asset() : Asset {
  233. return _asset;
  234. }
  235.  
  236.  
  237. override public function clone() : Event {
  238. return new AssetEvent(type, bubbles, cancelable, _asset);
  239. }
  240.  
  241. private var _asset:Asset;
  242.  
  243. }
  244.  
  245. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.