public
Description: Flying Saucer as3 helper classes
Clone URL: git://github.com/csessions/as3.git
Tie in Asset class
csessions (author)
Sat Aug 30 23:24:29 -0700 2008
commit  56d6fb035db5bdf59d438b3db868b66afa56fd19
tree    786b3a553b35c0d77459c4431b2629fed25c9b4d
parent  f35aa7f5e0b812a2915fb6c751a19643e030ccce
...
7
8
9
 
10
11
12
13
14
15
16
17
 
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
47
48
49
50
51
52
53
54
 
 
 
 
 
55
56
57
 
 
 
 
58
59
60
61
62
 
63
64
65
66
67
68
 
 
 
69
70
71
 
72
73
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
76
77
78
79
80
 
 
 
 
 
 
 
 
 
 
81
82
83
84
...
7
8
9
10
11
12
 
13
14
15
16
 
17
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
22
23
24
 
 
 
 
 
25
26
27
28
29
30
 
 
31
32
33
34
35
36
37
38
 
39
40
 
 
 
 
 
41
42
43
44
45
 
46
47
 
 
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
63
64
65
 
66
67
68
69
70
71
72
73
74
75
76
77
78
79
0
@@ -7,77 +7,72 @@
0
 
0
 package net.flying_saucer.ui {
0
 
0
+ import gs.TweenMax
0
   import flash.display.*
0
   import flash.events.*
0
- import flash.utils.Dictionary
0
   import net.flying_saucer.event.*
0
   import net.flying_saucer.ui.*
0
   import net.flying_saucer.util.*
0
 
0
- public class Asset extends Pixie {
0
+ public class Asset {
0
 
0
     // Properties
0
     // =========================================================================
0
- private static var assets_queue:Dictionary = new Dictionary(true)
0
-
0
-
0
- // Constructor
0
- // =========================================================================
0
- public function Asset(container:DisplayObjectContainer = null, asset_path:String = null) {
0
- if(container && asset_path) fetch(container, asset_path)
0
- }
0
-
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
-
0
- }
0
- var i:int = 0
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
-
0
- } else i++
0
- }
0
- }
0
+ private static var asset_queue:Array = new Array
0
 
0
     // Public Methods
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
+ l.alpha = 0
0
+ asset_queue.push(l)
0
+ return l
0
     }
0
 
0
     // Event handlers
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
+ ignore_listeners(l)
0
+ l.dispatchEvent(new FsEvent('Asset::LOADED', l))
0
     }
0
     
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
+ }
0
+
0
+ private static function clear_previous(e:Event):void {
0
+ var l:Loader = e.target.loader
0
+ var i:int = 0
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
+ } else i++
0
+ }
0
+ ignore_listeners(l)
0
     }
0
-
0
 
0
     // Private Methods
0
     // =========================================================================
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
+ }
0
+
0
+ private static function remove_asset(l:Loader):void {
0
+ try { l.close() } catch (e:Error) {}
0
+ l.parent.removeChild(l)
0
+ }
0
 
0
   }
0
 }
0
\ No newline at end of file
...
37
38
39
40
 
 
41
42
43
44
45
46
47
 
48
49
 
50
51
52
53
54
55
 
56
57
58
59
 
 
60
61
 
62
63
64
 
 
65
66
67
 
68
69
70
71
 
 
72
73
 
74
75
76
...
145
146
147
148
149
 
 
150
151
152
 
153
154
155
156
157
158
159
160
161
 
 
162
163
164
...
167
168
169
 
170
171
172
...
37
38
39
 
40
41
42
43
44
45
46
47
 
48
49
 
50
51
52
53
54
55
 
56
57
58
 
 
59
60
61
 
62
63
64
 
65
66
67
68
 
69
70
71
 
 
72
73
74
 
75
76
77
78
...
147
148
149
 
 
150
151
152
153
 
154
155
156
157
 
158
 
 
 
 
159
160
161
162
163
...
166
167
168
169
170
171
172
0
@@ -37,40 +37,42 @@ package net.flying_saucer.ui {
0
     // EventDispatcher Helpers
0
     // =========================================================================
0
 
0
- // EventDispatcher.dispatchEvent helper.
0
+ // EventDispatcher.dispatchEvent helper that takes an optional parameter
0
+ // for passing data with an event dispatch.
0
     //
0
     // Usage:
0
     // dispatch([dispatcher:*], event:String, [meta:*])
0
     //
0
     public function dispatch(...args):void {
0
       var dispatcher:* = args[0] is String ? this : args.shift()
0
- var event:String = args.shift()
0
+ var type:String = args.shift()
0
       var meta:* = args.shift()
0
- dispatcher.dispatchEvent(new FsEvent(event, meta))
0
+ dispatcher.dispatchEvent(new FsEvent(type, meta))
0
     }
0
 
0
     // This is a EventDispatcher.removeEventListener helper.
0
     //
0
     // Usage:
0
- // ignore([dispatcher], event, listener_fn)
0
+ // ignore([dispatcher], type, listener)
0
     //
0
     public function ignore(...args):void {
0
- var listener_fn:Function = args.pop()
0
- var event:String = args.pop()
0
+ var listener:Function = args.pop()
0
+ var type:String = args.pop()
0
       var dispatcher:EventDispatcher = args.pop() || this
0
- dispatcher.removeEventListener(event, listener_fn)
0
+ dispatcher.removeEventListener(type, listener, false)
0
     }
0
 
0
- // This is an EventDispatcher.addEventListener helper.
0
+ // This is an EventDispatcher.addEventListener helper that specifies
0
+ // weak references.
0
     //
0
     // Usage:
0
- // listen([dispatcher], event, listener_fn)
0
+ // listen([dispatcher], type, listener)
0
     //
0
     public function listen(...args):void {
0
- var listener_fn:Function = args.pop()
0
- var event:String = args.pop()
0
+ var listener:Function = args.pop()
0
+ var type:String = args.pop()
0
       var dispatcher:EventDispatcher = args.pop() || this
0
- dispatcher.addEventListener(event, listener_fn)
0
+ dispatcher.addEventListener(type, listener, false, 0, true)
0
     }
0
 
0
 
0
@@ -145,20 +147,17 @@ package net.flying_saucer.ui {
0
       }
0
     }
0
         
0
- // Load a swf or bitmap asset. Discards any previously loaded assets once
0
- // a new one is in place.
0
+ // Load a swf or bitmap asset. Optionally discards any previously loaded assets
0
+ // once a new one is in place.
0
     //
0
     // Usage:
0
- // load([recip], asset)
0
+ // load(asset, [discard])
0
     //
0
     // Examples:
0
     // load('asset.swf')
0
- // load(parent_sprite, 'asset.swf')
0
     //
0
- public function load(...args):Asset {
0
- var asset_path:String = args.pop()
0
- var container:DisplayObjectContainer = args.pop() || this
0
- var asset:Asset = add(new Asset(container, asset_path))
0
+ public function load(asset_path:String, discard_previous:Boolean = false):Loader {
0
+ var asset:Loader = add(Asset.load(asset_path, discard_previous))
0
       return asset
0
     }
0
     
0
@@ -167,6 +166,7 @@ package net.flying_saucer.ui {
0
     //
0
     // Usage:
0
     // remove([sprite1], [sprite2...])
0
+ // remove('*')
0
     //
0
     // Examples:
0
     // remove()

Comments

    No one has commented yet.