Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.73 KB | None | 0 0
  1. package {
  2.  
  3. //import the external files you need
  4. import com.phillippevk.utils.Stretcher;
  5. import flash.filters.BlurFilter;
  6. import flash.display.*;
  7. import flash.geom.*;
  8. import flash.events.Event;
  9.  
  10.  
  11. public class CardFlip extends MovieClip{
  12. //it is recommended that you only edit these two vars
  13. private var initBlurValue:Number=15; //adjust higher if you want more blur per flip
  14. private var easingResolve:Number = .2; //controls the default speed of the ease in the last flip (lower=slower)
  15.  
  16. //no need to touch the private var below this point
  17.  
  18. //blur private variables
  19. private var initBlur:Number;
  20. private var blurX:Number = 0;
  21. private var blurY:Number = 0;
  22. private var quality:Number = 3;
  23.  
  24. //shadow variables
  25. private var matrix:Matrix;
  26. private var fillType:String = "linear";
  27. private var colors:Array = [0x000000, 0x000000];
  28. private var alphas:Array = [100, 0];
  29. private var ratios:Array= [30,255];
  30.  
  31. //values used in positioning card
  32. private var mcWIDTH:Number;
  33. private var mcHEIGHT:Number;
  34.  
  35. //private variables used in building your card instance
  36. private var easing:Number;
  37. private var dir:String;
  38. private var turns:Number;
  39. private var setAlt:Boolean = true;
  40.  
  41. private var _mc:MovieClip;
  42. private var _axis:String;
  43. private var _seg:Number;
  44. private var _persp:Number;
  45. private var _useBlur:Boolean;
  46. private var _shadLimit:Number;
  47. private var _useShad:Boolean = true;
  48. private var _startX:Number;
  49. private var _startY:Number;
  50.  
  51. private var front:MovieClip;
  52. private var back:MovieClip;
  53. private var shad_mask:MovieClip;
  54. private var shad_mc:MovieClip;
  55. private var img1:MovieClip;
  56. private var img2:MovieClip;
  57. private var shad_holder:MovieClip;
  58.  
  59. // FLIP GOALS VARS
  60. private var currFront:Number;
  61. private var frontSet:Boolean;
  62. private var dx_start:Number;
  63. private var scaleNum:Number;
  64. private var flipNum:Number;
  65. private var masterEase:Number;
  66.  
  67.  
  68. public function CardFlip(front_img:MovieClip, back_img:MovieClip, axis:String, dx:Number, dy:Number, per:Number, seg:Number, blur:Boolean, shad:Number) {
  69. //mc is the MovieClip you build your card in;
  70. //front_img is the path of your front image - should be the same dimensions as back_img;
  71. //back_img is the path of your back image - should be the same dimensions as front_img;
  72. //dx is the center x position of your card;
  73. //dy is the center y position of your card;
  74. //per is the perspective value of yoru card (0-3),
  75. //seg is the number of segments you want to youse (0-3) to reduce distortion - higher equals heavier file;
  76. //blur is true/false;
  77. //shadow is (0-1) representing the max alpha you want your shadow to have;
  78. img1 = front_img;
  79. img2 = back_img;
  80.  
  81. _mc = new MovieClip();
  82. addChild(_mc);
  83.  
  84. _startX = dx;
  85. _startY = dy;
  86. _useBlur = blur;
  87. _shadLimit = shad;
  88. if (_shadLimit == 0) _useShad = false;
  89. _persp = per;
  90. _seg = seg;
  91. _axis = axis;
  92.  
  93.  
  94. front = new MovieClip();
  95. back = new MovieClip();
  96.  
  97. img1 = new MovieClip();
  98. img2 = new MovieClip();
  99.  
  100.  
  101. _mc.addChild(back);
  102. _mc.addChild(front);
  103. _mc.addChild(img1);
  104. _mc.addChild(img2);
  105.  
  106. _mc.visible=false;
  107.  
  108. if (_useShad){
  109. shad_mask = new MovieClip();
  110. shad_mc = new MovieClip();
  111. shad_holder = new MovieClip();
  112. }
  113.  
  114. loadImage(back_img,back,img2);
  115. loadImage(front_img,front,img1);
  116. }
  117. //this function applies the blur to the current flipped state
  118. private function blurit(mc:MovieClip):void {
  119. var filter:BlurFilter = new BlurFilter(blurX, blurY, quality);
  120. mc.filters = [filter];
  121. }
  122.  
  123. private function createShadow():void {
  124.  
  125. _mc.addChild(shad_holder);
  126. shad_holder.addChild(shad_mc);
  127. _mc.addChild(shad_mask);
  128.  
  129. shad_holder.mouseEnabled = false;
  130. shad_holder.mouseChildren = false;
  131. shad_mask.mouseEnabled = false;
  132.  
  133. shad_mc.graphics.beginGradientFill(fillType, colors, alphas, ratios, matrix);
  134. shad_mc.graphics.drawRect(0,0,mcWIDTH,mcHEIGHT);
  135. shad_mc.graphics.endFill();
  136. shad_holder.scaleY = 2;
  137.  
  138. /*shad_mask.graphics.beginFill(0xFF0000);
  139. shad_mask.graphics.drawRect(0,0,mcWIDTH,mcHEIGHT);
  140. shad_mask.graphics.endFill();*/
  141.  
  142. if (_axis=="y-axis"){
  143. shad_holder.graphics.moveTo(0, 0);
  144. }else if (_axis=="x-axis"){
  145. shad_holder.rotation = 90;
  146. }
  147. shad_holder.mask = shad_mask;
  148. shad_mc.alpha = 0;
  149. }
  150.  
  151. //center the individual card faces in the card mc and then position the card to the x and y coordinates you specified.
  152. private function positionCard():void {
  153.  
  154. _mc.x = _startX;
  155. _mc.y = _startY;
  156. front.x -= mcWIDTH/2;
  157. front.y -= mcHEIGHT/2;
  158. back.x -= mcWIDTH/2;
  159. back.y -= mcHEIGHT/2;
  160.  
  161. _mc.visible = true;
  162.  
  163.  
  164. if (_useShad){
  165. shad_mc.x -= mcWIDTH/2;
  166. shad_mc.y -= mcHEIGHT/2;
  167.  
  168. shad_mask.x -= mcWIDTH/2;
  169. shad_mask.y -= mcHEIGHT/2;
  170.  
  171. matrix = new Matrix();
  172. matrix.createGradientBox(mcWIDTH, mcHEIGHT, 0, 0, 0);
  173. createShadow();
  174. }
  175. }
  176. //load the clips
  177. private function loadImage(clip:MovieClip, tar:MovieClip, img:MovieClip):void {
  178. trace(clip.width);
  179. trace(clip.height);
  180. trace("---");
  181. var bmd:BitmapData = new BitmapData(clip.width, clip.height, true, 0x00000000);
  182. bmd.draw(clip);
  183. var bm:Bitmap = new Bitmap(bmd);
  184. //addChild(bm);
  185. //bm.x = 200;
  186. //bm.y = 200;
  187. img.addChild(bm);
  188. var d:Stretcher = new Stretcher(tar, img, _seg, _seg);
  189. d._smoothing = true;
  190. img.visible = false;
  191. if (isNaN(mcWIDTH)) {
  192. mcWIDTH = img.width;
  193. mcHEIGHT = img.height;
  194. } else {
  195. positionCard();
  196. }
  197. d.setTransform(0,0,mcWIDTH,0,0,mcHEIGHT,mcWIDTH,mcHEIGHT);
  198. }
  199.  
  200. //set the values, return to front
  201. public function flipToFront(direct:String = "cw", ease:Number = 0):void {
  202. if (isNaN(ease)){
  203. initBlur = ease*initBlurValue;
  204. }else{
  205. initBlur = initBlurValue;
  206. }
  207. if (_axis == "y-axis"){
  208. blurX = initBlur;
  209. } else if (_axis == "x-axis"){
  210. blurY = initBlur;
  211. }
  212. dir = direct;
  213. if (front.alpha != 1) {
  214. flipScript(1, ease);
  215. }
  216. }
  217.  
  218. //set the values, return to back
  219. public function flipToBack(direct:String = "cw", ease:Number = 0):void {
  220. if (isNaN(ease)){
  221. initBlur = ease*initBlurValue;
  222. }else{
  223. initBlur = initBlurValue;
  224. }
  225. if (_axis == "y-axis"){
  226. blurX = initBlur;
  227. } else if (_axis == "x-axis"){
  228. blurY = initBlur;
  229. }
  230. dir = direct;
  231. if (front.alpha == 1) {
  232. flipScript(1, ease);
  233. }
  234. }
  235. //set the values, start the flip
  236. public function startFlip(direct:String, turns:Number, ease:Number = 0):void {
  237. if (isNaN(ease)){
  238. initBlur = ease*initBlurValue;
  239. }else{
  240. initBlur = initBlurValue;
  241. }
  242. if (_axis == "y-axis"){
  243. blurX = initBlur;
  244. } else if (_axis == "x-axis"){
  245. blurY = initBlur;
  246. }
  247. dir = direct;
  248. flipScript(turns, ease);
  249. }
  250. //these next two are called during every frame of the flip.
  251. //they set the values of the images as your card turns.
  252. private function goals_v1():void {
  253. if (_axis=="y-axis"){
  254. _mc.x0_goal = 0;
  255. _mc.x1_goal = mcWIDTH;
  256. _mc.x2_goal = mcWIDTH;
  257. _mc.x3_goal = 0;
  258.  
  259. _mc.x0_pt = mcWIDTH;
  260. _mc.x1_pt = 0;
  261. _mc.x2_pt = 0;
  262. _mc.x3_pt = mcWIDTH;
  263. } else if (_axis=="x-axis"){
  264. _mc.y0_goal = 0;
  265. _mc.y1_goal = mcHEIGHT;
  266. _mc.y2_goal = mcHEIGHT;
  267. _mc.y3_goal = 0;
  268.  
  269. _mc.y0_pt = mcHEIGHT;
  270. _mc.y1_pt = 0;
  271. _mc.y2_pt = 0;
  272. _mc.y3_pt = mcHEIGHT;
  273. }
  274. }
  275. private function goals_v2():void {
  276. if (_axis=="y-axis"){
  277. _mc.x0_goal = mcWIDTH;
  278. _mc.x1_goal = 0;
  279. _mc.x2_goal = 0;
  280. _mc.x3_goal = mcWIDTH;
  281.  
  282. _mc.x0_pt = 0;
  283. _mc.x1_pt = mcWIDTH;
  284. _mc.x2_pt = mcWIDTH;
  285. _mc.x3_pt = 0;
  286. } else if (_axis=="x-axis"){
  287. _mc.y0_goal = mcHEIGHT;
  288. _mc.y1_goal = 0;
  289. _mc.y2_goal = 0;
  290. _mc.y3_goal = mcHEIGHT;
  291.  
  292. _mc.y0_pt = 0;
  293. _mc.y1_pt = mcHEIGHT;
  294. _mc.y2_pt = mcHEIGHT;
  295. _mc.y3_pt = 0;
  296. }
  297. }
  298. //the function that calls the previous functions
  299. private function setGoals(num:Number):void {
  300. if (front.alpha == 0) {
  301. goals_v1();
  302. } else {
  303. goals_v2();
  304. }
  305. }
  306.  
  307. //set the final ease, the goal (depending on which turn you are on), and call the engine
  308. private function flipScript(turnsVar:Number, ease:Number):void {
  309. //set turns
  310. if (isNaN(turnsVar)){
  311. turns = 1;
  312. }else{
  313. turns = turnsVar;
  314. }
  315. //set ease
  316. if (turns >= 1){
  317. easing = (turns>1) ? .6+(turns/100) : easingResolve;
  318. }else{
  319. easing = easingResolve
  320. }
  321. masterEase = easing;
  322.  
  323. //this is run only once
  324. if (setAlt) {
  325. if (_axis=="y-axis"){
  326. _mc.y0_goal = 0;
  327. _mc.y1_goal = 0;
  328. _mc.y2_goal = mcHEIGHT;
  329. _mc.y3_goal = mcHEIGHT;
  330. _mc.y0_pt = 0;
  331. _mc.y1_pt = 0;
  332. _mc.y2_pt = mcHEIGHT;
  333. _mc.y3_pt = mcHEIGHT;
  334. setAlt = false;
  335. }else if (_axis=="x-axis"){
  336. _mc.x0_goal = mcWIDTH;
  337. _mc.x1_goal = mcWIDTH;
  338. _mc.x2_goal = 0;
  339. _mc.x3_goal = 0;
  340. _mc.x0_pt = mcWIDTH;
  341. _mc.x1_pt = mcWIDTH;
  342. _mc.x2_pt = 0;
  343. _mc.x3_pt = 0;
  344. setAlt = false;
  345. }
  346. }
  347. //set goal
  348. setGoals(turns);
  349.  
  350. //call engine
  351. flipGoals(masterEase);
  352. }
  353. //the actual flipping engine
  354. private function flipGoals(ease:Number):void {
  355.  
  356. frontSet = true;
  357. currFront = front.alpha;
  358. scaleNum = 1;
  359. flipNum = 0;
  360.  
  361. if (_useShad){
  362. if (_axis=="y-axis"){
  363. if (mcWIDTH > mcHEIGHT) scaleNum = mcWIDTH/mcHEIGHT;
  364. if (dir=="ccw"){
  365. shad_holder.scaleX = scaleNum
  366. }else{
  367. shad_holder.scaleX= -scaleNum
  368. }
  369. }
  370. if (_axis == "x-axis"){
  371. if (mcHEIGHT > mcWIDTH) scaleNum = mcHEIGHT/mcWIDTH;
  372. if (dir == "ccw"){
  373. shad_holder.scaleX = scaleNum
  374. }else{
  375. shad_holder.scaleX = -scaleNum
  376. }
  377. }
  378. }
  379. //create and start engine
  380. _mc.addEventListener(Event.ENTER_FRAME,flipEngine);
  381. }
  382.  
  383. private function flipEngine(e:Event):void{
  384. //set and use blur
  385. if (_useBlur) {
  386. if (turns == 1) {
  387. if (_axis=="y-axis"){
  388. blurX += (0-blurX)*.1;
  389. if (blurX<2) {
  390. blurX = 0;
  391. }
  392. }else if (_axis=="x-axis"){
  393. blurY += (0-blurY)*.1;
  394. if (blurY<2) {
  395. blurY = 0;
  396. }
  397. }
  398. }
  399. blurit(_mc);
  400. }
  401.  
  402. //set x and y values of your distorted cards
  403. var i:uint = 0;
  404. for (i; i<=3; i++) {
  405. if (_axis=="y-axis"){
  406. _mc["x"+i+"_pt"] += (_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing;
  407.  
  408. if (dir == "ccw") {
  409. if (i<2) {
  410. _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
  411. } else {
  412. _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*-_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
  413. }
  414. } else {
  415. if (i>1) {
  416. _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
  417. } else {
  418. _mc["y"+i+"_pt"] += ((((_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"])*easing)*-_persp)+(_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"]))*easing;
  419. }
  420. }
  421. }else if (_axis=="x-axis"){
  422. _mc["y"+i+"_pt"] += (_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing;
  423.  
  424. if (dir == "ccw") {
  425. if (i==2||i==3) {
  426. _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
  427. } else {
  428. _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*-_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
  429. }
  430. } else {
  431. if (i==2||i==3) {
  432. _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*-_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
  433. } else {
  434. _mc["x"+i+"_pt"] += ((((_mc["y"+i+"_goal"]-_mc["y"+i+"_pt"])*easing)*_persp)+(_mc["x"+i+"_goal"]-_mc["x"+i+"_pt"]))*easing;
  435. }
  436. }
  437. }
  438. }
  439.  
  440. //create and distort the front and back of the card
  441. var d:Stretcher = new Stretcher(front, img1, _seg, _seg);
  442. d._smoothing = true;
  443. var d_back:Stretcher = new Stretcher(back, img2, _seg, _seg);
  444. d_back._smoothing = true;
  445.  
  446. if (_axis=="y-axis"){
  447. 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);
  448. 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);
  449. }if (_axis=="x-axis"){
  450. 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);
  451. 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);
  452. }
  453.  
  454. //if using shadow, create and distort the mask
  455. if (_useShad){
  456. var d_shadow:Stretcher = new Stretcher(shad_mask, img1, 2, 2);
  457. d_shadow._smoothing = true;
  458. if (_axis=="y-axis"){
  459. 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);
  460. }if (_axis=="x-axis"){
  461. 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);
  462. }
  463. }
  464.  
  465. //hide or show the front of the card, depending on the turn
  466. if (_axis=="y-axis"){
  467. if (_mc.x1_pt<(mcWIDTH/2)) {
  468. front.alpha = 0;
  469. } else {
  470. front.alpha = 1;
  471. }
  472. }else if (_axis=="x-axis"){
  473. if (_mc.y1_pt>(mcHEIGHT/2)) {
  474. front.alpha = 1;
  475. } else {
  476. front.alpha = 0;
  477. }
  478. }
  479.  
  480. //animate shadow
  481. if (currFront!=front.alpha){
  482. if (_useShad){
  483. shad_holder.scaleX = shad_holder.scaleX*-1;
  484. }
  485. currFront=front.alpha;
  486. }else if (_useShad){
  487. flipNum++;
  488. var dx:Number;
  489. if (_axis=="y-axis"){
  490. dx_start=_mc.x0_pt;
  491. dx = (Math.sin((Math.round(Math.abs((dx_start-_mc.x1_pt)/mcWIDTH)*360)) * Math.PI/360));
  492. }else if (_axis=="x-axis"){
  493. dx_start=_mc.x1_pt;
  494. dx = (Math.sin((Math.round(Math.abs((dx_start-_mc.x0_pt)/mcHEIGHT)*360)) * Math.PI/360));
  495. }
  496.  
  497. if (dx>_shadLimit) dx = _shadLimit * ((100 - flipNum)/100);
  498. if (dx<.07) dx=0;
  499. //trace(dx);
  500. shad_mc.alpha = dx;
  501. }
  502. var endCalc:Number;
  503. if (_axis=="y-axis"){
  504. endCalc=_mc.x1_pt-_mc.x1_goal
  505. }else if (_axis=="x-axis"){
  506. endCalc=_mc.y1_pt-_mc.y1_goal
  507. }
  508. //if finished.. end engine, if there are more turns...call the function again.
  509. if (Math.abs(endCalc)<.5+(turns-1)) {
  510. turns--;
  511. if (turns == 0) {
  512. i = 0;
  513. for (i; i<=3; i++) {
  514. if (_axis=="y-axis"){
  515. _mc["y"+i+"_pt"] = _mc["y"+i+"_goal"];
  516. } else if (_axis=="x-axis"){
  517. _mc["x"+i+"_pt"] = _mc["x"+i+"_goal"]
  518. }
  519. }
  520. if (_axis == "y-axis"){
  521. blurX = 0;
  522. blurit(_mc);
  523. 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);
  524. 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);
  525. 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);
  526. } else if (_axis == "x-axis"){
  527. blurY = 0;
  528. blurit(_mc);
  529. 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);
  530. 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);
  531. 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);
  532. }
  533. _mc.removeEventListener(Event.ENTER_FRAME,flipEngine);
  534.  
  535. } else {
  536. _mc.removeEventListener(Event.ENTER_FRAME,flipEngine);
  537. flipScript(turns, masterEase);
  538.  
  539. }
  540. }
  541. }
  542. }
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement