2011年11月23日 星期三

[AS3] 殘影效果 → 加上 z 軸的 Tween














1001122_shadowD.swf


import flash.geom.ColorTransform;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Graphics;
import fl.transitions.Tween;
import fl.transitions.Transition;
import fl.transitions.easing.Regular;
import fl.transitions.TweenEvent;
import flash.filters.GlowFilter;
import flash.filters.BlurFilter;

var finishX:Number;
var finishY:Number;
var finishZ:Number;
var duration:Number;
var tweenX:Tween;
var tweenY:Tween ;
var tweenZ:Tween ;

/*若BitmapData建構式的第3個參數透明度設為true,
可讓BitmapData擁有透明度。
當在loopHandler()裡的bmd.colorTransform(rec,ctf)持續作用,
BitmapData可為全透明, 讓下方底色透出*/
var ctf:ColorTransform = new ColorTransform(1, 1, 1, .88);
var bmd:BitmapData = new BitmapData(550,400,true,0xffff00);
/*var bmd:BitmapData = new BitmapData(550,400,false,0xffff00);*/

var bm:Bitmap = new Bitmap(bmd);
var rec:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
addChild(bm);

var motionTarget:Sprite = new Sprite();
motionTarget.x = getNewPos()[0];
motionTarget.x = getNewPos()[1];
motionTarget.graphics.lineStyle(3,0xffffff,1.0);
motionTarget.graphics.beginFill(0xffff00,1.0);
motionTarget.graphics.drawCircle(50,50,20);
motionTarget.graphics.endFill();
var targetContainer:Sprite = new Sprite();
targetContainer.addChild(motionTarget);

/*濾鏡*/
var blurFilter:BlurFilter = new BlurFilter(8,8,1);
var glowFilter:GlowFilter = new GlowFilter(0xffff66,1,32,32,1,1,false,false);
/*targetContainer.filters = [blurFilter,glowFilter];*/
motionTarget.filters = [blurFilter,glowFilter];

addEventListener(Event.ENTER_FRAME,loopHandler);
tweenFunc();

function loopHandler(event:Event):void{
 bmd.colorTransform(rec,ctf);
 bmd.draw(targetContainer);
 return;
}

function tweenFunc():void{
 tweenX = new Tween(motionTarget,"x",Regular.easeOut,motionTarget.x,finishX,duration,true);
 tweenY = new Tween(motionTarget,"y",Regular.easeOut,motionTarget.y,finishY,duration,true);
 tweenZ = new Tween(motionTarget,"z",Regular.easeOut,motionTarget.z,finishZ,duration,true);
 tweenX.addEventListener(TweenEvent.MOTION_FINISH,tweenFinish);
 return;
}

function tweenFinish(event:TweenEvent):void{
 tweenX.removeEventListener(TweenEvent.MOTION_FINISH,tweenFinish);
 getNewPos();
 tweenFunc();
 return;
}

function getNewPos():Array{
 finishX = Math.ceil(Math.random()*(stage.width-motionTarget.width));
 finishY = Math.ceil(Math.random()*(stage.height-motionTarget.height));
 finishZ = Math.ceil(Math.random()*(2000)-800);
 duration = Math.random()*1.8+0.3;
 var _tmpInfo:Array = [finishX,finishY,duration]
 return _tmpInfo;
}


沒有留言: