我正在检查 BitmapData 对象是否完全被 BitmapData 对象遮挡。是否有类似 hitTest 函数的东西,但它确保每个像素都被覆盖而不是任何像素?
编辑:重要的是在检查对象是否被遮挡时不包括透明像素。
我正在检查 BitmapData 对象是否完全被 BitmapData 对象遮挡。是否有类似 hitTest 函数的东西,但它确保每个像素都被覆盖而不是任何像素?
编辑:重要的是在检查对象是否被遮挡时不包括透明像素。
毕竟,这实际上是一个非常简单的解决方案!基本上你所做的只是在重叠位图占据的矩形区域中捕获重叠位图的像素值。然后,您遍历该值向量,只要您没有 0(完全透明的像素),那么您就完全覆盖了下面的位图。
这是我在此测试中使用的两个位图:
重叠位图:
重叠位图:
代码:
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.utils.ByteArray;
import flash.geom.Rectangle;
var coveredBitmapData:BitmapData = new CoveredBitmapData();
var coveringBitmapData:BitmapData = new CoveringBitmapData();
var coveringBitmap:Bitmap = new Bitmap(coveringBitmapData, "auto", true);
var coveredBitmap:Bitmap = new Bitmap(coveredBitmapData, "auto", true);
coveredBitmap.x = Math.random() * (stage.stageWidth - coveredBitmap.width);
coveredBitmap.y = Math.random() * (stage.stageHeight - coveredBitmap.height);
stage.addChild(coveredBitmap);
stage.addChild(coveringBitmap);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMovement);
function onMouseMovement(e:MouseEvent):void
{
coveringBitmap.x = mouseX - (coveringBitmap.width * .5);
coveringBitmap.y = mouseY - (coveringBitmap.height * .5);
checkIfCovering(coveringBitmap, coveredBitmap);
}
function checkIfCovering(bitmapA:Bitmap, bitmapB:Bitmap):Boolean
{
//bitmapA is the covering bitmap, bitmapB is the bitmap being overlapped
var overlappedBitmapOrigin:Point = new Point(bitmapB.x, bitmapB.y);
var localOverlappedBitmapOrigin:Point = bitmapA.globalToLocal(overlappedBitmapOrigin);
var overlappingPixels:Vector.<uint> = bitmapA.bitmapData.getVector(new Rectangle(localOverlappedBitmapOrigin.x, localOverlappedBitmapOrigin.y, bitmapB.width, bitmapB.height));
if(overlappingPixels.length == 0) {
//This means that there is no bitmap data in the rectangle we tried to capture. So we are not at all covering the underlying bitmap.
return false;
}
var i:uint = 0;
for(i; i < overlappingPixels.length; ++i) {
if(overlappingPixels[i] == 0) {
return false;
}
}
return true;
}
所以你想看看是否object2
完全覆盖object1
(两个位图)?
var left:Boolean = object2.x <= object1.x;
var top:Boolean = object2.y <= object1.y;
var right:Boolean = object2.x + object2.width >= object1.x + object1.width;
var bottom:Boolean = object2.y + object2.height >= object1.y + object1.height;
if(left && right && top && bottom)
{
// Completely covered.
}