Search code examples
actionscript-3flashhittest

.hitTestPoint hitting multiple test points (AS3)


I can write a hitTestPoint trigger as such

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

and if I want to have multiple test points I can write something like

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

I am wondering though if there is anyway to define multiple hitpoints in the same statement. I've tried something like this with no luck...

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

or

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

...and many others but nothing seems to work. It is a pain to write the new points out for the same object over and over especially when you have 20+ hitpoint that you need to check for. Is there any way to add multiple points in one statement?


Solution

  • You seem to confuse programming and witchery. DisplayObject.hitTestPoint accepts 2 to 3 arguments of designated types (Number, Number, [optional Boolean]) in this designated order, and nothing else.

    ((mc2.x, mc2.y, true) || (mc2.x, mc2.y, true)) = single Boolean argument

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

    So you hittest one point at a time. To hittest 20 of them you need to loop through an Array of points. For example:

    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]);
        }
    }