Quantcast
Channel: Cocos中文社区 - 最新帖子
Viewing all articles
Browse latest Browse all 495283

cocos2d js点击有没有像素级的判断啊

$
0
0

找到一个c++版的判断方式,,用于判断点击的点是否在sprite的有色范围内
不知道靠谱不靠谱,反正我看不太懂……

`> //判断有没有点到有材质的部分, p_point为相对, CCSprite锚点所在的坐标

bool Tools::isClickTheRealSprite(cocos2d::CCSprite* p_sprite, const cocos2d::CCPoint p_point){
    CCSize l_sizeInPixel = p_sprite->getContentSize();
    //得到相对,Sprite左下的偏移
    int l_iX = (int)(l_sizeInPixel.width/2.0f + p_point.x);
    int l_iY = (int)(l_sizeInPixel.height/2.0f + (int)p_point.y);
    CCPoint l_pointBL = ccp(l_iX, l_iY);
    CCRect l_rect = CCRect(0, 0, l_sizeInPixel.width, l_sizeInPixel.height);
    if(!(l_rect.containsPoint(l_pointBL))){
        return false;
    }

    //开辟出空内存buffer
    //unsigned char* l_buffer = (unsigned char*)malloc(l_sizeInPixel.width * l_sizeInPixel.height * 4);
    //memset(l_buffer, 0, l_sizeInPixel.width * l_sizeInPixel.height * 4);
    uint8_t l_buffer[4];

    //写内存
    CCSize size = p_sprite->getParent()->getContentSize();
    CCRenderTexture* l_renderTexture = CCRenderTexture::create(l_sizeInPixel.width, l_sizeInPixel.height, kCCTexture2DPixelFormat_RGBA8888);
    //l_renderTexture->begin();
    l_renderTexture->beginWithClear(0, 0, 0, 0);
    p_sprite->draw();
    glReadPixels(l_pointBL.x, l_pointBL.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, l_buffer);
    l_renderTexture->end();
    //透明度
    //int l_iValue_A = l_buffer[4*(int)(l_pointBL.y*l_sizeInPixel.width + l_pointBL.x)+3];
    int l_iValue_A = l_buffer[3];
    //free(l_buffer);
    if(l_iValue_A < 5 ){
        return false;
    }
    return true;
}`

Viewing all articles
Browse latest Browse all 495283

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>