Created
November 18, 2014 00:11
-
-
Save th3d0g/43350eca4dde1108e567 to your computer and use it in GitHub Desktop.
Flambe - Alpha Hit Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Checks if a point on a texture hits a pixel with 0 alpha | |
| // @th3d0g | |
| // ImageUtils.hx | |
| import flambe.display.Texture; | |
| import haxe.io.Bytes; | |
| import haxe.io.BytesData; | |
| class ImageUtils | |
| { | |
| public static function hitTestAlpha( x:Float, y:Float, texture:Texture ):Bool { | |
| var bytes:Bytes = texture.readPixels( Math.round( x ), Math.round( y ), 1, 1 ); | |
| var bytesData:BytesData = bytes.getData(); | |
| var colourHex:String = bytes.toHex(); | |
| var colourInt:Int = Std.parseInt( "0x" + colourHex ); | |
| var hasHitAlpha:Bool = ( colourInt >> 24 == 0 ); | |
| return hasHitAlpha; | |
| } | |
| } | |
| // Example | |
| imageSprite.pointerDown.connect(function(event : PointerEvent){ | |
| var imageSprite:ImageSprite = cast( event.hit, ImageSprite ); | |
| var hasHitAlpha = ImageUtils.hitTestAlpha( event.viewX - imageSprite.x._, | |
| event.viewY - imageSprite.y._, | |
| imageSprite.texture ); | |
| trace( hasHitAlpha ); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Needs some optimisation.