Skip to content

Instantly share code, notes, and snippets.

@itoz itoz/gist:1239123
Created Sep 24, 2011

Embed
What would you like to do?
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.Timer;
public class Sample extends Sprite
{
//なんミリ秒ごとによみこむか
private static const LOAD_INTERVAL : Number = 100;
private var loader : Loader;
private var bm : Bitmap;
private var bmd : BitmapData;
private var timer : Timer;
public function Sample()
{
loadStart();
bm = addChild(new Bitmap()) as Bitmap;
}
/**
* ろーどすたーと
*/
private function loadStart() : void
{
removeLoader();//ローダすでにあれば消す
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
loader.load(new URLRequest("http://xxxxxxxxxxxxxxx.png"), new LoaderContext(true));
}
/**
* ろーどかんりょう
*/
private function loaded(event : Event) : void
{
if (bmd != null) bmd.dispose();//前回のBitmapData消す
bmd = (loader.content as Bitmap).bitmapData.clone();//bmdの複製を表示用に保持しておく
bm.bitmapData = bmd;
// 一定時間まち
removeTimer();// すでにタイマーあれば消す
timer = new Timer(LOAD_INTERVAL, 1);
timer.addEventListener(TimerEvent.TIMER, timerHander);
timer.start();
}
/**
* まちおわった 
*/
private function timerHander(event : TimerEvent) : void
{
// 次のロード開始
loadStart();
}
private function removeTimer() : void
{
if (timer != null) {
if (timer.hasEventListener(TimerEvent.TIMER)) {
timer.removeEventListener(TimerEvent.TIMER, timerHander);
}
timer = null;
}
}
private function removeLoader() : void
{
if (loader != null) {
if (loader.hasEventListener(Event.COMPLETE)) {
loader.removeEventListener(Event.COMPLETE, loaded);
}
(loader.content as Bitmap ).bitmapData.dispose();
loader.unload();
loader = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.