The following piece of C code is used as a solution to check a point on a sprite and return the alpha value of that point (ie. is the point that was checked on the sprite transparent or not).
I need help figuring out how I can achieve the same logic in Cocos2d JS. Here is the reference link to the code I got this from: http://stackoverflow.com/questions/10889170/cocos2d-2-0-ignoring-touches-to-transparent-areas-of-layers-sprites
- (BOOL)isPixelTransparentAtLocation:(CGPoint)loc
{
//Convert the location to the node space
CGPoint location = [self convertToNodeSpace:loc];
//This is the pixel we will read and test
UInt8 pixel[4];
//Prepare a render texture to draw the receiver on, so you are able to read the required pixel and test it
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CCRenderTexture* renderTexture = [[CCRenderTexture alloc] initWithWidth:screenSize.width
height:screenSize.height
pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[renderTexture begin];
//Draw the layer
[self draw];
//Read the pixel
glReadPixels((GLint)location.x,(GLint)location.y, kHITTEST_WIDTH, kHITTEST_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
//Cleanup
[renderTexture end];
[renderTexture release];
//Test if the pixel's alpha byte is transparent
return (pixel[3] == 0);
}
As I understand, since the sprite cannot be read directly, a texture is made with the sprite inserted, AND FROM THAT POINT, we can check the transparency of a point on the texture.
This is what I have so far, However I do not know the Cocos2d JS equivilent of “glReadPixels” function. Is there some other implementation I could use?
var size = cc.winSize;
var sprite = new cc.Sprite(res.CloseNormal_png); // Add Sprite
sprite.setPosition(size.width/2,size.height/2); // Set to middle of screen
spriteTexture = new cc.RenderTexture(size.width,size.height); // Initialize Texture to the entire screen size
spriteTexture.setPosition(size.width/2,size.height/2); // Set the position of this texture to the middle of screen
this.addChild(spriteTexture,1); // Add the texture to the layer
spriteTexture.begin(); // Begin rendering the texture
sprite.visit(); // Add the sprite to the texture
/* NEED HELP FINDING/WRITING CODE HERE.
Get transparency / alpha value (RGBA, the A part) of texture at point(x,y) size.width/2,size.height/2.
*/
spriteTexture.end(); // Finish rendering the texture