Ignore transparent pixels for mouse click event

I have background and an image (bitmap) on top of it that when clicked in the area of the image a small splat is created at mouse x and y. I dont want to create the splat if the mouse X,Y are clicking transparent pixel of that image. I tried

trace(StringTools.hex(myBitmapDataVar.getPixel32(Std.int(mouseX), Std.int(mouseY))));

but hex or normal it returns 0 no matter where it;s clicked within image transparent parts or existing pixels. What is t he way to detect only the drawn pixels of the image asset ? I’ve seen game engines that kind of handle the event automatically only at existing pixels of the clicked image :confused:

I wanted to make if transparency is 0 to add condition where the pixel of clicking should not be 0 but it returns that no matter where I click

Bitmapdatas have a hitTest function that actually allows you to set a treshold for alpha detection.

https://api.openfl.org/openfl/display/BitmapData.html#hitTest

I usually do this:

private function click(e:MouseEvent = null):Void 
{
	if (bitmapData.hitTest(new Point(0, 0), 0, new Point(e.localX, e.localY)))
	{
		// do your thing
	}
}

What my bitmapData is depends on the use case. Sometimes is the raw bitmapdata from a bitmap, some other times is a bitmapdata where I draw the DisplayObject that takes the mouseEvent.

I use a png on top of background image, nothing is drawn, it has to read only if the mouse click is on NOT transparent pixel of the top image. That’s where the splat is created. With the above arguments nothing happens if first point is 0,0. Also treshold must be 0 or 255? I want to avoid the event being run if click on transparent pixel. I need to compare this Bitmap data against the mouse clicked point.

What are the two objects? I need only 1 image on which I check the pixels.

https://api.openfl.org/openfl/display/BitmapData.html#hitTest

the second object, in your case, should be the mouse position in local coordinates.

Ok so it is like the above posted but it doesn’t read the PNG well since the event runs when clicking on some of the transparent background of the image and even fewer places that are color pixels instead of running when the solid color pixels only and not run at all on the transparent ones… I tried for Point 1 0,0 and the image’s X and Y but that doesn’t matter.

perhaps check the value of firstAlphaThreshold. in my code, it’s 0xFE and it seems to be working alright (though i can’t exactly remember why 0xFE, it’s been more than a year ago when i did that code :smiley: )

I think the number is meant to be “if alpha is greater than this then is valid”

With 0 you get: even if it is a super almost transparent thing, it’s not really transparent so it’s a hit.

With FE (254) you get: only detect if the color is fully solid and alpha is not a thing.