/* * Copyright(c) 2007 Muraken * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ /** * GroupLoader * * 画像のURLをpushしていくと、BitmapDataの入った配列で返してくれるクラス * @usage * * function test():void{ * var gl:GroupLoader= new GroupLoader(); * gl.addLoad("test1.jpg"); * gl.addLoad("test2.jpg"); * gl.addLoad("test3.jpg"); * gl.addLoad("test4.jpg"); * gl.addEventListener(ProgressEvent.PROGRESS, loadingPhoto); * gl.addEventListener(Event.INIT, loadedPhoto); * gl.start(); * } * function loadingPhoto(e:ProgressEvent):void { * trace(Math.floor((e.bytesLoaded / e.bytesTotal) * 100)); * } * function loadedPhoto(e:Event):void { * var gl:GroupLoader = e.target as GroupLoader; * gl.removeEventListener(ProgressEvent.PROGRESS, loadingPhoto); * gl.removeEventListener(Event.INIT, loadedPhoto); * * //gl.data * //にBitmapDataの入った配列。 * } * * @author むらけん[http://www.muraken.biz/] * @since Flash Player 9 (ActionScript 3.0) * @version 0.1 * @history 2008.2.20 作成 * 2008.3.4 ちょっと修正 */ package biz.muraken.load{ import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.URLRequest; import flash.system.LoaderContext; public class GroupLoader extends EventDispatcher{ private var _loader:Loader; private var dataArray:Array; private var returnArray:Array = new Array(); public function get data():Array { return returnArray; } public function GroupLoader():void { _loader = new Loader(); dataArray = new Array(); } public function addLoad(fileName:String):void{ dataArray.push(fileName); } public function start():void{ var url : URLRequest = new URLRequest(dataArray[returnArray.length]); _loader.contentLoaderInfo.addEventListener (Event.INIT,LoaderInfoInitFunc); _loader.contentLoaderInfo.addEventListener (IOErrorEvent.IO_ERROR,_ioerror); _loader.load(url,new LoaderContext(true)); } private function _ioerror(e:IOErrorEvent):void { dispatchEvent(e); } private function LoaderInfoInitFunc(e:Event):void{ var _bmp:Bitmap = _loader.content as Bitmap; returnArray.push(_bmp.bitmapData); if (dataArray.length == returnArray.length) { _loader.contentLoaderInfo.removeEventListener (IOErrorEvent.IO_ERROR,_ioerror); _loader.contentLoaderInfo.removeEventListener (Event.INIT, LoaderInfoInitFunc); dispatchEvent(new Event(Event.INIT)); }else { dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, returnArray.length, dataArray.length)); start(); } } } }