TransitBlock
//////////////////////////////////////////////////////////////////////////////
[AS3.0] TransitLoaderクラスだ! (3)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1024
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/v4T4
*/
////////////////////////////////////////////////////////////////////////////////
// [AS3.0] TransitLoaderクラスだ! (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1024
////////////////////////////////////////////////////////////////////////////////
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 block:Block = new Block("++", 40, 40, 8, 6);
transit = new TransitLoader(block);
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 Block extends Sprite {
public static var className:String = "Block";
public var transitName:String = "block";
private var loader:PhotoLoader;
private var type:String;
private var mWidth:uint;
private var mHeight:uint;
private var cols:uint;
private var rows:uint;
private var blocks:Sprite;
private var blockList:Array;
private var completed:uint = 0;
public static const COMPLETE:String = "complete";
public function Block(t:String, w:uint, h:uint, c:uint, r:uint) {
type = t;
mWidth = w;
mHeight = h;
cols = c;
rows = r;
init();
}
private function init():void {
if (!blocks) {
blocks = new Sprite();
blockList = new Array();
createBlocks();
}
}
public function effect(target:PhotoLoader):void {
loader = target;
loader.addChild(blocks);
loader.mask = blocks;
transit()
}
private function transit():void {
for (var n:uint = 0; n < rows*cols; n++) {
var block:BlockMask = blockList[n];
block.effect();
block.addEventListener(Block.COMPLETE, complete, false, 0, true);
}
}
private function complete(evt:Event):void {
evt.target.removeEventListener(Block.COMPLETE, complete);
completed ++;
if (completed >= rows*cols) {
reset();
dispatchEvent(new Event(TransitLoader.COMPLETE));
}
}
private function createBlocks():void {
for (var r:uint = 0; r < rows; r++) {
for (var c:uint = 0; c < cols; c++) {
var xPos:uint;
var yPos:uint;
switch (type) {
case "++" :
xPos = mWidth/2 + mWidth*(c%cols);
yPos = mHeight/2 + mHeight*r;
break;
case "+-" :
xPos = mWidth/2 + mWidth*(c%cols);
yPos = mHeight/2 + mHeight*(rows - r - 1);
break;
case "-+" :
xPos = mWidth/2 + mWidth*((cols - c - 1)%cols);
yPos = mHeight/2 + mHeight*r;
break;
case "--" :
xPos = mWidth/2 + mWidth*((cols - c - 1)%cols);
yPos = mHeight/2 + mHeight*(rows - r - 1);
break;
}
var block:BlockMask = new BlockMask(mWidth, mHeight);
blockList.push(block);
block.x = xPos;
block.y = yPos;
block.generation = r + c;
blocks.addChild(block);
}
}
}
public function reset():void {
if (loader) {
if (loader.contains(blocks)) {
loader.removeChild(blocks);
loader.mask = null;
}
}
close();
}
private function close():void {
for (var n:uint = 0; n < rows*cols; n++) {
var block:BlockMask = blockList[n];
block.close();
block.removeEventListener(Block.COMPLETE, complete);
}
completed = 0;
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class BlockMask extends Sprite {
public var generation:uint;
private var mWidth:uint;
private var mHeight:uint;
private var _scale:Number = 1;
private var timer:Timer;
private static var deceleration:Number = 0.4;
public static const COMPLETE:String = "complete";
public function BlockMask(w:uint, h:uint) {
mWidth = w;
mHeight = h;
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(-mWidth/2, -mHeight/2, mWidth, mHeight);
graphics.endFill();
}
public function effect():void {
scale = 0;
var offset:uint = 300*(generation + 1);
timer = new Timer(offset, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, open, false, 0, true);
timer.start();
}
private function open(evt:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, open);
addEventListener(Event.ENTER_FRAME, transit, false, 0, true);
}
private function transit(evt:Event):void {
scale += (1- scale)*deceleration;
if (Math.abs(1 - scale) < 0.005) {
scale = 1;
removeEventListener(Event.ENTER_FRAME, transit);
dispatchEvent(new Event(BlockMask.COMPLETE));
}
}
public function get scale():Number {
return _scale;
}
public function set scale(param:Number):void {
_scale = param;
scaleX = scaleY = _scale;
}
public function close():void {
scale = 0;
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, open);
timer.stop();
removeEventListener(Event.ENTER_FRAME, transit);
}
}