天天看點

cocos2d-x3.x 像素碰撞

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
using namespace cocos2d;

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  
    
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
    
    
    cocos2d::EventListenerTouchOneByOne *_listener;
    cocos2d::Sprite *bulletForTower12;
    cocos2d::Sprite *m_imgMan;
    cocos2d::RenderTexture *m_pRenderTexture;
    cocos2d::Label *m_pLabTips;
    
    virtual bool onTouchBegan(Touch *touch, Event *unused_event);
    virtual void onTouchEnded(Touch *touch, Event *unused_event);
    
};

#endif // __HELLOWORLD_SCENE_H__
           
<pre name="code" class="cpp">#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    m_imgMan = Sprite::create("tangfa09/tafang09-1-zidan0002.png");
    m_imgMan->setPosition(Vec2(400, 200));
    this->addChild(m_imgMan, 1);
    
    m_pRenderTexture = RenderTexture::create(m_imgMan->getContentSize().width, m_imgMan->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
    m_pRenderTexture->ignoreAnchorPointForPosition(true);
    m_pRenderTexture->setPosition(Vec2(400, 200));
    m_pRenderTexture->setAnchorPoint(Vec2::ZERO);
    this->addChild(m_pRenderTexture, 0, 1);
    m_pRenderTexture->setVisible(false); //将繪制出來的圖檔隐藏
    
    
    _listener=EventListenerTouchOneByOne::create();
    _listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
    _listener->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
    
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_listener, this);
    
    return true;
}

bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event)
{
    bool isTouched = false;
    
    auto touchPoint = touch->getLocationInView();
    auto glPoint = Director::getInstance()->convertToGL(touchPoint);

    if (m_imgMan->boundingBox().containsPoint(glPoint)) {
        
        Color4B color4B = {0, 0, 0, 0};
        
        Point nodePos = m_imgMan->convertTouchToNodeSpace(touch);
        unsigned int x = nodePos.x;
        unsigned int y = m_imgMan->getContentSize().height - nodePos.y;
        
        Point point = m_imgMan->getPosition();
        //開始準備繪制
        //繪制完成後清理畫布的内容
        m_pRenderTexture->beginWithClear(0, 0, 0, 0);
        //繪制使用的臨時精靈,與原圖是同一圖檔
        auto pTempSpr = Sprite::createWithSpriteFrame(m_imgMan->displayFrame());
        pTempSpr->setPosition(Vec2(pTempSpr->getContentSize().width / 2, pTempSpr->getContentSize().height / 2));
        //繪制
        pTempSpr->visit();
        //結束繪制
        m_pRenderTexture->end();
        //通過畫布拿到這張畫布上每個像素點的資訊,封裝到CCImage中
        Image* pImage = m_pRenderTexture->newImage();
        //擷取像素資料
        unsigned char* data_ = pImage->getData();
        unsigned int *pixel = (unsigned int *)data_;
        pixel = pixel + (y * (int)pTempSpr->getContentSize().width) * 1 + x * 1;
        //R通道
        color4B.r = *pixel & 0xff;
        //G通道
        color4B.g = (*pixel >> 8) & 0xff;
        //B通道
        color4B.b = (*pixel >> 16) & 0xff;
        //Alpha通道,我們有用的就是Alpha
        color4B.a = (*pixel >> 24) & 0xff;
        CCLOG("目前點選的點的: alpha = %d", color4B.a);
        
        if (color4B.a > 50) {
            isTouched = true;
        } else {
            isTouched = false;
        }
    }
    if (m_pLabTips) {
        m_pLabTips->removeFromParentAndCleanup(true);
        m_pLabTips = NULL;
    }
    
    return isTouched;
}

void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event)
{
    if (m_pLabTips) {
        m_pLabTips->removeFromParentAndCleanup(true);
        m_pLabTips = NULL;
    }
    
    m_pLabTips = Label::createWithTTF("NO", "fonts/Marker Felt.ttf", 30);
    m_pLabTips->setAnchorPoint(Vec2::ZERO);
    m_pLabTips->setPosition(Vec2(300.0f, 100.0f));
    m_pLabTips->setColor(Color3B::YELLOW);
    this->addChild(m_pLabTips, 1);
}