像素碰撞检测
程序员文章站
2022-03-13 12:50:24
...
var redRect:Rectangle = redClip.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
redClipBmpData.draw(redClip);
var blueRect:Rectangle = blueClip.getBounds(this);
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(blueClip);
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void
{
blueClip.x = mouseX;
blueClip.y = mouseY;
if(redClipBmpData.hitTest(new Point(redClip.x, redClip.y),
255,
blueClipBmpData,
new Point(blueClip.x, blueClip.y),
255
))
{
trace("hit");
redClip.filters = [new GlowFilter()];
}
else
{
redClip.filters = [];
}
}
渲染版
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void
{
blueClip.x = mouseX;
blueClip.y = mouseY;
redClip.rotation++;
var blueRect:Rectangle = blueClip.getBounds(this);
var blueOffset:Matrix = blueClip.transform.matrix;
blueOffset.tx = blueClip.x - blueRect.x;
blueOffset.ty = blueClip.y - blueRect.y;
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(blueClip, blueOffset);
var redRect:Rectangle = redClip.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
var redOffset:Matrix = redClip.transform.matrix;
redOffset.tx = redClip.x - redRect.x;
redOffset.ty = redClip.y - redRect.y;
redClipBmpData.draw(redClip, redOffset);
var rLoc:Point = new Point(redRect.x, redRect.y);
var bLoc:Point = new Point(blueRect.x, blueRect.y);
if(redClipBmpData.hitTest(rLoc,
255,
blueClipBmpData,
bLoc,
255
))
{
trace("hit");
redClip.filters = [new GlowFilter()];
}
else
{
redClip.filters = [];
}
blueClipBmpData.dispose();
redClipBmpData.dispose();
}