0
package net.flying_saucer.ui {
0
- import flash.utils.Dictionary
0
import net.flying_saucer.event.*
0
import net.flying_saucer.ui.*
0
import net.flying_saucer.util.*
0
- public class Asset
extends Pixie {
0
// =========================================================================
0
- private static var assets_queue:Dictionary = new Dictionary(true)
0
- // =========================================================================
0
- public function Asset(container:DisplayObjectContainer = null, asset_path:String = null) {
0
- if(container && asset_path) fetch(container, asset_path)
0
- // Public Static Methods
0
- // =========================================================================
0
- public static function clear(container:DisplayObjectContainer, except:Loader = null):void {
0
- Debug.tt('Asset::clear', container, except)
0
- for each(var item:Object in params) {
0
- while(i < container.numChildren) {
0
- var c:* = container.getChildAt(i)
0
- Debug.tt('Asset::clearing', c.name, except.name, c is Loader, c != except)
0
- if(c is Loader && c != except) {
0
- container.removeChild(c)
0
+ private static var asset_queue:Array = new Array
0
// =========================================================================
0
- public function fetch(container:DisplayObjectContainer, asset_path:String):void {
0
- Debug.tt('Asset::load', container, asset_path)
0
- var l:Loader = add(new Loader, {alpha: 0, mouseEnabled: false})
0
- listen(l.contentLoaderInfo, Event.INIT, asset_loaded)
0
- listen(l.contentLoaderInfo, IOErrorEvent.IO_ERROR, asset_failed)
0
+ public static function load(asset_path:String, discard_previous:Boolean = false):Loader {
0
+ var l:Loader = new Loader
0
+ l.contentLoaderInfo.addEventListener(Event.INIT, asset_loaded, false, 0, true)
0
+ l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, asset_failed, false, 0, true)
0
+ if(discard_previous) l.contentLoaderInfo.addEventListener(Event.INIT, clear_previous, false, 0, true)
0
l.load(Url.path(asset_path))
0
- assets_queue[container] ||= new Array
0
- assets_queue[container].push(l)
0
+ l.mouseEnabled = false
0
// =========================================================================
0
- private
function asset_loaded(e:Event):void {
0
+ private
static function asset_loaded(e:Event):void {
0
var l:Loader = e.target.loader
0
- Debug.tt('Asset::asset_loaded', e.target, l)
0
- ignore(l.contentLoaderInfo, Event.INIT, asset_loaded)
0
- ignore(l.contentLoaderInfo, IOErrorEvent.IO_ERROR, asset_failed)
0
- tween(l, {alpha: 1.0, onComplete: clear, onCompleteParams: [l]})
0
- dispatch('Asset::LOADED', this)
0
+ TweenMax.to(l, 0.6, {alpha: 1})
0
+ l.dispatchEvent(new FsEvent('Asset::LOADED', l))
0
- private
function asset_failed(e:IOErrorEvent):void {
0
+ private
static function asset_failed(e:IOErrorEvent):void {
0
Debug.warn('Asset::asset_failed', e)
0
- // ignore(l.contentLoaderInfo, Event.INIT, asset_loaded)
0
- // ignore(l.contentLoaderInfo, IOErrorEvent.IO_ERROR, asset_failed)
0
+ ignore_listeners(e.target.loader)
0
+ private static function clear_previous(e:Event):void {
0
+ var l:Loader = e.target.loader
0
+ while(i < asset_queue.length) {
0
+ var a:Loader = asset_queue[i]
0
+ if(a != l && a.parent == l.parent) {
0
+ TweenMax.to(a, 0.6, {alpha: 0, onComplete: remove_asset, onCompleteParams: [a]})
0
+ asset_queue.splice(i, 1)
0
// =========================================================================
0
+ private static function ignore_listeners(l:Loader):void {
0
+ l.contentLoaderInfo.removeEventListener(Event.INIT, asset_loaded, false)
0
+ l.contentLoaderInfo.removeEventListener(Event.INIT, clear_previous, false)
0
+ l.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, asset_failed, false)
0
+ private static function remove_asset(l:Loader):void {
0
+ try { l.close() } catch (e:Error) {}
0
+ l.parent.removeChild(l)
0
\ No newline at end of file
Comments
No one has commented yet.