zoukankan      html  css  js  c++  java
  • cocos2d-x 不规则碰撞检测 【转载】

     原文:http://www.2cto.com/kf/201401/272331.html

    //判断有没有点到有材质的部分, p_point相对, CCSprite坐标  (p_point是相对 Sprite锚点的偏移)

    bool 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;

    }

  • 相关阅读:
    Android View体系(七)从源码解析View的measure流程
    Android View体系(六)从源码解析Activity的构成
    Android View体系(五)从源码解析View的事件分发机制
    Android View体系(四)从源码解析Scroller
    Android常用学习网站
    Android View体系(三)属性动画
    购物车
    模块与包
    s5_day9作业
    s5_day11作业
  • 原文地址:https://www.cnblogs.com/mokey/p/3925631.html
Copyright © 2011-2022 走看看