Thread example 01 : Loading of image
そうめんを使って画像を読み込みサンプル。
*
* 画像はFlickrから、http://www.flickr.com/photos/auburnnewyork/3356082615/
*
* 読み込み状況がtraceされていますので、
* Flash tracerなどを入れている方は覗いてみてください。
*
* そうめん使うとソースが無駄に長い。
*
/*
*
* そうめんを使って画像を読み込みサンプル。
*
* 画像はFlickrから、http://www.flickr.com/photos/auburnnewyork/3356082615/
*
* 読み込み状況がtraceされていますので、
* Flash tracerなどを入れている方は覗いてみてください。
*
* そうめん使うとソースが無駄に長い。
*
*/
package{
import flash.display.Sprite;
import org.libspark.thread.Thread;
import org.libspark.thread.EnterFrameThreadExecutor;
public class ThreadTest01 extends Sprite {
public function ThreadTest01() {
Thread.initialize(new EnterFrameThreadExecutor());
var main:MainThread = new MainThread(this);
main.start();
}
}
}
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import org.libspark.thread.Thread;
import org.libspark.thread.utils.Executor;
import org.libspark.thread.utils.ParallelExecutor;
import org.libspark.thread.threads.display.LoaderThread;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.errors.IOError;
class MainThread extends Thread {
public function MainThread(l:DisplayObjectContainer) {
_layer = l;
}
private var _loader:LoaderThread;
private var _layer:DisplayObjectContainer;
override protected function run():void{
_loader = new LoaderThread(new URLRequest('http://farm4.static.flickr.com/3462/3356082615_b0749265f2.jpg?v=0'));
var tracer:Thread = new ProgressTraceThread(_loader.progress);
trace('begin loading.');
_loader.start();
_loader.join();
tracer.start();
next(executeComplete);
error(IOError, errorHandler);
error(SecurityError, errorHandler);
}
private function executeComplete():void{
trace('complete loading.');
_layer.addChild(_loader.loader);
}
protected override function finalize():void{
_loader = null;
trace('end.');
}
private function errorHandler(e:IOError, t:Thread):void{
trace('error!!');
trace(e.getStackTrace());
next(null);
}
}
import flash.events.Event;
import org.libspark.thread.Thread;
import org.libspark.thread.utils.events.ProgressEvent;
import org.libspark.thread.utils.IProgress;
class ProgressTraceThread extends Thread{
public function ProgressTraceThread(progress:IProgress){
_progress = progress;
}
private var _progress:IProgress;
override protected function run():void{
event(_progress, ProgressEvent.START, startHandler);
}
private function startHandler(e:Event):void{
trace('[Progress] Start...');
event(_progress, ProgressEvent.UPDATE, updateHandler);
event(_progress, ProgressEvent.COMPLETED, completeHandler);
event(_progress, ProgressEvent.FAILED, failedHandler);
event(_progress, ProgressEvent.CANCELED, canceledHandler);
}
private function updateHandler(e:Event):void{
trace('[Progress] ' + (_progress.percent * 100) + '%');
event(_progress, ProgressEvent.UPDATE, updateHandler);
event(_progress, ProgressEvent.COMPLETED, completeHandler);
event(_progress, ProgressEvent.FAILED, failedHandler);
event(_progress, ProgressEvent.CANCELED, canceledHandler);
}
private function completeHandler(e:Event):void{
trace('[Progress] Complete');
}
private function failedHandler(e:Event):void{
trace('[Progress] Failed');
}
private function canceledHandler(e:Event):void{
trace('[Progress] Cancel');
}
}