mc触碰事件_不规则图形的触碰!

由 夕空 撰写于  2009年9月24日

调用方法:
MovieClip.hitTest2(obj:MovieCLip[,points:Array]):Boolean


 一、自由运动的mc触碰事件:
不规则影片剪辑"wall"与运动影片剪辑"o"


//代码:
MovieClip.prototype.hitTest2 = function(obj:MovieClip, points:Array):Boolean  {
	var pos:Object = obj.getBounds(_root);
	//posX,posY是图形的几何中心. 
	var posX:Number = (pos.xMin+pos.xMax)/2;
	var posY:Number = (pos.yMin+pos.yMax)/2;
	//如果碰撞到中心,则返回true; 
	if (this.hitTest(posX, posY, true)) {
		return true;
	}
	if (points != undefined) {
		//如果有传入数组. 
		var tmpFlag:Boolean = false;
		for (var i in points) {
			if (this.hitTest((points[i][0]+posX), (points[i][1]+posY), true)) {
				tmpFlag = true;
				//如果目标与数组中任一坐标碰撞则返回true; 
				break;
			}
		}
		return tmpFlag;
	}
	//如果与四边碰撞则返回true;   
	if (this.hitTest(pos.xMin, pos.yMin, true) || this.hitTest(pos.xMax, pos.yMin, true) || this.hitTest(pos.xMax, pos.yMax, true) || this.hitTest(pos.xMin, pos.yMax, true)) {
		return true;
	}
	return false;
};
//////////////////////////////////////////////////
setInterval(test, 20);
var x:Number = 3;
var y:Number = 3;
//定义o的四角触碰区域
var p:Array = [[15, 15], [-15, -15]];
function test() {
	o._x += x;
	o._y += y;
	if (wall.hitTest2(o, p)) {
		trace("hit");
		x = -x+2*(Math.random()-0.5);
		y = -y+2*(Math.random()-0.5);
	}
}
//来源:http://blog.5d.cn/user4/jinjiaxin/200602/208211.html

===================================== 

 二、键盘控制mc运动时的触碰事件:


//代码:
MovieClip.prototype.hitTest2 = function(obj:MovieClip, points:Array):Boolean  {
	var pos:Object = obj.getBounds(_root);
	//posX,posY是图形的几何中心. 
	var posX:Number = (pos.xMin+pos.xMax)/2;
	var posY:Number = (pos.yMin+pos.yMax)/2;
	//如果碰撞到中心,则返回true; 
	if (this.hitTest(posX, posY, true)) {
		return true;
	}
	if (points != undefined) {
		//如果有传入数组. 
		var tmpFlag:Boolean = false;
		for (var i in points) {
			if (this.hitTest((points[i][0]+posX), (points[i][1]+posY), true)) {
				tmpFlag = true;
				//如果目标与数组中任一坐标碰撞则返回true; 
				break;
			}
		}
		return tmpFlag;
	}
	//如果与四边碰撞则返回true;      
	if (this.hitTest(pos.xMin, pos.yMin, true) || this.hitTest(pos.xMax, pos.yMin, true) || this.hitTest(pos.xMax, pos.yMax, true) || this.hitTest(pos.xMin, pos.yMax, true)) {
		return true;
	}
	return false;
};
///////////////////////////////////////////////////
var W = 550;
var H = 400;
//定义mc.lead的四角触碰区域
var p:Array = [[15, 15], [-15, -15]];
//定义mc.lead的位置变化步长值
var sp = 5;
//限制mc.lead与边界的距离
var bj = 0;
//创建一个对象,用来侦听键盘事件
var key:Object = new Object();
//侦听对象开始侦听键盘按下事件
key.onKeyDown = function() {
	if (mc.obstacle.hitTest2(mc.lead, p)) {
		mc.lead._x = temp_x;
		mc.lead._y = temp_y;
	} else {
		temp_x = mc.lead._x;
		temp_y = mc.lead._y;
	}
	if (Key.isDown(Key.LEFT)) {
		mc.lead._x = Math.max(mc.lead._width/2+bj-mc.bg._width/2, mc.lead._x-sp);
	} else if (Key.isDown(Key.RIGHT)) {
		mc.lead._x = Math.min(mc.bg._width/2-mc.lead._width/2-bj, mc.lead._x+sp);
	} else if (Key.isDown(Key.DOWN)) {
		mc.lead._y = Math.min(mc.bg._height/2-mc.lead._height/2-bj, mc.lead._y+sp);
	} else if (Key.isDown(Key.UP)) {
		mc.lead._y = Math.max(mc.lead._height/2+bj-mc.bg._height/2, mc.lead._y-sp);
	}
};
//侦听键盘释放事件
key.onKeyUp = function() {
};
//键盘注册侦听对象
Key.addListener(key);
//////////////////////////////////////////////
this.onEnterFrame = function() {
	mc._x = -mc.lead._x;
	mc._y = -mc.lead._y;
	if (mc._x<-(mc._width-W)/2) {
		mc._x = -(mc._width-W)/2;
	} else if (mc._x>(mc._width-W)/2) {
		mc._x = (mc._width-W)/2;
	}
	if (mc._y<-(mc._height-H)/2) {
		mc._y = -(mc._height-H)/2;
	} else if (mc._y>(mc._height-H)/2) {
		mc._y = (mc._height-H)/2;
	}
};

声明:星耀夕空|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - mc触碰事件_不规则图形的触碰!


欢迎光顾我的小站!