.hitTestPoint命中多个测试点(AS3)

时间:2017-03-25 08:37:04

标签: actionscript-3 flash hittest

我可以写一个hitTestPoint触发器

if (mc1.hitTestPoint(mc2.x, mc2.y, true)) 

如果我想拥有多个测试点,我可以编写类似

的内容
if (mc1.hitTestPoint(mc2.x, mc2.y, true) || mc1.hitTestPoint(mc2.x-5, mc2.y, true) || mc1.hitTestPoint(mc2.x+5, mc2.y, true))

我想知道是否还有在同一个声明中定义多个生命值。我没试过这样的事情......

if (mc1.hitTestPoint((mc2.x, mc2.y, true) || (mc2.x, mc2.y, true))) 

if (mc1.hitTestPoint((mc2.x+5 || mc2.x-5), mc2.y, true)) 

......还有很多其他人似乎没什么用。为同一个对象反复写出新的点是很痛苦的,特别是当你有20+命中点需要检查时。有没有办法在一个语句中添加多个点?

1 个答案:

答案 0 :(得分:2)

你似乎混淆了编程和巫术。 DisplayObject.hitTestPoint以此指定的顺序接受指定类型(Number,Number,[optional Boolean])的2到3个参数,而不接受任何其他参数。

  

((mc2.x,mc2.y,true)||(mc2.x,mc2.y,true))=单个布尔参数

     

((mc2.x + 5 || mc2.x-5),mc2.y,true)=(布尔值,数字,布尔值)

所以你一次击中一个点。要击中其中的20个,你需要遍历一个点数组。例如:

var Foo:Array =
[
    mc2.x, mc2.y,
    mc2.x+5, mc2.y,
    mc2.x-5, mc2.y
];

// Hit test pairs of elements as (x,y) coordinates.
for (var i:int = 0; i < Foo.length; i += 2)
{
    var aHit:Boolean = mc1.hitTestPoint(Foo[i], Foo[i+1]);

    if (aHit)
    {
        trace("Hit!", Foo[i], Foo[i+1]);
    }
    else
    {
        trace("Miss!", Foo[i], Foo[i+1]);
    }
}