【问题标题】:.hitTestPoint hitting multiple test points (AS3).hitTestPoint 击中多个测试点 (AS3)
【发布时间】:2017-03-25 08:37:04
【问题描述】:

我可以这样写一个 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+ 的生命值需要检查时。有没有办法在一个语句中添加多个点?

【问题讨论】:

    标签: actionscript-3 flash hittest


    【解决方案1】:

    您似乎混淆了编程和巫术。 DisplayObject.hitTestPoint 接受 2 到 3 个指定类型的参数(数字、数字、[可选布尔值]),按此指定顺序,仅此而已。

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

    ((mc2.x+5 || mc2.x-5), mc2.y, true) = (Boolean, Number, Boolean)

    所以你一次打测试一个点。要对其中的 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]);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      相关资源
      最近更新 更多