This article focuses on how to make an irregular button, such as gossip, click on the green and click on the Red section, you need to perform different events
In general, if you want to detect whether an elf is in the dot, the procedure is as follows
BOOL Helloworld::cctouchbegan (cocos2d::cctouch* Ptouch, cocos2d::ccevent* pevent) { ccsize sprSize = pSpr-> Getcontentsize (); Ccpoint point = Pspr->converttouchtonodespace (Ptouch); if (ccrectmake (0, 0, Sprsize.width, sprsize.height). Containspoint (point)) { Cclog ("dots"); } return true;}
The above method is judged by the rectangular area, including the transparent part of the Click is also valid, if you want to make the above gossip button, it is not easy to do.
This leads to the implementation of the following, to do two pictures, the two pictures as large, stacked together is exactly a complete gossip.
Click to determine whether the area of the click is transparent, if not transparent response to the event! If it is transparent, pass it down.
How do I get the clicked area transparent? Create a ccrendertexture, draw the current sprite, and then get a ccimage from the ccrendertexture, and then take the pixel information from a certain point in the ccimage.
To get the point is transparent, I encapsulated two functions, the implementation code is as follows
BOOL Helloworld::cctouchbegan (cocos2d::cctouch* Ptouch, cocos2d::ccevent* pevent) { ccsize sprSize = pSpr-> Getcontentsize (); Ccpoint point = Pspr->converttouchtonodespace (Ptouch); if (ccrectmake (0, 0, Sprsize.width, sprsize.height). Containspoint (point)) { int i = Getopacitywithpoint (PSPR, point ); if (i > 0) { cclog ("dots");} } return true;}
int Helloworld::getopacitywithpoint (Cocos2d::ccsprite *pspr, Cocos2d::ccpoint point) { Point.y = pSpr-> Getcontentsize (). Height-point.y; Ccimage * pimage = Createimagefromsprite (PSPR); cccolor4b C = pimage->getcolor4b (Point.x, point.y); Cclog ("%d,%d,%d,%d", C.R, C.G, C.B, C.A); return C.A;} cocos2d::ccimage* helloworld::createimagefromsprite (Cocos2d::ccsprite *pspr) { ccsprite* PNEWSPR = CCSprite:: Createwithspriteframe (Pspr->displayframe ()); Pnewspr->setanchorpoint (Ccpointzero); ccrendertexture* Prender = Ccrendertexture::create (Pnewspr->getcontentsize (). width, pnewspr->getcontentsize ( ). height, kcctexture2dpixelformat_rgba8888); Prender->begin (); Pnewspr->visit (); Prender->end (); return Prender->newccimage ();}
Forgot to post the code that was added to the ccimage.
Add the following function to the CCImage.h file
Public: cccolor4b getcolor4b (float x, float y) { cccolor4b color = {0, 0, 0, 0}; int IX = (int) x-1; int iy = (int) y-1; M_pdata + = (iy*getwidth () + ix) * 4; COLOR.R = * (m_pdata++); COLOR.G = * (m_pdata++); color.b = * (m_pdata++); COLOR.A = * (m_pdata++); return color; };
Turn.... An irregular button is not available for cocos2dx3.0 or later but the idea should be this way.