Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2dx Widget 按钮透明区域过滤大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

小伟哥 遇到一个命题:

按钮透明区域过滤。当点击一个建筑按钮、花的时候不得不想一些方法把点击透明区域过滤掉。

让点击也没有效果滴啦。

开始搜索了半天才有所思路。

在网络上很多贴代码的。

http://blog.csdn.net/lwuit/article/details/40658347

整理后代码如下:

bool CCMenu::checkAlphaPoint(CCMenuItem* pChild,const cCPoint& point)
{
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    CCNode* SELEctSprite = ((CCMenuItemSprite*)pChild)->getSELEctedImage();
    
    CCRenderTexture *renderer = CCRenderTexture::create(winSize.width,winSize.height);
    renderer->begin();
    
    bool visible = SELEctSprite->isVisible();
    if (visiblE) {
        SELEctSprite->visit();
    }
    else
    {
        SELEctSprite->setVisible(true);
        SELEctSprite->visit();
        SELEctSprite->setVisible(false);
    }
    
    GLubyte pixelColors[4];
    
#if ( CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    glReadPixels(point.x,point.y,1,GL_RGBA,GL_UNSIGNED_BYTE,&pixelColors[0]);
#else
    glReadPixels(point.x,GL_ALPHA,&pixelColors[0]);
#endif
    
    int alpha = pixelColors[0];
    CCLOG("----alpha %d",alpha);
    
    renderer->end();
    
    if (alpha <= 30)
    {
       return true;
    }
    else
    {
        return false;
    }
    
}

上面代码的确在测试工程上面直接简历个ccsprite 活着 menuitem 是可以执行的。


随着UI工具的进步。我们选择了Cocostudio 的 Widget 。方便了你我啊。

但是可但是,把上面的代码贴过来,试了试真心不能用啊。


有些同志,到此放弃了对知识原理的探究。

程序就是苦啊。遇到这样的问题必须往下研究不是?

经过了多重推敲与图纸推测。

后来发现了出现问题的根本原因:

CCRenderTexture *renderer 渲染后不能得到位置上面的颜色值 为0 00000为什么为0
visit()好不好使?各种疑惑
bool Widget::onTouchBegan(CCTouch *touch,CCEvent *unused_event)
{
    _touchStartPos = touch->getLOCATIOn();
    _hitted = isEnabled()
            & isTouchEnabled()
            & hitTest(_touchStartPos)
    & clippingParentAreaContainPoint(_touchStartPos);
    
    if (!_hitted)
    {
        return false;
    }
    
    // add yww alpha check
    if (!AlphaTouchcheck(_touchStartPos))
    {
        return false;
    }
    
    setFocused(true);
    Widget* widgetParent = getWidgetParent();
    if (widgetParent)
    {
        widgetParent->checkChildInfo(0,this,_touchStartPos);
    }
    pushDownevent();
    return !_touchPassedEnabled;
}

上面是按键检测的逻辑。


下面是修改过的代码。原理很简单 在widget 里面ccnode节点 节点位置 相对父节点是0. 所以在visit的时候 位置就从0,0 开始了。

我们矫正下改渲染节点的位置。转成屏幕坐标 然后在根据touch 坐标获取当前点击像素的 透明值。


// yww get alpha touch event check
bool Button::AlphaTouchcheck(const cCPoint &point)
{
    bool isTouchClaimed = false;
    
    if (getAlphaTouchEnable())
    {
            // check claimed touch arena
            CCSize winSize = CCDirector::sharedDirector()->getWinSize();
            CCSprite* SELEctSprite = (CCSprite*)getVirtualRenderer();
            CCPoint cutPos = SELEctSprite->getPosition();
            // CCLOG("getAlphaTouchEnable SELEctSprite X %f,Y %f",cutPos.x,cutPos.y);
        
            // get screen point
            CCPoint wordpx = SELEctSprite->getParent()->convertToWorldSpace(cutPos);
            // CCLOG("getAlphaTouchEnable convertToWorldSpace X %f,wordpx.x,wordpx.y);
        
            SELEctSprite->setPosition(wordpX);
        
            CCRenderTexture *renderer = CCRenderTexture::create(winSize.width,winSize.height);
            //SELEctSprite->addChild(renderer);
        
            renderer->begin();
        
            bool visible = SELEctSprite->isVisible();
            if (visiblE)
            {
                SELEctSprite->visit();
            }
            else
            {
                SELEctSprite->setVisible(true);
                SELEctSprite->visit();
                SELEctSprite->setVisible(false);
            }
        
            GLubyte pixelColors[4];
        
#if ( CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
            glReadPixels(point.x,&pixelColors[0]);
#else
            glReadPixels(point.x,&pixelColors[0]);
#endif
        
            int alpha = pixelColors[0];
            CCLOG("----alpha %d",alpha);
        
            renderer->end();
        
            SELEctSprite->setPosition(cutPos);
        
            if (alpha <= 20)
            {
                isTouchClaimed = false;
            }
            else
            {
                isTouchClaimed = true;
            }
        // check claimed touch arena
    }
    else
    {
        isTouchClaimed = true;
    }
    return isTouchClaimed;
}

上面逻辑是 重写了widget 的自定义函数

AlphaTouchcheck

这个根据自己的需求构建结构了。

在lua里面可以提供检测开关 是否对透明纸进行检测咯。

不多往下说了。浪费网络内存咯。

大佬总结

以上是大佬教程为你收集整理的Cocos2dx Widget 按钮透明区域过滤全部内容,希望文章能够帮你解决Cocos2dx Widget 按钮透明区域过滤所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。