转盘:
0001 package lu.leon.display {
0002     import flash.display.MovieClip;
0003     import flash.events.Event;
0004 
0005     import com.greensock.TweenLite;
0006     import com.greensock.easing.*;
0007 
0008     /**
0009      * @author LeonLu
0010      * @email [email protected]
0011      * @date 2010-12-27 15:30
0012      **/
0013     //[SWF(width="",height="",frameRate="",backgroundColor="#")]
0014     public class Turntable extends MovieClip {
0015         public var isDrawing:Boolean = false;
0016 
0017         private var vr:Number = 0; //旋转角速度
0018         private var ar:Number = 0.15; //旋转角加速度
0019         private var maxVr:Number = 18.0;
0020 
0021         public function Turntable(){
0022             if (stage)
0023                 init();
0024             else
0025                 addEventListener(Event.ADDED_TO_STAGE, init);
0026         }
0027 
0028         private function init(e:Event = null):void {
0029             removeEventListener(Event.ADDED_TO_STAGE, init);
0030             // entry point
0031         }
0032 
0033         public function startRotating():void {
0034             isDrawing = true;
0035             this.addEventListener(Event.ENTER_FRAME, rotate, false, 0, true);
0036         }
0037 
0038         private function rotate(e:Event):void {
0039             //vr如果一直不变,那么是匀速运动,此时,点击开始抽奖按钮,要求指针慢慢加速,
0040             //到了>maxVr时,就不用再加速,保持匀速运动
0041             if (vr < maxVr){
0042                 vr += ar;
0043             }
0044             pointer.rotation += vr;
0045 
0046             //此处控制当单击停止抽奖按钮后,速度慢慢递减,当vr<0时,不用再转了,移除ENTER_FRAME事件侦听,
0047             //并重置vr、ar为初始值
0048             if (vr < 0){
0049                 this.removeEventListener(Event.ENTER_FRAME, rotate);
0050                 vr = 0;
0051                 ar = 0.15;
0052                 isDrawing = false;
0053             }
0054         }
0055 
0056         public function stopRotating():void {
0057             ar = -0.15; //单击停止按钮后,让vr递减
0058             //加上这if条件的原因是,只有当vr>maxVr时,才需要微调vr
0059             //这时速度还未能递增至maxVr,如果这时也设置vr = maxVr - 1,那么速度会突然变快然后再减速
0060             if (vr > maxVr){
0061                 vr = maxVr - 1; //如果不加这句,此时的vr>maxVr,ENTER_FRAME事件触发函数中没有执行任何操作
0062             }
0063         }
0064 
0065         public function resetData():void {
0066             pointer.rotation = 0;        
0067         }
0068 
0069     }
0070 }
文字×××灯:
0001 package lu.leon.display {
0002     import flash.display.DisplayObject;
0003     import flash.display.DisplayObjectContainer;
0004     import flash.display.Sprite;
0005     import flash.display.MovieClip;
0006     import flash.events.Event;
0007 
0008     import com.greensock.TweenLite;
0009     import com.greensock.easing.*;
0010 
0011     /**
0012      * @author LeonLu
0013      * @email [email protected]
0014      * @date 2010-12-28 10:33
0015      **/
0016     //[SWF(width="",height="",frameRate="",backgroundColor="#")]
0017     public class DeskNumsBrowser extends Sprite {
0018         public var isDrawing:Boolean = false;
0019 
0020         private var beginNum:int;
0021         private var endNum:int;
0022         private var sum:int;
0023         private var ary_nums:Array;
0024 
0025         private var container:Sprite;
0026         private var itemW:int;
0027 
0028         private var maxVx:int = -30; //x方向恒定平移速度
0029         private var vx:int; //x方向平移速度
0030         private var ax:int = 1; //停止抽奖时,缓慢减速
0031 
0032         private var isSlowing:Boolean = false; //判断是不是减速停止过程中        
0033 
0034         //计数器
0035         private var count:int = 0;
0036         //container.x不断变化过程中,计数器正常应该是的数值
0037         private var correctCount:int;
0038         //container.x不断变化过程中两次计数器的差值
0039         private var substractionOfCount:int;
0040 
0041         private var substractionOfX:int; //指针停止时,离到达itemW整数倍的x坐标还需走过的距离
0042 
0043         public function DeskNumsBrowser(){
0044             if (stage)
0045                 init();
0046             else
0047                 addEventListener(Event.ADDED_TO_STAGE, init);
0048         }
0049 
0050         private function init(e:Event = null):void {
0051             removeEventListener(Event.ADDED_TO_STAGE, init);
0052 
0053             container = new Sprite();
0054             addChild(container);
0055 
0056             container.mask = maskMc;
0057         }
0058 
0059         public function setData(begin:int, end:int):void {
0060             beginNum = begin;
0061             endNum = end;
0062             sum = endNum - beginNum + 1;
0063             initData();
0064         }
0065 
0066         private function initData():void {
0067             ary_nums = [];
0068             for (var i:int = beginNum; i <= endNum; i++){
0069                 ary_nums[i - beginNum] = i;
0070             }
0071 
0072             for (var j:int = 0; j < sum; j++){
0073                 var item:DeskNumItem = new DeskNumItem();
0074                 var randomIndex:int = Math.floor(Math.random() * ary_nums.length);
0075                 var num:int = ary_nums[randomIndex];
0076                 ary_nums.splice(randomIndex, 1);
0077                 item.setData(num);
0078                 //item.setData(j + beginNum);
0079                 container.addChild(item);
0080                 item.x = item.width * j;
0081 
0082                 if (j == 0){
0083                     itemW = item.width;
0084                 }
0085             }
0086         }
0087 
0088         public function startRolling():void {
0089             isDrawing = true;
0090             this.addEventListener(Event.ENTER_FRAME, roll, false, 0, true);
0091         }
0092 
0093         private function roll(e:Event):void {
0094             if (!isSlowing){
0095                 vx = maxVx;
0096             } else {
0097                 vx += ax;
0098             }
0099             container.x += vx;
0100             /*            trace("vx=" + vx);
0101                trace("container.x=" + container.x);
0102                trace("container.x% itemW=" + (container.x % itemW));
0103              */ /*
0104              * 通过判断【container.x不断变化前后container所跑过的距离对应itemW的倍数】的差值,
0105              * 来获知需要将前面的几个DeskNumItem移到最后面
0106              */
0107             if (container.x / itemW <= -1){
0108                 correctCount = Math.floor(Math.abs(container.x / itemW));
0109                 substractionOfCount = correctCount - count;
0110                 trace("count=" + count);
0111                 trace("correctCount=" + correctCount);
0112                 trace("substractionOfCount=" + substractionOfCount + "\n");
0113                 if (substractionOfCount > 0){
0114                     setPosition(substractionOfCount);
0115                 }
0116             }
0117 
0118             if (vx >= 0){
0119                 this.removeEventListener(Event.ENTER_FRAME, roll);
0120                 substractionOfX = itemW + container.x % itemW; //此处container.x % itemW是负数,所以前面是+号
0121                 TweenLite.to(container, 2, {x: container.x - substractionOfX, onComplete: tweenOver});
0122             }
0123         }
0124 
0125         private function tweenOver():void {
0126             setPosition(1);
0127             isDrawing = false;
0128             isSlowing = false;
0129         }
0130 
0131         private function setPosition(sum2:int):void {
0132             for (var i:int = 0; i < sum2; i++){
0133                 var firstItem:DeskNumItem = container.getChildAt(0) as DeskNumItem;
0134                 var lastItem:DeskNumItem = container.getChildAt(sum - 1) as DeskNumItem;
0135                 firstItem.x = lastItem.x + itemW;
0136                 container.addChild(firstItem);
0137 
0138                 count++;
0139             }
0140         }
0141 
0142         public function stopRolling():void {
0143             isSlowing = true;
0144         }
0145 
0146         public function resetData():void {
0147             removeMcChildren(container);
0148             container.x = 0;
0149             count = 0;            
0150             initData();            
0151         }
0152 
0153         private function removeMcChildren(container:DisplayObjectContainer):void {
0154             var childrenNum:int = container.numChildren;
0155             if (childrenNum > 0){
0156                 for (var i:int = 0; i < childrenNum; i++){
0157                     var item:DisplayObject = container.getChildAt(0) as DisplayObject;
0158                     container.removeChild(item);
0159                     item = null;
0160                 }
0161             }
0162         }
0163 
0164     }
0165 }
抽奖_转盘_第1张图片