▼ActionScript 3.0メモ▼
ネットからの画像やテキストの読み込み
ネットから画像やテキストを読み込むFlashアプリを作成する。
素材の準備
以下の2つの素材をネット上に配置。
文字コードはUTF-8。
sample.gif
test.txt これはテストです。
ソースコード
HttpEx.as package { import flash.display.*; import flash.text.*; import flash.utils.*; //ネットからの画像やテキストの読み込み [SWF(width=240, height=240, backgroundColor=0xFFFFFF)] public class HttpEx extends Sprite { private static const ACT_LOAD_TEXT :int = 0; private static const ACT_LOAD_IMAGE:int = 1; //コンストラクタ public function HttpEx() { //テキストの読み込み ByteLoader.load( "http://www.saturn.dti.ne.jp/~npaka/flash/as30/HttpEx/test.txt", null, ACT_LOAD_TEXT, onLoadData); //イメージの読み込み ByteLoader.load( "http://www.saturn.dti.ne.jp/~npaka/flash/as30/HttpEx/sample.gif", null, ACT_LOAD_IMAGE, onLoadData); } //バイトデータ読み込み完了イベントの処理 public function onLoadData(result:int, data:ByteArray, action:int):void { if (result == ByteLoader.SUCCESS) { //バイトデータのテキスト変換 if (action == ACT_LOAD_TEXT) { var label:TextField = new TextField(); label.text = data.toString(); label.x = 10; label.y = 10; label.autoSize = TextFieldAutoSize.LEFT; addChild(label); } //バイトデータのイメージ変換 else if (action == ACT_LOAD_IMAGE) { ImageLoader.load(data, ACT_LOAD_IMAGE, onLoadImage); } } } //イメージ変換完了イベントの処理 public function onLoadImage(result:int, loader:Loader, action:int):void { if (result == ImageLoader.SUCCESS) { //イメージの読み込み if (action == ACT_LOAD_IMAGE) { loader.x = 10; loader.y = 60; addChild(loader); } } } } }
ByteLoader.as package { import flash.events.Event; import flash.events.HTTPStatusEvent; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.events.SecurityErrorEvent; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLStream; import flash.utils.ByteArray; //バイトローダー public class ByteLoader { //定数 public static const SUCCESS :int = 0; public static const IO_ERROR :int = 1; public static const SECURITY_ERROR:int = 2; //情報 private var onLoadData:Function; private var action :int; private var stream :URLStream; public var bytesLoaded:int; public var bytesTotal :int; //コンストラクタ public function ByteLoader(onLoadData:Function) { this.onLoadData = onLoadData; } //ロード public static function load(url:String, data:ByteArray = null, action:int = 0, onLoadData:Function = null):void { var loader:ByteLoader=new ByteLoader(onLoadData); loader.load(url, data, action); } //ロード public function load(url:String, data:ByteArray = null, action:int = 0):void { this.action = action; //リクエスト var req:URLRequest=new URLRequest(url); req.contentType = "application/octet-stream"; if (data == null) { req.method = URLRequestMethod.GET; } else { req.method = URLRequestMethod.POST; req.data = data; } //ストリーム stream = new URLStream(); stream.addEventListener(ProgressEvent.PROGRESS, onProgress); stream.addEventListener(Event.COMPLETE, onComplete); stream.addEventListener(IOErrorEvent.IO_ERROR, onIOError); stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); stream.load(req); } //プログレスイベントの処理 private function onProgress(evt:ProgressEvent):void { bytesLoaded = evt.bytesLoaded; bytesTotal = evt.bytesTotal; trace("progress>" + bytesLoaded + "/" + bytesTotal); } //完了イベントの処理 private function onComplete(evt:Event):void { var data:ByteArray = new ByteArray(); stream.readBytes(data,0,stream.bytesAvailable); if (onLoadData != null) onLoadData(SUCCESS, data, action); } //IOエラーイベントの処理 private function onIOError(evt:IOErrorEvent):void { if (onLoadData != null) onLoadData(IO_ERROR, null, action); } //セキュリティエラーイベントの処理 private function onSecurityError(evt:SecurityErrorEvent):void { if (onLoadData != null) onLoadData(SECURITY_ERROR, null, action); } } }
ImageLoader.as package { import flash.display.Loader; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.net.URLRequest; import flash.net.URLStream; import flash.utils.ByteArray; //イメージローダー public class ImageLoader { //定数 public static const SUCCESS :int = 0; public static const IO_ERROR :int = 1; public static const SECURITY_ERROR:int = 2; //情報 private var onLoadImage:Function; private var action :int; private var loader :Loader; //コンストラクタ public function ImageLoader(onLoadImage:Function) { this.onLoadImage = onLoadImage; } //ロード public static function load(data:ByteArray, action:int, onLoadImage:Function):void { var loader:ImageLoader = new ImageLoader(onLoadImage); loader.load(data, action); } //ロード private function load(data:ByteArray, action:int):void { this.action = action; loader=new Loader(); with(loader.contentLoaderInfo) { addEventListener(Event.COMPLETE, onComplete); addEventListener(IOErrorEvent.IO_ERROR, onIOError); addEventListener(IOErrorEvent.IO_ERROR, onSecurityError); } loader.loadBytes(data); } //完了イベントの処理 private function onComplete(evt:Event):void { if (onLoadImage != null) onLoadImage(SUCCESS, loader, action); } //IOエラーイベントの処理 private function onIOError(evt:IOErrorEvent):void { if (onLoadImage != null) onLoadImage(IO_ERROR, null, action); } //セキュリティエラーイベントの処理 private function onSecurityError(evt:SecurityErrorEvent):void { if (onLoadImage != null) onLoadImage(SECURITY_ERROR, null, action); } } }