我有一个应用程序,其中我从具有透明性的PNG图像创建了多个图层。这些层彼此都在屏幕上。当用户点击图层的非透明区域时,我需要能够忽略对图层的透明区域所进行的触摸,而只能检测为触摸...参见图片...



我怎么做?谢谢。

最佳答案

在这里,您有可能的解决方案。

在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);
}

关于cocos2d-iphone - Cocos2d 2.0-忽略对图层/ Sprite 的透明区域的触摸,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10889170/

10-13 04:51