Cocos2dx Widget 按鈕透明地區過濾,cocos2dxwidget
小偉哥 遇到一個命題:
按鈕透明地區過濾。當點擊一個建築按鈕、花的時候不得不想一些方法把點擊透明地區過濾掉。
讓點擊也沒有效果滴啦。
開始搜尋了半天才有所思路。
在網路上很多貼代碼的。
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, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColors[0]);#else glReadPixels(point.x, point.y, 1, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &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 checkbool 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, Y %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, point.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColors[0]);#else glReadPixels(point.x, point.y, 1, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &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裡面可以提供檢測開關 是否對透明紙進行檢測咯。
不多往下說了。浪費網路記憶體咯。