码迷,mamicode.com
首页 > 其他好文 > 详细

[ActionScript 3.0] 翻牌效果,运用语法rotationY,PerspectiveProjection

时间:2015-07-30 11:07:20      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

 

  1 package
  2 {
  3     import com.tweener.transitions.Tweener;
  4     
  5     import flash.display.Bitmap;
  6     import flash.display.BitmapData;
  7     import flash.display.Sprite;
  8     import flash.events.MouseEvent;
  9     import flash.geom.PerspectiveProjection;
 10     import flash.geom.Point;
 11     import flash.utils.getDefinitionByName;
 12     import flash.utils.setTimeout;
 13     
 14     /**
 15      * @author Frost.Yen
 16      * @E-mail 871979853@qq.com
 17      * @create 2015-7-29 下午4:53:03
 18      *
 19      */
 20     [SWF(width="1024",height="768",backgroundColor = "0x000000")]
 21     public class FlipCard extends Sprite
 22     {
 23         [Embed(source="card.png")] 
 24         private var _card:Class;
 25         [Embed(source="back.jpg")] 
 26         private var _back:Class;
 27         public function FlipCard()
 28         {
 29             initViews();
 30         }
 31         private function initViews():void
 32         {
 33             var cardClass:Class = getDefinitionByName("FlipCard__card") as  Class; 
 34             var backClass:Class = getDefinitionByName("FlipCard__back") as  Class; 
 35             var cardBitmap:Bitmap = new cardClass();
 36             var backBitmap:Bitmap = new backClass();
 37             
 38             for(var i:int = 0;i<18;i++){
 39                 var card:Card = new Card();
 40                 var backBmp:Bitmap = new Bitmap();
 41                 var cardBmp:Bitmap = new Bitmap();
 42                 var backBmpd:BitmapData = new BitmapData(backBitmap.width,backBitmap.height);
 43                 var cardBmpd:BitmapData = new BitmapData(cardBitmap.width,cardBitmap.height);
 44                 backBmpd.draw(backBitmap);
 45                 cardBmpd.draw(cardBitmap);
 46                 backBmp.bitmapData = backBmpd;
 47                 cardBmp.bitmapData = cardBmpd;
 48                 cardBmp.width = backBmp.width = 79;
 49                 cardBmp.height = backBmp.height = 115;
 50                 card.getCard(cardBmp,backBmp);
 51                 card.x = 200 + i%6*110+79*0.5;
 52                 card.y = 100 + Math.floor(i/6)*140+115*0.5;
 53                 this.addChild(card);
 54                 var perspective:PerspectiveProjection = new PerspectiveProjection();
 55                 perspective.projectionCenter = new Point(card.x,card.y)
 56                 card.transform.perspectiveProjection = perspective;
 57             }
 58             setTimeout(turnTheCard,1000*2);
 59         }
 60         private function onCardClick(e:MouseEvent):void
 61         {
 62             turnTween(e.currentTarget as Card);
 63         }
 64         private function turnTheCard():void
 65         {
 66             for(var i:int = 0;i<this.numChildren;i++){
 67                 turnTween(this.getChildAt(i) as Card);
 68                 this.getChildAt(i).addEventListener(MouseEvent.CLICK,onCardClick);
 69             }
 70         }
 71         private function turnTween(card:Card):void
 72         {
 73             Tweener.addTween(card,{rotationY:90,time:0.3,transition:"linear",onComplete:onComplete,onCompleteParams:[card]});    
 74             function onComplete(obj:Card):void
 75             {
 76                 obj.swapCard();
 77                 Tweener.addTween(obj,{rotationY:180,time:0.3,transition:"linear",onComplete:function():void{obj.rotationY = 0;obj.setRotationY();}});
 78             }
 79         }
 80     }
 81 }
 82 import flash.display.Bitmap;
 83 import flash.display.Sprite;
 84 
 85 class Card extends Sprite{
 86     private var _back:Sprite;
 87     private var _card:Sprite;
 88     private var _rotationY:Number;
 89     public function Card(){
 90         _back = new Sprite();
 91         _card = new Sprite();
 92         this.addChild(_back);
 93         this.addChild(_card);
 94     }
 95     public function getCard(card:Bitmap,back:Bitmap):void
 96     {
 97         card.x = back.x = -back.width*0.5;
 98         card.y = back.y = -back.height*0.5;
 99         _card.addChild(card);
100         _back.addChild(back);
101     }
102     
103     public function swapCard():void
104     {
105         this.swapChildren(_card,_back);
106         setRotationY();
107     }
108     public function setRotationY():void
109     {
110         if(this.rotationY == 90){
111             _card.rotationY = _back.rotationY = 180;
112         }else if(this.rotationY == 0){
113             _card.rotationY = _back.rotationY = 0;
114         }
115     }
116 }

 

[ActionScript 3.0] 翻牌效果,运用语法rotationY,PerspectiveProjection

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/4688394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!