MultiLoader 0.3
BulkLoaderをお手本に0から書き直し。
主たる目的としてはBulkLoaderで作ったものをwonderflで公開する時にクラス名をMultiLoaderと書き換えるだけで動くように作る、というもの。なので、今の所必要最小限。
EventListenerまわりをちゃんと作り直したら、だいぶ使い勝手がよくなるはず。今後の課題。
▼Wonderfl
▼ActionScript AS3(FP9)
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
/* MultiLoader 0.3 BulkLoaderをお手本に0から書き直し。 主たる目的としてはBulkLoaderで作ったものを wonderflで公開する時に クラス名をMultiLoaderと書き換えるだけで 動くように作る、というもの。 なので、今の所必要最小限。 EventListenerまわりをちゃんと作り直したら、 だいぶ使い勝手がよくなるはず。今後の課題。 */ package { import flash.display.Bitmap; import flash.display.Sprite; import flash.events.Event; import flash.system.LoaderContext; public class Main extends Sprite { private var multiLoader:MultiLoader; public function Main() { multiLoader = new MultiLoader("main"); multiLoader.add("http://farm3.static.flickr.com/2499/3828917483_8948414d57_o.jpg",{context:new LoaderContext(true)}); multiLoader.add("http://farm3.static.flickr.com/2664/3828917495_8e21ea52c1_o.jpg",{context:new LoaderContext(true)}); multiLoader.add("http://farm4.static.flickr.com/3467/3828917519_d209a026fb_o.jpg",{context:new LoaderContext(true)}); multiLoader.add("http://farm4.static.flickr.com/3542/3829716284_3f77a81e73_o.jpg",{context:new LoaderContext(true)}); multiLoader.add("http://mztm.jp/wonderfl/3d/arss/data.xml"); multiLoader.addEventListener(MultiLoader.COMPLETE, onCompleteHandler); multiLoader.start(); } private function onCompleteHandler(event:Event):void{ var n:int = multiLoader.itemsTotal; for (var i:int = 0; i < n; i++) { if(multiLoader.items[i].type == "image"){ var bitmap:Bitmap = multiLoader.getBitmap(multiLoader.items[i].url.url); bitmap.x = (i%2)*220; bitmap.y = Math.floor(i/2)*220; this.addChild(bitmap); }else if(multiLoader.items[i].type == "text"){ trace(multiLoader.getText(multiLoader.items[i].url.url)); } } } } } /* * Fileローダー * */ import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.events.Event; import flash.events.IOErrorEvent; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.system.LoaderContext; import flash.system.Security; import flash.utils.Dictionary; class MultiLoader{ public static var IMAGE_EXTENSIONS:Array = ["swf", "jpg", "jpeg", "gif", "png"]; public static var TEXT_EXTENSIONS:Array = ["txt", "js", "xml", "php", "asp"]; public static const COMPLETE:String = "complete"; private var _listener:Function = function(event:Event):void{}; private var _loads:Dictionary; private var _loadCount:int; private var _itemsLoaded:int; public var items:Array; public function MultiLoader(name:String){ _loads = new Dictionary(); _itemsLoaded = 0; items = []; } public function add(url:String,props:Object = null):void{ var loadingItem:LoadingItem = new LoadingItem(); loadingItem.url = new URLRequest(url); loadingItem.type = getType(url); if(props){ if(props.context){ loadingItem.context = props.context; } } items.push(loadingItem); } private function getType(url:String):String{ var i:int; var extension:String; var n:int = IMAGE_EXTENSIONS.length; var result:String = ""; for (i = 0; i < n; i++) { extension = IMAGE_EXTENSIONS[i]; if(extension == url.substr(-extension.length).toLowerCase()){ result = "image"; break; } } if(result == ""){ n = TEXT_EXTENSIONS.length; for (i = 0; i < n; i++) { extension = TEXT_EXTENSIONS[i]; if(extension == url.substr(-extension.length).toLowerCase()){ result = "text"; break; } } } return result; } public function start():void{ var n:int = items.length; for (var i:int = 0; i < n; i++) { var type:String = items[i].type; if(type == "image"){ _loads[items[i].url.url] = loadImage(items[i].url,items[i].context); }if(type == "text"){ _loads[items[i].url.url] = loadText(items[i].url); } } } public function addEventListener(type:String,listener:Function):void{ _listener = listener; } public function getBitmap(key:String):Bitmap{ var bitmap:Bitmap = _loads[key].content; return bitmap; } public function getBitmapData(key:String):BitmapData{ var bitmap:Bitmap = getBitmap(key); var bitmapData:BitmapData = new BitmapData(bitmap.width,bitmap.height); bitmapData.draw(bitmap); return bitmapData; } private function loadImage(url:URLRequest,context:LoaderContext = null):Loader { var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComp); loader.load(url,context); return loader; } public function getText(key:String):String{ return _loads[key].data; } private function loadText(url:URLRequest):URLLoader{ var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE,onComp); loader.load(url); return loader; } private function onComp(event:Event):void{ _itemsLoaded ++; if(_itemsLoaded == items.length){ _listener(event); } } public function get itemsTotal():int{ return items.length; } public function get itemsLoaded():int{ return _itemsLoaded; } public function get loadedRatio():Number { return _itemsLoaded/items.length; } } class LoadingItem{ public var url:URLRequest; public var type:String; public var status:String; public var context:LoaderContext; public function LoadingItem(){}; } |