Point in shape (graphic) hit test?

Sure Brent, thanks for looking.
 

The test code looks as follows:

For the test function:

local shape = display.newImage("test\_shape.png", 0, 0); shape.anchorX = 0; shape.anchorY = 0; local mask = graphics.newMask("test\_mask.png"); shape:setMask( mask ); shape:addEventListener("tap", M.shapeTouched); local event = { name="tap", x=64, y=5, target=shape,time=1011,numTaps=1}; shape:dispatchEvent( event );

And then for the listener:

M.shapeTouched = function(event) print("TAP TRIGGERED"); end

The way I wanted this to behave was to not trigger the listener because the tap event is being dispatched with an x, y in the masked area. But the actual behavior is the shapeTouched handler is always called and TAP TRIGGERED always gets printed. It seems like the listener function gets called no matter where the coordinates are; even if they are way off of the image.

Another thing I tried was triggering a tap event on a graphic layer above this shape, hoping that maybe propagation would make it work right, but in that case the listener wasn’t triggered at all.

Thanks for the help.

Hi @dfoxinator,

I don’t see that you’ve set “.isHitTestMasked”. Is that somewhere else in your code?

Also, for the time being, can you use default (center) anchor on this? I don’t think there’s any conflict with masks and offset anchor points, but for testing, I’d like to eliminate that offset.

Thanks,

Brent

Thanks Brent. I tried setting isHitTestMasked to true and still no luck. I also got rid of the anchor point changing.

I think this is definitely some issue with the event dispatch method I’m using because actually clicking the shape produces the expected results, meaning when you click the masked off area it doesn’t fire the listener but when you click the proper area it does.

Yes, this is probably not supported (dispatching event while also honoring the hit mask).

If your shapes aren’t incredibly complex, and you don’t have to handle dozens or hundreds of varieties, could you build vector polygons that basically surround the touchable area, and overlay those (invisible but hit-testable) over the images?

Brent

Yeah, that’s definitely an option. Either that or just plot some points to form border lines. It would be a really good feature though, to have a function that could test whether a single point intersects with an object. This is something Flash has which is very useful.

Thanks for the advice.