纯代码(action script )实现转盘效果(原创)

1:写代码的需求

因为项目的需求,想在游戏中加一个转盘抽奖的系统,计算结果由服务器根据一定概率事先己经提供好了,现在只不过想做一个类似转盘的效果,让结果显示给玩家

 

先上效果图

image

 

2:分析

     a:圆形布局

根据一个指写的中心点,半径,分布好所有的显示列表(Displayobject),这里,我只给了16个

     b:实现转动

我们假定一个初始速度和一个加速度,一个最大速度

一开始以一个初始速度运行,不断地加上加速度,当到达最大速度时,为了更加地“模拟”转盘,以这个最大速度再转个100圈(具体多少圈可以更像,读者自己可以试试哦),然后以一个减速度一直减下去,减到了初始速度时,不断地判断,这个结果是否是我们之前给定的结果,如果是,那么就停止下来。

3:代码

主要分为两个类

1:CircleTools

主要用来计算 圆形分布

package
{
    import flash.display.DisplayObject;
    import flash.geom.Point;

    public class CircleTools
    {
        public function CircleTools()
        {
        }
       
        /**
         * 计算圆形分布
         * @param center 中心点
         * @param radius 半径
         * @param list 需要分布的显示对像
         *
         */       
        public static function Calculate(center:Point,radius:int,list:Vector.<DisplayObject>):Vector.<DisplayObject>{
            var d:Number = 360/list.length;
            for(var idx:int;idx<list.length;idx++){
                var radians:Number = d*idx * Math.PI/180;
                list[idx].y = Math.sin(radians)*radius + center.y;
                list[idx].x = Math.cos(radians)*radius + center.x;
            }
           
            return list;
        }
       
    }
}

 

 

2:ChangePlate

实现转盘效果

package
{
    import flash.utils.setInterval;
    import flash.utils.clearInterval;
   
    /**
     * 转盘 抽像类
     * @author Junda
     *
     */   
    public class ChangePlate
    {
        private var _initSpeed:int = 300;

        /**
         * 初始速度
         * 速度越小越快
         */
        public function get initSpeed():int
        {
            return _initSpeed;
        }

        /**
         * @private
         */
        public function set initSpeed(value:int):void
        {
            _initSpeed = value;
        }

       
        private var _aSpeed:int = 20;

        /**
         * 加速度
         * 加速度越大
         * 加速越快
         */
        public function get aSpeed():int
        {
            return _aSpeed;
        }

        /**
         * @private
         */
        public function set aSpeed(value:int):void
        {
            _aSpeed = value;
        }

       
        private var _laSpeed:int = 40;

        /**
         * 减速度
         * 减速度越大
         * 减速越快
         */
        public function get laSpeed():int
        {
            return _laSpeed;
        }

        /**
         * @private
         */
        public function set laSpeed(value:int):void
        {
            _laSpeed = value;
        }

       
        private var _maxSpeed:int = 10;

        /**
         * 最大速度
         * 数值越小,整度越快
         * 最快为1
         */
        public function get maxSpeed():int
        {
            return _maxSpeed;
        }

        /**
         * @private
         */
        public function set maxSpeed(value:int):void
        {
            _maxSpeed = value;
        }
       
       
        private var isLow:Boolean = false;
       
        private var isHig:Boolean = false;
       
        private var isHigTme:int = 0;
       
        private var _EndStop:Boolean = false;

        public function get EndStop():Boolean
        {
            return _EndStop;
        }

       
        private var IntervalTime:uint;
       
        private var JXSTOP:Boolean = false;
       
        public function ChangePlate()
        {
        }
       
       
        /**
         * 停止
         *
         */       
        public function Stop():void{
            JXSTOP = true;
        }
       
       
        private var _callback:Function;
       
        public function Start(callback:Function):void{
            isLow = false;
            isHig = false;
            isHigTme = 0;
            _EndStop = false;
            JXSTOP = false;
           
            _callback = callback;   
            IntervalTime = setInterval(Effect,initSpeed);
        }
       
       
        private function Effect():void{
            clearInterval(IntervalTime);
            _callback.apply();
           
            if(JXSTOP){
                return;
            }
           
            if(isLow){
                initSpeed +=laSpeed;
                if(initSpeed>=500){
                    initSpeed = 500;
                    _EndStop = true;
                }
            }else{
                if(initSpeed<=maxSpeed){
                    if(!isHig){
                        if(isHigTme>100){
                            isHig = true;
                        }
                        isHigTme++;
                    }else{
                        initSpeed += laSpeed;
                        isLow = true;
                    }
                }else{
                    initSpeed -= aSpeed;
                }
            }
           
           
            IntervalTime = setInterval(Effect,initSpeed);
        }
       
       
    }
}

 

4:在UI显示方面我主要用了ASWING的JButton 作为主要的显示对像(各位读者可以用其它的作为代替)

主要调用代码

 

package
{
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.utils.clearInterval;
    import flash.utils.setInterval;
   
    import org.aswing.ASColor;
    import org.aswing.JButton;
    import org.aswing.JLabel;
   
   
   
    public class Test extends Sprite
    {
        private var list:Vector.<DisplayObject>;
       
        private var msg:JLabel;
       
        private var changePlate:ChangePlate;
       
        public function Test()
        {
           
            var start:JButton = new JButton("开始");
            start.setSizeWH(50,50);
            start.setLocationXY(120,120);
            this.addChild(start);
            start.addEventListener(MouseEvent.CLICK,HandleStartClick);
           
            msg = new JLabel("结果:");
            msg.setSizeWH(80,30);
            msg.setLocationXY(90,180);
            this.addChild(msg);
           
            list = new Vector.<DisplayObject>();
            for(var idx:int = 0;idx<16;idx++){
                var obj:JButton = new JButton((idx+1).toString());
                obj.setSizeWH(50,50);
                this.addChild(obj);
                list.push(obj);
            }
           
            CircleTools.Calculate(new Point(120,120),100,list);
           
           
            changePlate = new ChangePlate();
           
        }
       
        private var StartItemIndex:int = 0;
       
        private var Result:int;
       
        private function HandleStartClick(e:MouseEvent):void{
           
            Result = int(Math.random()*16);
            msg.setText("结果:"+Result);
           
            changePlate.Start(CallBack);
        }
       
       
        public function CallBack():void{
            if(StartItemIndex>=16){
                StartItemIndex   = 0;
            }
           
            if(StartItemIndex==0){
                (list[15] as JButton).setText((16).toString());
                (list[15] as JButton).setForeground(ASColor.WHITE);
                (list[StartItemIndex++] as JButton).setForeground(ASColor.BLACK);
            }else{
                (list[StartItemIndex-1] as JButton).setForeground(ASColor.WHITE);
                (list[StartItemIndex++] as JButton).setForeground(ASColor.BLACK);
            }
           
            trace("Result is "+Result);
            //            StartItemIndex++;
            if(changePlate.EndStop){
                if(StartItemIndex==Result){
                    changePlate.Stop();
                }
            }
           
        }
    }
}

 

OK ,到这里,打完,收工!

posted on 2012-11-25 22:50  Junda.chen  阅读(1685)  评论(2编辑  收藏  举报

导航