AS3自带有三种碰撞检测方式:
DisplayObject 的 hitTestObject() //检测两个显示对象是否发生碰撞(比较粗糙)
DisplayObject 的 hitTestPoint() //检测一个点是否与显示对象碰撞
BitmapData 的 hitTest() //像素级碰撞检测
一、DisplayObject 的 hitTestObject():
检测两个显示对象是否发生碰撞。
这是最粗糙的碰撞:只要两个可视对象(DisplayObject)的边框重叠,就认为它两相撞
package { import flash.display.MovieClip; import flash.display.Shape; import flash.events.Event; import flash.text.TextField; /** * 最粗糙的碰撞:只要两个可视对象(DisplayObject)的边框重叠,就认为它两相撞 * */ public class HitTestObject_test extends MovieClip { private var circle1:Shape; private var circle2:Shape; private var msg:TextField; public function HitTestObject_test() { circle1 = new Shape(); circle1.graphics.beginFill(0xFF0000); circle1.graphics.drawCircle(0, 0, 40); circle1.x = 100; circle1.y = 100; addChild(circle1); circle2 = new Shape(); circle2.graphics.beginFill(0x00FF00); circle2.graphics.drawCircle(0, 0, 40); circle2.x = 50; addChild(circle2); msg = new TextField(); msg.selectable = false; addChild(msg); this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(e:Event):void { circle2.x = mouseX; circle2.y = mouseY; if(circle1.hitTestObject(circle2)) msg.text = "hit"; else msg.text = "no hit"; msg.x = mouseX + 20; msg.y = mouseY; } } }
二、DisplayObject 的 hitTestPoint()
检测一个点是否与显示对象碰撞
package { import flash.display.MovieClip; import flash.display.Shape; import flash.events.Event; import flash.text.TextField; /** * 检测显示对象是否与 x 和 y 参数指定的点碰撞 * */ public class HitTestPoint_Test extends MovieClip { private var circle:Shape; private var msg:TextField; public function HitTestPoint_Test() { circle = new Shape(); circle.graphics.beginFill(0x0000FF, 0.5); circle.graphics.drawCircle(0, 0, 40); circle.x = 100; circle.y = 100; this.addChild(circle); msg = new TextField(); msg.multiline = true; msg.height = 50; msg.selectable = false; addChild(msg); this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(e:Event):void { if(circle.hitTestPoint(mouseX, mouseY, true)) //以实际图像为准(碰到实际图像才为true) msg.text = "hit"; else msg.text = "no hit"; if(circle.hitTestPoint(mouseX, mouseY, false)) //以边框为准(碰到边框就为true) msg.text += "\nhit"; else msg.text += "\nno hit"; msg.x = mouseX + 20; msg.y = mouseY; } } }flash.display.DisplayObject.hitTestPoint(x:Number, y:Number, shapeFlag:Boolean=false):Boolean
注意事项:
1、检测点的坐标(第一、二两个参数)必须以整个舞台为坐标系,而不能以该可视对象的父级容器为坐标系;
2、第三个参数为true时表示以实际图像为准(碰到实际图像时才认为碰撞发生),为false时表示以边框为准(碰到边框就认为碰撞发生)。
三、BitmapData 的 hitTest()
像素级的碰撞检测
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; import flash.text.TextField; public class HitTest_Test extends MovieClip { private var bm1:Bitmap; private var bm2:Bitmap; private var msg:TextField; public function HitTest_Test() { var sp1:Sprite = new Sprite(); sp1.graphics.beginFill(0xff0000, 0.5); sp1.graphics.drawCircle(50, 50, 50); sp1.graphics.endFill(); var bmd1:BitmapData = new BitmapData(sp1.width, sp1.height, true, 0); bmd1.draw(sp1); bm1 = new Bitmap(bmd1); bm1.x = 150; bm1.y = 150; this.addChild(bm1); var sp2:Sprite = new Sprite(); sp2.graphics.beginFill(0, 0.8); sp2.graphics.drawCircle(10, 10, 10); var bmd2:BitmapData = new BitmapData(sp2.width, sp2.height, true, 0); bmd2.draw(sp2); bm2 = new Bitmap(bmd2); this.addChild(bm2); msg = new TextField(); msg.selectable = false; addChild(msg); this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(e:Event):void { bm2.x = mouseX-0.5*bm2.width; bm2.y = mouseY-0.5*bm2.height; msg.x = mouseX+20; msg.y = mouseY; if (bm1.bitmapData.hitTest(new Point(bm1.x, bm1.y), 127, bm2.bitmapData, new Point(bm2.x, bm2.y), 128)) { msg.text ="hit"; } else { msg.text ="no hit"; } } } }flash.display.BitmapData.hitTest(firstPoint:Point, firstAlphaThreshold:uint, secondObject:Object, secondBitmapDataPoint:Point=null, secondAlphaThreshold:uint=1):Boolean
注意事项:
1、firstPoint一般为第一个bitmap的左上角点,secondBitmapDataPoint一般为第二个bitmap的左上角点,它两必须以同一个坐标系为参照;
2、firstAlphaThreshold表示第一个bitmap中参与碰撞检测的像素的最小alpha值,也就是说第一个bitmap中的像素点,只能当它的alpha值大于等于firstAlphaThreshold时才会参与碰撞检测;
3、secondAlphaThreshold表示第二个bitmap中参与碰撞检测的像素的最小alpha值,也就是说第二个bitmap中的像素点,只能当它的alpha值大于等于firstAlphaThreshold时才会参与碰撞检测;
4、secondObject除了BitmapData 外,还可以是一个 Rectangle、Point或Bitmap。