Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- //import the external files you need
- import com.phillippevk.utils.Stretcher;
- import flash.filters.BlurFilter;
- import flash.display.*;
- import flash.geom.*;
- import flash.events.Event;
- public class CardFlip extends MovieClip{
- //it is recommended that you only edit these two vars
- private var initBlurValue:Number=15; //adjust higher if you want more blur per flip
- private var easingResolve:Number = .2; //controls the default speed of the ease in the last flip (lower=slower)
- //no need to touch the private var below this point
- //blur private variables
- private var initBlur:Number;
- private var blurX:Number = 0;
- private var blurY:Number = 0;
- private var quality:Number = 3;
- //shadow variables
- private var matrix:Matrix;
- private var fillType:String = "linear";
- private var colors:Array = [0x000000, 0x000000];
- private var alphas:Array = [100, 0];
- private var ratios:Array= [30,255];
- //values used in positioning card
- private var mcWIDTH:Number;
- private var mcHEIGHT:Number;
- //private variables used in building your card instance
- private var easing:Number;
- private var dir:String;
- private var turns:Number;
- private var setAlt:Boolean = true;
- private var _mc:MovieClip;
- private var _axis:String;
- private var _seg:Number;
- private var _persp:Number;
- private var _useBlur:Boolean;
- private var _shadLimit:Number;
- private var _useShad:Boolean = true;
- private var _startX:Number;
- private var _startY:Number;
- private var front:MovieClip;
- private var back:MovieClip;
- private var shad_mask:MovieClip;
- private var shad_mc:MovieClip;
- private var img1:MovieClip;
- private var img2:MovieClip;
- private var shad_holder:MovieClip;
- // FLIP GOALS VARS
- private var currFront:Number;
- private var frontSet:Boolean;
- private var dx_start:Number;
- private var scaleNum:Number;
- private var flipNum:Number;
- private var masterEase:Number;
- public function CardFlip(front_img:MovieClip, back_img:MovieClip, axis:String, per:Number, seg:Number, blur:Boolean, shad:Number) {
- //per is the perspective value of yoru card (0-3),
- //seg is the number of segments you want to youse (0-3) to reduce distortion - higher equals heavier file;
- //blur is true/false;
- //shadow is (0-1) representing the max alpha you want your shadow to have;
- img1 = front_img;
- img2 = back_img;
- _mc = new MovieClip();
- addChild(_mc);
- _startX = 0;
- _startY = 0;
- _useBlur = blur;
- _shadLimit = shad;
- if (_shadLimit == 0) _useShad = false;
- _persp = per;
- _seg = seg;
- _axis = axis;
- front = new MovieClip();
- back = new MovieClip();
- img1 = new MovieClip();
- img2 = new MovieClip();
- _mc.addChild(back);
- _mc.addChild(front);
- _mc.addChild(img1);
- _mc.addChild(img2);
- _mc.visible=false;
- if (_useShad){
- shad_mask = new MovieClip();
- shad_mc = new MovieClip();
- shad_holder = new MovieClip();
- }
- loadImage(back_img,back,img2);
- loadImage(front_img,front,img1);
- }
- //this function applies the blur to the current flipped state
- private function blurit(mc:MovieClip):void {
- var filter:BlurFilter = new BlurFilter(blurX, blurY, quality);
- mc.filters = [filter];
- }
- private function createShadow():void {
- _mc.addChild(shad_holder);
- shad_holder.addChild(shad_mc);
- _mc.addChild(shad_mask);
- shad_holder.mouseEnabled = false;
- shad_holder.mouseChildren = false;
- shad_mask.mouseEnabled = false;
- shad_mc.graphics.beginGradientFill(fillType, colors, alphas, ratios, matrix);
- shad_mc.graphics.drawRect(0,0,mcWIDTH,mcHEIGHT);
- shad_mc.graphics.endFill();
- shad_holder.scaleY = 2;
- /*shad_mask.graphics.beginFill(0xFF0000);
- shad_mask.graphics.drawRect(0,0,mcWIDTH,mcHEIGHT);
- shad_mask.graphics.endFill();*/
- if (_axis=="y-axis"){
- shad_holder.graphics.moveTo(0, 0);
- }else if (_axis=="x-axis"){
- shad_holder.rotation = 90;
- }
- shad_holder.mask = shad_mask;
- shad_mc.alpha = 0;
- }
- //center the individual card faces in the card mc and then position the card to the x and y coordinates you specified.
- private function positionCard():void {
- _mc.x = _startX;
- _mc.y = _startY;
- front.x -= mcWIDTH/2;
- front.y -= mcHEIGHT/2;
- back.x -= mcWIDTH/2;
- back.y -= mcHEIGHT/2;
- _mc.visible = true;
- if (_useShad){
- shad_mc.x -= mcWIDTH/2;
- shad_mc.y -= mcHEIGHT/2;
- shad_mask.x -= mcWIDTH/2;
- shad_mask.y -= mcHEIGHT/2;
- matrix = new Matrix();
- matrix.createGradientBox(mcWIDTH, mcHEIGHT, 0, 0, 0);
- createShadow();
- }
- }
- //load the clips
- private function loadImage(clip:MovieClip, tar:MovieClip, img:MovieClip):void {
- trace(clip.width);
- trace(clip.height);
- trace("---");
- var bmd:BitmapData = new BitmapData(clip.width, clip.height, true, 0x00000000);
- bmd.draw(clip);
- var bm:Bitmap = new Bitmap(bmd);
- //addChild(bm);
- //bm.x = 200;
- //bm.y = 200;
- img.addChild(bm);
- var d:Stretcher = new Stretcher(tar, img, _seg, _seg);
- d._smoothing = true;
- img.visible = false;
- if (isNaN(mcWIDTH)) {
- mcWIDTH = img.width;
- mcHEIGHT = img.height;
- } else {
- positionCard();
- }
- d.setTransform(0,0,mcWIDTH,0,0,mcHEIGHT,mcWIDTH,mcHEIGHT);
- }
- //set the values, return to front
- public function flipToFront(direct:String = "cw", ease:Number = 0):void {
- if (isNaN(ease)){
- initBlur = ease*initBlurValue;
- }else{
- initBlur = initBlurValue;
- }
- if (_axis == "y-axis"){
- blurX = initBlur;
- } else if (_axis == "x-axis"){
- blurY = initBlur;
- }
- dir = direct;
- if (front.alpha != 1) {
- flipScript(1, ease);
- }
- }
- //set the values, return to back
- public function flipToBack(direct:String = "cw", ease:Number = 0):void {
- if (isNaN(ease)){
- initBlur = ease*initBlurValue;
- }else{
- initBlur = initBlurValue;
- }
- if (_axis == "y-axis"){
- blurX = initBlur;
- } else if (_axis == "x-axis"){
- blurY = initBlur;
- }
- dir = direct;
- if (front.alpha == 1) {
- flipScript(1, ease);
- }
- }
- //set the values, start the flip
- public function startFlip(direct:String, turns:Number, ease:Number = 0):void {
- if (isNaN(ease)){
- initBlur = ease*initBlurValue;
- }else{
- initBlur = initBlurValue;
- }
- if (_axis == "y-axis"){
- blurX = initBlur;
- } else if (_axis == "x-axis"){
- blurY = initBlur;
- }
- dir = direct;
- flipScript(turns, ease);
- }
- //these next two are called during every frame of the flip.
- //they set the values of the images as your card turns.
- private function goals_v1():void {
- if (_axis=="y-axis"){
- _mc.x0_goal = 0;
- _mc.x1_goal = mcWIDTH;
- _mc.x2_goal = mcWIDTH;
- _mc.x3_goal = 0;
- _mc.x0_pt = mcWIDTH;
- _mc.x1_pt = 0;
- _mc.x2_pt = 0;
- _mc.x3_pt = mcWIDTH;
- } else if (_axis=="x-axis"){
- _mc.y0_goal = 0;
- _mc.y1_goal = mcHEIGHT;
- _mc.y2_goal = mcHEIGHT;
- _mc.y3_goal = 0;
- _mc.y0_pt = mcHEIGHT;
- _mc.y1_pt = 0;
- _mc.y2_pt = 0;
- _mc.y3_pt = mcHEIGHT;
- }
- }
- private function goals_v2():void {
- if (_axis=="y-axis"){
- _mc.x0_goal = mcWIDTH;
- _mc.x1_goal = 0;
- _mc.x2_goal = 0;
- _mc.x3_goal = mcWIDTH;
- _mc.x0_pt = 0;
- _mc.x1_pt = mcWIDTH;
- _mc.x2_pt = mcWIDTH;
- _mc.x3_pt = 0;
- } else if (_axis=="x-axis"){
- _mc.y0_goal = mcHEIGHT;
- _mc.y1_goal = 0;
- _mc.y2_goal = 0;
- _mc.y3_goal = mcHEIGHT;
- _mc.y0_pt = 0;
- _mc.y1_pt = mcHEIGHT;
- _mc.y2_pt = mcHEIGHT;
- _mc.y3_pt = 0;
- }
- }
- //the function that calls the previous functions
- private function setGoals(num:Number):void {
- if (front.alpha == 0) {
- goals_v1();
- } else {
- goals_v2();
- }
- }
- //set the final ease, the goal (depending on which turn you are on), and call the engine
- private function flipScript(turnsVar:Number, ease:Number):void {
- //set turns
- if (isNaN(turnsVar)){
- turns = 1;
- }else{
- turns = turnsVar;
- }
- //set ease
- if (turns >= 1){
- easing = (turns>1) ? .6+(turns/100) : easingResolve;
- }else{
- easing = easingResolve
- }
- masterEase = easing;
- //this is run only once
- if (setAlt) {
- if (_axis=="y-axis"){
- _mc.y0_goal = 0;
- _mc.y1_goal = 0;
- _mc.y2_goal = mcHEIGHT;
- _mc.y3_goal = mcHEIGHT;
- _mc.y0_pt = 0;
- _mc.y1_pt = 0;
- _mc.y2_pt = mcHEIGHT;
- _mc.y3_pt = mcHEIGHT;
- setAlt = false;
- }else if (_axis=="x-axis"){
- _mc.x0_goal = mcWIDTH;
- _mc.x1_goal = mcWIDTH;
- _mc.x2_goal = 0;
- _mc.x3_goal = 0;
- _mc.x0_pt = mcWIDTH;
- _mc.x1_pt = mcWIDTH;
- _mc.x2_pt = 0;
- _mc.x3_pt = 0;
- setAlt = false;
- }
- }
- //set goal
- setGoals(turns);
- //call engine
- flipGoals(masterEase);
- }
- //the actual flipping engine
- private function flipGoals(ease:Number):void {
- frontSet = true;
- currFront = front.alpha;
- scaleNum = 1;
- flipNum = 0;
- if (_useShad){
- if (_axis=="y-axis"){
- if (mcWIDTH > mcHEIGHT) scaleNum = mcWIDTH/mcHEIGHT;
- if (dir=="ccw"){
- shad_holder.scaleX = scaleNum
- }else{
- shad_holder.scaleX= -scaleNum
- }
- }
- if (_axis == "x-axis"){
- if (mcHEIGHT > mcWIDTH) scaleNum = mcHEIGHT/mcWIDTH;
- if (dir == "ccw"){
- shad_holder.scaleX = scaleNum
- }else{
- shad_holder.scaleX = -scaleNum
- }
- }
- }
- //create and start engine
- _mc.addEventListener(Event.ENTER_FRAME,flipEngine);
- }
- private function flipEngine(e:Event):void{
- //set and use blur
- if (_useBlur) {
- if (turns == 1) {
- if (_axis=="y-axis"){
- blurX += (0-blurX)*.1;
- if (blurX<2) {
- blurX = 0;
- }
- }else if (_axis=="x-axis"){
- blurY += (0-blurY)*.1;
- if (blurY<2) {
- blurY = 0;
- }
- }
- }
- blurit(_mc);
- }
- //set x and y values of your distorted cards
- var i:uint = 0;
- for (i; i<=3; i++) {
- if (_axis=="y-axis"){
- _mc["x"+i+"_pt"] += (_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing;
- if (dir == "ccw") {
- if (i<2) {
- _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
- } else {
- _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*-_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
- }
- } else {
- if (i>1) {
- _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
- } else {
- _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*-_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
- }
- }
- }else if (_axis=="x-axis"){
- _mc["y"+i+"_pt"] += (_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing;
- if (dir == "ccw") {
- if (i==2||i==3) {
- _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
- } else {
- _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*-_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
- }
- } else {
- if (i==2||i==3) {
- _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*-_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
- } else {
- _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
- }
- }
- }
- }
- //create and distort the front and back of the card
- var d:Stretcher = new Stretcher(front, img1, _seg, _seg);
- d._smoothing = true;
- var d_back:Stretcher = new Stretcher(back, img2, _seg, _seg);
- d_back._smoothing = true;
- if (_axis=="y-axis"){
- d.setTransform(_mc.x0_pt,_mc.y0_pt,_mc.x1_pt,_mc.y1_pt,_mc.x3_pt,_mc.y3_pt,_mc.x2_pt,_mc.y2_pt);
- d_back.setTransform(_mc.x1_pt,_mc.y1_pt,_mc.x0_pt,_mc.y0_pt,_mc.x2_pt,_mc.y2_pt,_mc.x3_pt,_mc.y3_pt);
- }if (_axis=="x-axis"){
- d.setTransform(_mc.x3_pt,_mc.y3_pt,_mc.x0_pt,_mc.y0_pt,_mc.x2_pt,_mc.y2_pt,_mc.x1_pt,_mc.y1_pt);
- d_back.setTransform(_mc.x2_pt,_mc.y2_pt,_mc.x1_pt,_mc.y1_pt,_mc.x3_pt,_mc.y3_pt,_mc.x0_pt,_mc.y0_pt);
- }
- //if using shadow, create and distort the mask
- if (_useShad){
- var d_shadow:Stretcher = new Stretcher(shad_mask, img1, 2, 2);
- d_shadow._smoothing = true;
- if (_axis=="y-axis"){
- d_shadow.setTransform(_mc.x0_pt,_mc.y0_pt,_mc.x1_pt,_mc.y1_pt,_mc.x3_pt,_mc.y3_pt,_mc.x2_pt,_mc.y2_pt);
- }if (_axis=="x-axis"){
- d_shadow.setTransform(_mc.x3_pt,_mc.y3_pt,_mc.x0_pt,_mc.y0_pt,_mc.x2_pt,_mc.y2_pt,_mc.x1_pt,_mc.y1_pt);
- }
- }
- //hide or show the front of the card, depending on the turn
- if (_axis=="y-axis"){
- if (_mc.x1_pt<(mcWIDTH/2)) {
- front.alpha = 0;
- } else {
- front.alpha = 1;
- }
- }else if (_axis=="x-axis"){
- if (_mc.y1_pt>(mcHEIGHT/2)) {
- front.alpha = 1;
- } else {
- front.alpha = 0;
- }
- }
- //animate shadow
- if (currFront!=front.alpha){
- if (_useShad){
- shad_holder.scaleX = shad_holder.scaleX*-1;
- }
- currFront=front.alpha;
- }else if (_useShad){
- flipNum++;
- var dx:Number;
- if (_axis=="y-axis"){
- dx_start=_mc.x0_pt;
- dx = (Math.sin((Math.round(Math.abs((dx_start-_mc.x1_pt)/mcWIDTH)*360)) * Math.PI/360));
- }else if (_axis=="x-axis"){
- dx_start=_mc.x1_pt;
- dx = (Math.sin((Math.round(Math.abs((dx_start-_mc.x0_pt)/mcHEIGHT)*360)) * Math.PI/360));
- }
- if (dx>_shadLimit) dx = _shadLimit * ((100 - flipNum)/100);
- if (dx<.07) dx=0;
- //trace(dx);
- shad_mc.alpha = dx;
- }
- var endCalc:Number;
- if (_axis=="y-axis"){
- endCalc=_mc.x1_pt-_mc.x1_goal
- }else if (_axis=="x-axis"){
- endCalc=_mc.y1_pt-_mc.y1_goal
- }
- //if finished.. end engine, if there are more turns...call the function again.
- if (Math.abs(endCalc)<.5+(turns-1)) {
- turns--;
- if (turns == 0) {
- i = 0;
- for (i; i<=3; i++) {
- if (_axis=="y-axis"){
- _mc["y"+i+"_pt"] = _mc["y"+i+"_goal"];
- } else if (_axis=="x-axis"){
- _mc["x"+i+"_pt"] = _mc["x"+i+"_goal"]
- }
- }
- if (_axis == "y-axis"){
- blurX = 0;
- blurit(_mc);
- d.setTransform(_mc.x0_pt,_mc.y0_pt,_mc.x1_pt,_mc.y1_pt,_mc.x3_pt,_mc.y3_pt,_mc.x2_pt,_mc.y2_pt);
- d_back.setTransform(_mc.x1_pt,_mc.y1_pt,_mc.x0_pt,_mc.y0_pt,_mc.x2_pt,_mc.y2_pt,_mc.x3_pt,_mc.y3_pt);
- if (_useShad) d_shadow.setTransform(_mc.x0_goal,_mc.y0_goal,_mc.x1_goal,_mc.y1_goal,_mc.x3_goal,_mc.y3_goal,_mc.x2_goal,_mc.y2_goal);
- } else if (_axis == "x-axis"){
- blurY = 0;
- blurit(_mc);
- d.setTransform(_mc.x3_pt,_mc.y3_pt,_mc.x0_pt,_mc.y0_pt,_mc.x2_pt,_mc.y2_pt,_mc.x1_pt,_mc.y1_pt);
- d_back.setTransform(_mc.x2_pt,_mc.y2_pt,_mc.x1_pt,_mc.y1_pt,_mc.x3_pt,_mc.y3_pt,_mc.x0_pt,_mc.y0_pt);
- if (_useShad) d_shadow.setTransform(_mc.x3_pt,_mc.y3_pt,_mc.x0_pt,_mc.y0_pt,_mc.x2_pt,_mc.y2_pt,_mc.x1_pt,_mc.y1_pt);
- }
- _mc.removeEventListener(Event.ENTER_FRAME,flipEngine);
- } else {
- _mc.removeEventListener(Event.ENTER_FRAME,flipEngine);
- flipScript(turns, masterEase);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement