美文网首页cocos2d-Lua
cocos2d-x lua 3.10 不规则按钮点击(鼠标点击判

cocos2d-x lua 3.10 不规则按钮点击(鼠标点击判

作者: 人气小哥 | 来源:发表于2017-11-10 10:52 被阅读23次

    3.10 调通了
    G:\Mycocos310Cpp\touchAlpha\Classes\HelloWorldScene.h

    #ifndef __HELLOWORLD_SCENE_H__
    #define __HELLOWORLD_SCENE_H__
    
    #include "cocos2d.h"
    
    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();
        bool onTouchBegan(cocos2d::Touch* pTouch, cocos2d::Event* pEvent);
        //void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
        void onTouchEnded(cocos2d::Touch* pTouch, cocos2d::Event* pEvent);
    
        // implement the "static create()" method manually
        CREATE_FUNC(HelloWorld);
    private:
        cocos2d::CCSprite* m_imgMan;
        cocos2d::CCRenderTexture* m_pRenderTexture;
    
        cocos2d::LabelTTF* m_pLabTips;
    
        cocos2d::EventListenerTouchOneByOne* _touchListener;
    };
    
    #endif // __HELLOWORLD_SCENE_H__
    
    
    #include "HelloWorldScene.h"
    #include "cocostudio/CocoStudio.h"
    #include "ui/CocosGUI.h"
    
    USING_NS_CC;
    
    using namespace cocostudio::timeline;
    
    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 = NULL;
        m_pRenderTexture = NULL;
        m_pLabTips = NULL;
        
        m_imgMan = CCSprite::create("ui_force_20001.png");
        m_imgMan->setPosition(ccp(100, 100));
        m_imgMan->setAnchorPoint(Vec2(0.5f, 0.5f));
        addChild(m_imgMan, 1);
    
        //这里参数很固定 没有什么可改的余地
        //参数1 你需要渲染出来区域的宽
        //参数2 你需要渲染出来区域的高
        //参数3 必须是这个模式 否则后面复制内存好像会有问题Texture2D::PixelFormat::RGBA8888
        m_pRenderTexture = RenderTexture::create(m_imgMan->getContentSize().width, m_imgMan->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
        m_pRenderTexture->setPosition(ccp(100, 100));
        //m_pRenderTexture->setAnchorPoint(Vec2(0, 0));//设置锚点无效 锚点默认0.5f, 0.5f
        addChild(m_pRenderTexture, 2);
    
        // Adds touch event listener
        auto listener = EventListenerTouchOneByOne::create();
        listener->setSwallowTouches(true);
    
        listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
        listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
    
        _eventDispatcher->addEventListenerWithFixedPriority(listener, 1);
    
        _touchListener = listener;
    
        return true;
    }
    
    bool HelloWorld::onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent) {
        //CCLOG("123");
        bool isTouched = false;
    
        CCPoint touchPoint = pTouch->getLocationInView();
        CCPoint glPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
    
    
        if (m_imgMan->boundingBox().containsPoint(glPoint)) {
    
            Color4B color4B = { 0, 0, 0, 0 };
    
            CCPoint nodePos = m_imgMan->convertTouchToNodeSpace(pTouch);
            unsigned int x = nodePos.x;
            unsigned int y = m_imgMan->getContentSize().height - nodePos.y;
    
            CCPoint point = m_imgMan->getPosition();
            //开始准备绘制
            m_pRenderTexture->begin();
            //绘制使用的临时精灵,与原图是同一图片
            CCSprite* pTempSpr = CCSprite::createWithSpriteFrame(m_imgMan->displayFrame());
            pTempSpr->setPosition(ccp(pTempSpr->getContentSize().width / 2, pTempSpr->getContentSize().height / 2));
            //绘制
            pTempSpr->visit();
            //结束绘制
            m_pRenderTexture->end();
    
            //解决方案原文 找了一下午原因 竟是这样 http://blog.csdn.net/super_level/article/details/41707687
            Director::getInstance()->getRenderer()->render();//在3.0此处必须写上这个,否则newImage整张图片都为黑色,或者在下一帧获取  
    
            //通过画布拿到这张画布上每个像素点的信息,封装到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("cur click color: alpha = %d", color4B.a);
    
            if (color4B.a > 50) {
                isTouched = true;
            }
            else {
                isTouched = false;
            }
    
            //绘制完成后清理画布的内容
            m_pRenderTexture->clear(0, 0, 0, 0);
        }
    
    
        if (m_pLabTips) {
            m_pLabTips->removeFromParentAndCleanup(true);
            m_pLabTips = NULL;
        }
    
        return isTouched;
    }
    
    void HelloWorld::onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent) {
    
        if (m_pLabTips) {
            m_pLabTips->removeFromParentAndCleanup(true);
            m_pLabTips = NULL;
        }
    
        m_pLabTips = CCLabelTTF::create("点击到非透明的像素点", "Courier", 30);
        m_pLabTips->setAnchorPoint(Vec2(0.5f, 0.5f));
        m_pLabTips->setPosition(ccp(300.0f, 100.0f));
        m_pLabTips->setColor(ccc3(150, 150, 100));
        addChild(m_pLabTips, 1);
    
    }
    
    点击白色图片 以及边缘区域 测试结果 ui_force_20001.png
    image.png image.png

    相关文章

      网友评论

        本文标题:cocos2d-x lua 3.10 不规则按钮点击(鼠标点击判

        本文链接:https://www.haomeiwen.com/subject/jdammxtx.html