(5)創(chuàng)建文檔類Main類,如圖3-所示,首先創(chuàng)建一個(gè)牌的容器“container”,并將其放置于舞臺(tái)中心位置,如構(gòu)造函數(shù)第20到23行代碼所示,然后創(chuàng)建撲克牌容器對(duì)象并添加到容器“container”中,如第25、26行代碼所示,并將撲克牌容器對(duì)象停止播放(也就是停留在第1幀)、啟動(dòng)按鈕模式和注冊(cè)偵聽器函數(shù),如第27到29行代碼所示。
AS3代碼
package { import flash.display.*; import flash.events.*; import caurina.transitions.Tweener; /** * ... * @author lbynet (Tools -> Custom Arguments...) */ public class Main extends Sprite { private var container:Sprite; private var pane:MovieClip; private var isback:Boolean; private var currentPlane:MovieClip; private var currentRotationY:Number; public function Main():void { container = new Sprite(); container.x=stage.stageWidth/2; container.y=stage.stageHeight/2; addChild(container); pane = new Card(); container.addChild(pane); pane.stop(); pane.buttonMode=true; pane.addEventListener(MouseEvent.CLICK, onClick); } |
(6)當(dāng)撲克牌容器對(duì)象被單擊時(shí)調(diào)用onClick偵聽器函數(shù),為stage注冊(cè)事件偵聽來控制何時(shí)切換為正面或是反面,通過判斷變量isback 來重新設(shè)置被單擊撲克牌的rotationY屬性,如第36到44行代碼所示,在第38、42行代碼,通過Tweener 來切換撲克牌的rotationY值在0與-180度之間,如下原理圖15-5 所示。
AS3代碼
private function onClick(event:MouseEvent):void { stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler); currentPlane = MovieClip(event.currentTarget); if (isback) { Tweener.addTween(currentPlane, { rotationY:0,time:1 } ); //Tweener.addTween(currentPlane, { z:0,time:1 } ); } else { Tweener.addTween(currentPlane, { rotationY:-180,time:1 } ); //Tweener.addTween(currentPlane, { z:-200,time:1 } ); } isback = ! isback; } private function enterFrameHandler(event:Event):void { currentRotationY = currentPlane.rotationY; if (currentRotationY >= -90 && currentRotationY <= 10) { if (isback) { currentPlane.gotoAndStop(2); } else { currentPlane.gotoAndStop(1); } stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler); trace("已清除事件偵聽"); } } } |
圖15-5 文檔類
(7) 當(dāng)用戶單擊撲克牌容器對(duì)象,在第34行代碼中注冊(cè)的偵聽器后,執(zhí)行偵聽器函數(shù)enterFrameHandler ,并通過時(shí)時(shí)判斷撲克牌容器對(duì)象的rotationY的值來判斷撲克牌容器對(duì)象要切換跳轉(zhuǎn)到哪一幀,如第50到59行代碼所示,最后,清除無(wú)用的偵聽器,如第57行代碼所示。
(8)把onClick偵聽器函數(shù)中第39、43兩行代碼放開,發(fā)布測(cè)試,當(dāng)用戶單擊撲克牌容器對(duì)象后,撲克牌切換翻轉(zhuǎn)并放大(其實(shí)際是對(duì)象的z軸值減小),如下圖3- 所示,當(dāng)再次單擊后,撲克牌切換翻轉(zhuǎn)并縮小致原始狀態(tài)。
進(jìn)入論壇參與討論和交作業(yè):http://www.missyuan.com/viewthread.php?tid=425890