1. 程式人生 > >【ActionScript3.0】投籃Flash

【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

看看就好,不必較真。