加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-iphone – Cocos2d 2.0 – 忽略层/精灵透明区域的触摸

发布时间:2020-12-14 17:29:40 所属栏目:百科 来源:网络整理
导读:我有一个应用程序,我有几个图层从透明度的PNG图像创建.这些层都在屏幕上彼此相交.我需要能够忽略给层的透明区域的触摸,并且当用户点击层的不透明区域时,就能够被触摸检测到…请参见图片… 我怎么做?谢谢. 这里有一个可能的解决方案. 在CCLayer上实现扩展,并
我有一个应用程序,我有几个图层从透明度的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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读