TransitFade
//////////////////////////////////////////////////////////////////////////////
[AS3.0] TransitLoaderクラスだ! (1)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1022
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vIZH
*/
////////////////////////////////////////////////////////////////////////////////
// [AS3.0] TransitLoaderクラスだ! (1)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1022
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.events.Event;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
private var photoList:Array;
private var transit:TransitLoader;
public function Main() {
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
photoList = new Array();
photoList.push(basePath + "5/59/5947/59475c25ec077563a6f075c89f332f36ddffc305");
photoList.push(basePath + "c/c3/c331/c331d0a4aefdac4c38ea1e4f652aad201cf793ca");
photoList.push(basePath + "c/cc/ccff/ccff5ef8a381810a246b9dd4e8fbdad2e999a347");
photoList.push(basePath + "e/ed/edda/eddac30fdbf622bc82c268d2399382f68da653fd");
photoList.push(basePath + "c/c5/c5a8/c5a8f6fa9cf0edf0fc81cd1dd01590436b0ef41c");
photoList.push(basePath + "b/b7/b788/b78801872e5b439ea3f8f2dceaebd441eee0ca6f");
var fade:Fade = new Fade();
transit = new TransitLoader(fade);
addChild(transit);
transit.x = 72;
transit.y = 112;
transit.dataProvider = photoList;
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class TransitLoader extends Sprite {
private var loaderList:Array;
private var photoList:Array;
private var max:uint;
private var hideID:uint = 0;
private var showID:uint = 0;
private static var interval:uint = 2;
private var transit:*;
public static const COMPLETE:String = "transitComplete";
public function TransitLoader(transition:*) {
transit = transition;
}
public function set dataProvider(list:Array):void {
photoList = list;
max = photoList.length;
initialize();
}
private function initialize():void {
loaderList = new Array();
for (var n:uint = 0; n < max; n++) {
var loader:PhotoLoader = new PhotoLoader();
loaderList.push(loader);
loader.addEventListener(PhotoLoader.INIT, loadInit, false, 0, true);
loader.addEventListener(PhotoLoader.COMPLETE, loadComplete, false, 0, true);
}
load(showID);
}
private function load(id:uint):void {
var loader:PhotoLoader = loaderList[id];
var filePath:String = photoList[id];
loader.load(filePath);
}
private function loadInit(evt:Event):void {
var hide:PhotoLoader = loaderList[hideID];
var show:PhotoLoader = loaderList[showID];
if (hide) addChild(hide);
addChild(show);
transitEffect();
}
private function loadComplete(evt:Event):void {
var hide:PhotoLoader = loaderList[hideID];
var show:PhotoLoader = loaderList[showID];
if (hide) addChild(hide);
addChild(show);
transitEffect();
}
private function transitEffect():void {
var show:PhotoLoader = loaderList[showID];
transit.effect(show);
transit.addEventListener(TransitLoader.COMPLETE, setInterval, false, 0, true);
}
private function setInterval(evt:Event):void {
evt.target.removeEventListener(TransitLoader.COMPLETE, setInterval);
var timer:Timer = new Timer(1000*interval, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, transitPhoto, false, 0, true);
timer.start();
}
private function transitPhoto(evt:TimerEvent):void {
evt.target.removeEventListener(TimerEvent.TIMER_COMPLETE, transitPhoto);
var hide:PhotoLoader = loaderList[hideID];
if (hideID != showID) removeChild(hide);
hideID = showID;
showID ++;
if (showID >= max) showID = 0;
load(showID);
}
}
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.display.Bitmap;
import flash.system.LoaderContext;
class PhotoLoader extends Sprite {
private var loader:Loader;
private var info:LoaderInfo;
public var content:Bitmap;
private var smoothing:Boolean;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function PhotoLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, s:Boolean = false):void {
smoothing = s;
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, true);
info.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(PhotoLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(PhotoLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(PhotoLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
content = Bitmap(info.content);
if (smoothing) content.smoothing = true;
dispatchEvent(new Event(PhotoLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
addChild(loader);
dispatchEvent(new Event(PhotoLoader.COMPLETE));
}
}
import flash.display.Sprite;
import flash.events.Event;
class Fade extends Sprite {
public static var className:String = "Fade";
public var transitName:String = "fade";
private var loader:PhotoLoader;
private static var speed:Number = 0.05;
public static const COMPLETE:String = Event.COMPLETE;
public function Fade() {
}
public function effect(target:PhotoLoader):void {
loader = target;
loader.alpha = 0;
addEventListener(Event.ENTER_FRAME, transit, false, 0, true);
}
private function transit(evt:Event):void {
loader.alpha += speed;
if (loader.alpha >= 1) {
loader.alpha = 1;
removeEventListener(Event.ENTER_FRAME, transit);
dispatchEvent(new Event(TransitLoader.COMPLETE));
}
}
}