While i was doing a little AS2.0 project i made this MultiImageLoader class.
This class will load multiple images for you and it will dispatch events.
*NOTE: This class uses the EventDelegate from the asapframework.
———-
USAGE:
var multi:MultiImageLoader = MultiImageLoader.getInstance();
multi.addEventListener( MultiImageLoader.IMAGE_START , onImageStart );
multi.addEventListener( MultiImageLoader.IMAGE_PROGRESS , onImageProgress );
multi.addEventListener( MultiImageLoader.IMAGE_COMPLETE , onImageComplete );
multi.addEventListener( MultiImageLoader.MULTILOADER_COMPLETE, onMultiComplete );
multi.store( “img/img_01.png”, photoContainer_01 ); //Image address and image holder.
multi.store( “img/img_02.png”, photoContainer_02 );
multi.store( “img/img_03.png”, photoContainer_03 );
multi.startLoad();
Events:
- MultiImageLoader.IMAGE_START ;
- MultiImageLoader.IMAGE_PROGRESS ;
- MultiImageLoader.IMAGE_COMPLETE ;
- MultiImageLoader.MULTI_START ;
- MultiImageLoader.MULTI_COMPLETE ;
———-
MultiImageLoader class:
|
1
2
3
4
5
6
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
85
86
| import mx.events.EventDispatcher;
import org.asapframework.events.EventDelegate;
class loaders.MultiImageLoader extends EventDispatcher
{
public static var IMAGE_COMPLETE :String = "imagecomplete";
public static var IMAGE_PROGRESS :String = "imageprogress";
public static var IMAGE_START :String = "imagestart";
public static var MULTILOADER_START :String = "multistart";
public static var MULTILOADER_COMPLETE :String = "multicomplete";
private var l_mcl :MovieClipLoader;
private var o_mcl_listener :Object = new Object;
private var i_counter :Number = 0;
private var a_identifiers :Array = new Array();
private var a_addresses :Array = new Array();
public function MultiImageLoader()
{
build();
};
public function build():Void
{
buildLoadObject();
}
public function store( address:String, container:MovieClip ):Void
{
a_identifiers.push( container );
a_addresses.push ( address );
}
public function startLoad():Void
{
l_mcl.loadClip( a_addresses[i_counter], a_identifiers[i_counter] );
}
private function buildLoadObject():Void
{
l_mcl = new MovieClipLoader();
o_mcl_listener.onLoadStart = EventDelegate.create( this, onStart );
o_mcl_listener.onLoadProgress = EventDelegate.create( this, onProgress );
o_mcl_listener.onLoadInit = EventDelegate.create( this, onInit );
l_mcl.addListener( o_mcl_listener );
}
private function onStart( target:MovieClip ):Void
{
dispatchEvent( { type:MultiImageLoader.IMAGE_START, mc:target } );
}
private function onProgress( target:MovieClip, bytesLoaded:Number, bytesTotal:Number ):Void
{
dispatchEvent( { type:MultiImageLoader.IMAGE_PROGRESS, mc:target, bytesloaded:bytesLoaded, bytestotal:bytesTotal } );
}
private function onInit( target:MovieClip ):Void
{
dispatchEvent( { type: MultiImageLoader.IMAGE_COMPLETE, mc:target } );
if ( i_counter < a_addresses.length-1 )
{
loadNext();
}else {
dispatchEvent( { type: MultiImageLoader.MULTILOADER_COMPLETE } );
}
}
private function loadNext():Void
{
i_counter++;
startLoad();
}
private static var _instance:MultiImageLoader;
public static function getInstance():MultiImageLoader
{
if (!_instance) _instance = new MultiImageLoader();
return _instance;
}
} |