AS3.0抽奖盘,控制角度和圈数

参照夏树的例子,自己也模仿了一个,夏树的地址:http://blog.csdn.net/hero82748274/article/details/6527162

自己加以理解也注释了一部分。

package 
{
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.display.Shape;
	import fl.controls.ComboBox;

	public class Main extends MovieClip
	{
		private var arrow:Arrow=new Arrow();
		private var speed:Number=20;//速度
		private var istart:Boolean = false;//是否开始
		private var rounds:int;//圈数
		private var totalAngle :Number;//总的角度
		private var currentAngle:Number;//当前角度
		private var targetAngle:Number;//目标角度
		private var totalTime:Number;//总的次数
		private var perSpeed:Number;//每一次的递减的加速度
		private var circle_combox:ComboBox
		private var angle_combox:ComboBox
		
		public function Main()
		{
			init();
		}
		private function init():void
		{
			//轮盘
			var disk:Disk=new Disk(10,150);
			//addChild(disk);
			disk.x = stage.stageWidth / 2;
			disk.y = stage.stageHeight / 2;

			addChild(arrow);
			arrow.x = disk.x;
			arrow.y = disk.y;
			
			//创建按钮
			var start_btn:CButton=new CButton("开始抽奖");
			start_btn.x=660;
			start_btn.y=30;
			var stop_btn:CButton=new CButton("停止转动");
			stop_btn.x=660;
			stop_btn.y=60;
			
			addChild(start_btn);
			addChild(stop_btn);
			
			start_btn.addEventListener(MouseEvent.CLICK,onClick);
			stop_btn.addEventListener(MouseEvent.CLICK,onStopClick);
			
			//初始化原始角度和圈数
			rounds = 1;
			targetAngle = 0;
			
			circle_combox = new ComboBox();
			angle_combox = new ComboBox();
			//设置选择圈数
			//new Label(this,420,70,"Circle:");
			//var items:Array=["1","2","3","4","5","6","7","8","9"];
			//circle_combox=new ComboBox(this,450,70,items[1],items);
			circle_combox.addItem({label:"1",data:1});
			circle_combox.addItem({label:"2",data:2});
			circle_combox.addItem({label:"3",data:3});
			circle_combox.addItem({label:"4",data:4});
			circle_combox.addItem({label:"5",data:5});
			circle_combox.addItem({label:"6",data:6});
			circle_combox.addItem({label:"7",data:7});
			circle_combox.addItem({label:"8",data:8});
			circle_combox.addItem({label:"9",data:9});
			
			circle_combox.addEventListener(Event.CHANGE,onComboxChange);
			
			//new Label(this,420,90,"Angle:");
			//var angles:Array=["0","36","72","108","144","180","216","252","288","324"];
			//angle_combox=new ComboBox(this,450,90,angles[1],angles); 
			//旋转角度可以自己定义
			angle_combox.addItem({label:"0",data:0});
			angle_combox.addItem({label:"36",data:36});
			angle_combox.addItem({label:"72",data:72});
			angle_combox.addItem({label:"108",data:108});
			angle_combox.addItem({label:"144",data:144});
			angle_combox.addItem({label:"180",data:180});
			angle_combox.addItem({label:"216",data:216});
			angle_combox.addItem({label:"252",data:252});
			angle_combox.addItem({label:"288",data:288});
			angle_combox.addItem({label:"324",data:324});
			
			angle_combox.addEventListener(Event.CHANGE,onComboxChangeAngle);

			setParameters();
			circle_combox.x = 60;
			circle_combox.y = 30;
			angle_combox.x = 60;
			angle_combox.y = 60;
			addChild(circle_combox);
			addChild(angle_combox);
		}
		
		//设置初始化参数
		private function setParameters():void
		{
			
			currentAngle=arrow.rotation;//当前角度
			speed=10;
		totalAngle=(360*rounds)+(targetAngle-currentAngle);//算出总的角度
			trace("开始总角度"+totalAngle);
			//计算项数Sn=(a1+an)*n/2 
			//等差数列,算出n列次数
			//算出项数
			totalTime =((2 * totalAngle) / speed);		
			
			//d=(an-a1)÷(n-1)
			//算出公差
			perSpeed = (speed / (totalTime -1 ));
	
		}
		
		

		private function onClick(event:MouseEvent):void
		{
			startGame();
		}

		private function onStopClick(event:MouseEvent):void
		{
			if (! istart)
			{
				return;
			}
			stopMove();
		}
		
		private function onComboxChange(event:Event):void
		{
		    rounds=int(circle_combox.selectedItem.data); 
		}
		
		private function onComboxChangeAngle(event:Event):void
		{
			 targetAngle=int(angle_combox.selectedItem.data);
		}		
		private function startGame():void
		{
            if ( istart) return;
			istart = true;
            setParameters()
			addEventListener(Event.ENTER_FRAME,Run);
		}
	
		private function Run(event:Event):void
		{
			if (istart)
			{
				move();
			}
			else
			{
				stopMove();
			}
		}
         
		//运动  
		private function move():void
		{			 
			arrow.rotation += speed;//加上一个初始速度然后进行递减
			speed-=perSpeed;
			if(speed<=0)stopMove();
		}

		//停止运行
		private function stopMove():void
		{
			istart=false;
			removeEventListener(Event.ENTER_FRAME,Run);
		}

	}

}

 

package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.filters.GlowFilter;
	
	//测试按钮
	public class CButton extends Sprite
	{
		public function CButton(label:String)
		{
			var btn_label:TextField=new TextField();
			btn_label.text=label;
			btn_label.selectable=false;
			btn_label.autoSize=TextFieldAutoSize.CENTER;
			btn_label.mouseEnabled=false;
			btn_label.x=10;
			addChild(btn_label);
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(0,0,80,20);
			this.graphics.endFill();
			this.filters=[new GlowFilter(0x333333,0.3,3,3)];
			this.buttonMode=true;
			
		}
		
		
	}
	
	
	
	
}


 

package
{
	//圆盘
	
	import flash.display.Sprite;	
	public class Disk extends Sprite
	{
		public function Disk(num:int,radius:Number)
		{
			if(num==0) return;
			this.graphics.lineStyle(0);
			this.graphics.drawCircle(0,0,radius);
			var perAngle:Number=360/num;
			for (var i:int=0; i<num; i++)
			{
				this.graphics.moveTo(0,0);
				this.graphics.lineTo(Math.cos(i*perAngle*Math.PI/180)*radius,Math.sin(i*perAngle*Math.PI/180)*radius);				 
			}
		}		
	}
	
}


 

package
{
	import flash.display.Sprite
	public class Arrow extends Sprite
	{
		public function Arrow()
		{
		   this.graphics.lineStyle(10,0x00ff00);
		   this.graphics.drawCircle(0,0,10);  
           this.graphics.moveTo(0,0);  
           this.graphics.lineTo(0,-160);  
           this.graphics.moveTo(0,-160);  
           this.graphics.lineTo(Math.cos(Math.PI/3)*8,-160+Math.sin(Math.PI/3)*8);  
           this.graphics.moveTo(0,-160);  
           this.graphics.lineTo(Math.cos(Math.PI-Math.PI/3)*8,-160+Math.sin(Math.PI-Math.PI/3)*8);
		}
	}
		
}



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值