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);
}


 
 


你可能感兴趣的:(C++,Class,cocos2d-x,cocos2d-x3.x,cocostudio)