首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AS3 -检查BitmapData是否完全被其他BitmapData遮挡

AS3 -检查BitmapData是否完全被其他BitmapData遮挡
EN

Stack Overflow用户
提问于 2012-05-01 06:47:22
回答 2查看 727关注 0票数 1

我正在检查一个BitmapData对象是否被一个BitmapData对象完全遮挡。是否有类似于hitTest函数的功能,但它可以确保覆盖每个像素,而不是任何像素?

编辑:检查对象是否模糊时,不要包含透明像素,这一点很重要。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-01 08:08:32

毕竟,这实际上是一个非常简单的解决方案!基本上,您所要做的就是在重叠的位图占据的矩形区域中捕获重叠的位图的像素值。然后迭代该值的向量,只要没有0(完全透明的像素),就完全覆盖了下面的位图。

下面是我在测试中使用的两个位图:

重叠位图:

重叠位图:

代码:

代码语言:javascript
运行
复制
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;
}
票数 5
EN

Stack Overflow用户

发布于 2012-05-01 06:54:23

所以你想看看object2是否完全覆盖了object1 (两个位图)?

代码语言:javascript
运行
复制
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.
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10391538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档