Cocos2d 2.0 - 忽略对图层/精灵的透明区域的触摸

Spa*_*Dog 16 cocos2d-iphone

我有一个应用程序,我有几个层从PNG图像创建透明度.这些图层都在屏幕上相互叠加.我需要能够忽略给予层的透明区域的触摸,并且当用户点击层的非透明区域时能够检测到触摸...参见图片...

在此输入图像描述

我怎么做?谢谢.

Lio*_*Lio 6

这里有一个可能的解决方案.

在CCLayer上实现扩展并提供此方法:

- (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);
}
Run Code Online (Sandbox Code Playgroud)