本文介绍了Cocos2d 2.0 - 忽略对透明区域的图层/精灵的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个应用程序,其中我有从PNG图像与透明度创建几个图层。这些层都在屏幕上彼此。我需要能够忽略给透明区域的图层的触摸,只是能够检测为触摸,当用户点击一个层的不透明区域...见图...
I have an app where I have several layers created from PNG images with transparency. These layers are all on the screen over each other. I need to be able to ignore touches given to transparent areas of layers and just be able to detect as touches, when the user taps on a non-transparent area of a layer... see pic...
>
我该如何做?感谢。
推荐答案
这里有一个可能的解决方案。
Here you have a possible solution.
在CCLayer上实施扩展程序并提供此方法:
Implement an extension on CCLayer and provide this method:
- (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 2.0 - 忽略对透明区域的图层/精灵的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!