右と左上からピースが崩れていく
溶けるエフェクト。
右上と左上から溶けていく。
// write as3 code here..
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.Security;
[SWF(width=800, height=600, backgroundColor=0x000000)]
/**
* 溶けるエフェクト。
* 右上と左上から溶けていく。
*/
public class Main05 extends Sprite {
// ----------------------------
// データ
// ----------------------------
// [Embed(source="http://img.f.hatena.ne.jp/images/fotolife/s/siba2260/20081019/20081019220109.jpg")] private var P1:Class;
private var image:Bitmap;
private var loader:Loader;
// ----------------------------
// メンバ変数
// ----------------------------
private var enterFrameCount:int = 0;
private var splitSize:int = 5;
private var meltImageList:Vector.<MeltImage> = new Vector.<MeltImage>();
private var dispImage:Bitmap = new Bitmap(new BitmapData(800, 600, true, 0x00FFFFFF));
// ----------------------------
// 初期化
// ----------------------------
public function Main05() {
Security.loadPolicyFile("http://farm2.static.flickr.com/crossdomain.xml");
var context:LoaderContext = new LoaderContext(true);
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://farm4.static.flickr.com/3180/3115974098_d1b27674ef.jpg?v=0"), context);
}
// ----------------------------
// 内部メソッド
// ----------------------------
private function splitImage(x:Number, y:Number):void {
var bmp:BitmapData = new BitmapData(splitSize, splitSize, true, 0x00FFFFFF);
var mat:Matrix = new Matrix();
mat.translate(-x, -y);
bmp.draw(image, mat);
var tempImage:MeltImage = new MeltImage(bmp);
tempImage.x = image.x + x;
tempImage.y = image.y + y;
meltImageList[meltImageList.length] = tempImage;
var rect:Rectangle = new Rectangle(x, y, splitSize, splitSize);
image.bitmapData.fillRect(rect, 0x00FFFFFF);
}
// ----------------------------
// イベント
// ----------------------------
private function onComplete(event:Event):void {
var loaderInfo:LoaderInfo = event.currentTarget as LoaderInfo;
var loader:Loader = loaderInfo.loader;
var bmp:BitmapData = new BitmapData(loader.width, loader.height, true, 0x00FFFFFF);
bmp.draw(loader);
image = new Bitmap(bmp);
image.x = 400 - image.width/2;
image.y = 300 - image.height/2;
addChild(image);
addChild(dispImage);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
enterFrameCount++;
const bmp:BitmapData = dispImage.bitmapData;
bmp.colorTransform(bmp.rect, new ColorTransform(1, 1, 1, 1, 0, 0, 0, -0x11));
var numSplitX:int = image.width / splitSize;
for (var i:int=0; i<enterFrameCount; i++) {
if (i <= numSplitX/2) {
splitImage(i*splitSize, (enterFrameCount-1-i)*splitSize);
}
if (numSplitX-i > numSplitX/2) {
splitImage((numSplitX-i)*splitSize, (enterFrameCount-1-i)*splitSize);
}
}
for (var j:int=meltImageList.length-1; j>-1; j--) {
var meltImage:MeltImage = meltImageList[j];
meltImage.move();
var mat:Matrix = new Matrix();
mat.translate(meltImage.x, meltImage.y);
bmp.draw(meltImage.bitmapData, mat);
if (meltImage.y > stage.stageHeight) {
meltImageList.splice(j, 1);
}
}
}
}
}
// ------------------------------------
// 内部クラス
// ------------------------------------
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
import flash.filters.BlurFilter;
import flash.events.Event;
import flash.geom.Point;
class MeltImage {
// --------------------------------
// メンバ変数
// --------------------------------
public var x:Number = 0;
public var y:Number = 0;
private var isStage:Boolean = false;
public var bitmapData:BitmapData;
private var speed:Number = Math.random()*10+10;
// --------------------------------
// 初期化
// --------------------------------
public function MeltImage(bitmapData:BitmapData) {
this.bitmapData = bitmapData;
//bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), new BlurFilter(10, 10));
//filters = [new BlurFilter(15, 15)];
}
// --------------------------------
// パブリック
// --------------------------------
public function move():void {
y += speed;
}
}