【ActionScript3.0】投篮Flash

做得比较简陋。

代码:

import flash.events.Event;
import flash.events.MouseEvent;

var dragging:Boolean = false;
var onthebask:Boolean = false;
var times:Number = 0;
var g:Number = 9.8;//重力参数
var zuni:Number = 0.998;//空气阻尼
var txxs:Number = 0.5;//弹性系数
var speedx:Number = 0;
var speedy:Number = 0;
//当前时刻的坐标
var newx:Number = ball.x;
var newy:Number = ball.y;
var oldx:Number = newx;
var oldy:Number = newy;
//屏幕的尺寸
var stageWidth:Number = stage.stageWidth;
var stageHeight:Number = stage.stageHeight;
var radius:Number = ball.width / 2;
var lkx:Number = lk.x;
var lky:Number = lk.y;
var lkwidth:Number = lk.width;
var lkheight:Number = lk.height;
var win:Number = 0;
var lose:Number = 0;
var b_lose:Number = 0;
ball.addEventListener(MouseEvent.MOUSE_DOWN,dragBall);
ball.addEventListener(MouseEvent.MOUSE_UP,dropBall);
addEventListener(Event.ENTER_FRAME,repeat);
ts.autoSize = "left";
function dragBall(evt:MouseEvent){
	dragging = true;
	evt.target.startDrag();
}
function dropBall(evt:MouseEvent){
	dragging = false;
	evt.target.stopDrag();
}

function repeat(e:Event){
	if(onthebask){
		//trace(times);
		--times;
		if(times<=0){
			onthebask = false;
		}
	}
	//trace((ball.x > ));
	if(dragging){
		newx = ball.x;
		newy = ball.y;
		speedx = newx - oldx;
		speedy = newy - oldy;
		oldx = newx;
		oldy = newy;
	}
	else{
		oldx = ball.x;
		oldy = ball.y;
		speedy = (speedy + g * 1) * zuni;
		speedx = speedx * zuni;
		newx = ball.x + speedx * 1;
		newy = ball.y + speedy * 1;
		if(isonthebasketry(oldx,oldy,newx,newy)){
			//trace(oldx + " " + oldy + " " +newx + " " + newy);
			speedx = 0;
			onthebask = true;
			times = 10;
			//++win;
			//speedy = (speedy + g * 1) * zuni;
		}
		if( onthebask && (ball.x >  lkx - lkwidth/3) && ball.x  < (lkx + lkwidth/5) 
	  && (ball.y >  lky - lkheight/2 + 10) && (ball.y < lky + lkheight/2)){
		  ++win;
		//  trace(times);
		  onthebask = false;
		  times = 0;
	  	}
		if( touchthebasket(oldx,oldy,newx,newy)){
			//trace("打框");
			++b_lose;
			speedy = - speedy * txxs;
			speedx = - speedx * txxs;
		}
		if(isontherightbasketry(oldx,oldy,newx,newy)){
			//trace("1");
			//trace("打框");
			++lose;
			speedy = - speedy * txxs;
			speedx = - speedx * txxs;
		}
		if(newx < radius || newx > stageWidth - radius){
			speedx = -speedx * txxs;
		}
		else{
			ball.x = newx;
		}
		if(newy < radius || newy > stageHeight - radius){
			speedy = -speedy * txxs;
		}
		else{
			ball.y = newy;
		}
	}
	ts.text = "进框:" + win + "\n打框:" + lose
					+"\n打篮筐左上:" + b_lose
					+"\n重力加速度:" + g + "\n空气阻尼:"+zuni
					+"\n弹性系数:" + txxs + "\nonthebasket:"
					+ onthebask + "\ntimes:"+times;
}

function isonthebasketry(oldx:Number,oldy:Number
						 					,newx:Number,newy:Number):Boolean{
	if( !( (lky - lkheight/2)  > oldy && (lky - lkheight/2) < newy ) )return false;
	if(newx == oldx && newy >= lky-lkheight/2 && oldy <= lky-lkheight/2){
		return ((newx > lkx - lkwidth/3)) && (newx  < (lkx + lkwidth/5));
	}
	var k:Number = (newy - oldy) / (newx - oldx);
	var b:Number  = newy - k * newx;
	var ax:Number = (lky - lkheight / 2 - b)/k;
	//trace(ax + " " + (lkx + lkwidth/5));
	return (ax> lkx - lkwidth/3) && ax  < (lkx + lkwidth/5);
}

function isontherightbasketry(oldx:Number,oldy:Number
						 					,newx:Number,newy:Number):Boolean{
	if( !( (lky - lkheight/2)  > oldy && (lky - lkheight/2) < newy ) )return false;
	if(newx == oldx && newy >= lky-lkheight/2 && oldy <= lky-lkheight/2){
		return  ( newx >= (lkx - lkwidth/2)  && (newx <= (lkx - lkwidth/3))) 
			|| newx  >= (lkx + lkwidth/5);
	}
	var k:Number = (newy - oldy) / (newx - oldx);
	var b:Number  = newy - k * newx;
	var ax:Number = (lky - lkheight / 2 - b)/k;
	//trace(ax + " " + oldx + " " + oldy +  " " + newx + " " +newy);
	//trace("y = " + k +"x + "+b + " " +ax);
	//trace(ax+ " " + (lkx + lkwidth/5));
	return  ( ax >= (lkx - lkwidth/2)  && (ax <= (lkx - lkwidth/3))) 
			||   ( ax  >= (lkx + lkwidth/5));
}



function touchthebasket(oldx:Number,oldy:Number,newx:Number,newy:Number):Boolean{
		var lkb_x:Number = lkx - lkwidth / 2;
		var lkb_y:Number = lky - lkheight / 2;
		var len:Number = radius;
		if(!((oldx >= lkb_x - len && newx <=lkb_x +len) || (newx >= lkb_x - len && oldx <=lkb_x +len))&& ((oldy >= lkb_y - len && newy <=lkb_y +len) || (newy >= lkb_y - len && oldy <=lkb_y +len))
		  )return false;
		if(oldx == newx){
			return false;
		}
		var k:Number = (newy - oldy) / (newx - oldx);
		var b:Number  = newy - k * newx;
		var ax1:Number = (lkb_y-len - b)/k;
		var ax2:Number = (lkb_y+len - b)/k;
		if( ax1 > ax2){
			var temp:Number = ax1;
			ax1 = ax2;
			ax2 = temp;
		}
		return ax1 >= lkb_x - len && ax2 <= lkb_x + len;
}


本案例下载地址:http://download.csdn.net/detail/u013580497/9300993

看看就好,不必较真。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值