【问题标题】:How can I get the Hit point on hit test between two dynamically added objects in AS3?如何在 AS3 中的两个动态添加的对象之间获得命中测试的命中点?
【发布时间】:2023-03-21 04:04:01
【问题描述】:

我想知道虚线击中三角形对象时的实际命中点。

我使用以下代码在两个对象之间进行命中:

target.hitTestObject(border);

target 是三角形对象,border 是一个组,我在其中放置了一个带有笔触 SolidColorDash 的矩形。

我正在使用以下代码来获取命中坐标:

var x:Number = target.x;
var y:Number = target.y;

当虚线接触到三角形对象的边界时,它给出了 x 和 y 坐标,但我想要虚线接触到三角形对象的位图数据时的坐标。

所以任何人都知道如何解决此问题或如何获取命中坐标。

【问题讨论】:

标签: actionscript-3 apache-flex flex4 hittest


【解决方案1】:

以下代码解决了我获得真正碰撞的问题:

private var returnValue:Boolean;
private var firstPoint:Point;
private var secondPoint:Point;
private var firstRectangle:Rectangle;
private var secondRectangle:Rectangle;
private var firstObjectBmpData:BitmapData;
private var secondObjectBmpData:BitmapData;
private var firstOffSetMatrix:Matrix;
private var secondOffSetMatrix:Matrix;

public function testCollision(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
{
        returnValue = false;

        firstRectangle = clip1.getBounds(clip1);
        secondRectangle = clip2.getBounds(clip2);

        if(secondRectangle.width != 0 && secondRectangle.height != 0 && firstRectangle.width != 0 && firstRectangle.height != 0)
        {
            firstObjectBmpData = new BitmapData(firstRectangle.width, firstRectangle.height, true, 0);
            firstObjectBmpData.draw(clip1);

            secondObjectBmpData = new BitmapData(secondRectangle.width, secondRectangle.height, true, 0);
            secondObjectBmpData.draw(clip2);

            firstPoint = new Point(clip1.x, clip1.y)
            secondPoint =  new Point(clip2.x, clip2.y)

            if (firstObjectBmpData.hitTest(firstPoint, 255, secondObjectBmpData, secondPoint, 255))
            {
                returnValue = true;
            }
        }

        return returnValue;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多