Cocos2d-x pixel-level touch Processing

Source: Internet
Author: User

Recently I have studied pixel-level touch processing. Sometimes we use an irregular image as a button. This irregular image is a PNG image of a rectangle, it is very likely that the actual effective display content of the image only accounts for a small part of the entire PNG image, and most of the remaining content is the transparent area of the PNG image. We want to filter out this transparent area, A button response is effective only when the actual content is touched.

At the beginning, I tried to get the pixel information of the texture directly through ccsprite, but the cocos2d-x didn't provide us with an interface to get the pixel information directly through ccsprite, and studied several online demos, it is found that this effect can be achieved by re-painting with rendertexture. The code is pasted below.

# Include "helloworldscene. H "# include" simpleaudioengine. H "using namespace cocos2d; using namespace cocosdenshion; ccscene * helloworld: Scene () {// 'Scene 'is an autorelease object ccscene * scene = ccscene: Create (); // 'player' is an autorelisted object helloworld * layer = helloworld: Create (); // Add layer as a child to scene-> addchild (layer ); // return the scene return scene;} bool helloworld: I Nit () {If (! Cclayer: Init () {return false;} This-> settouchenabled (true); this-> m_imgman = ccsprite: Create ("man.png "); this-> m_imgman-> setposition (CCP (400,200); this-> addchild (this-> m_imgman, 1); this-> m_prendertexture = ccrendertexture :: create (this-> m_imgman-> getcontentsize (). width, this-> m_imgman-> getcontentsize (). height, kcctexture2dpixelformat_rgba8888); this-> m_prendertexture-> ignoreanchorpointforposition (true); this-> m_prendertexture-> setposition (CCP (400,200 )); this-> m_prendertexture-> setanchorpoint (ccpointzero); this-> addchild (this-> m_prendertexture, 0, 1); Return true;} bool helloworld: cctouchbegan (cocos2d :: cctouch * ptouch, cocos2d: ccevent * pevent) {bool istouched = false; ccpoint touchpoint = ptouch-> getlocationinview (); ccpoint glpoint = ccdirector: shareddire () -> converttogl (touchpoint); If (this-> m_imgman-> boundingbox (). containspoint (glpoint) {cccolor4b color4b = {0, 0, 0}; ccpoint nodepos = This-> m_imgman-> converttouchtonodespace (ptouch); unsigned int x = nodepos. x; unsigned int y = This-> m_imgman-> getcontentsize (). height-nodepos. y; ccpoint point = This-> m_imgman-> getposition (); // start preparing to draw this-> m_prendertexture-> begin (); // The temporary sprite used for painting, the same image as the source image is ccsprite * ptempspr = ccsprite: createwithspriteframe (this-> m_imgman-> displayframe (); ptempspr-> setposition (CCP (ptempspr-> getcontentsize (). width/2, ptempspr-> getcontentsize (). height/2); // draw ptempspr-> visit (); // end draw this-> m_prendertexture-> end (); // obtain the information of each pixel on the canvas through the canvas and encapsulate it into ccimage * pimage = This-> m_prendertexture-> newccimage (); // obtain the pixel data unsigned char * Data _ = pimage-> getdata (); unsigned int * pixel = (unsigned int *) Data _; pixel = pixel + (y * (INT) ptempspr-> getcontentsize (). width) * 1 + x * 1; // R channel color4b. R = * pixel & 0xff; // G channel color4b. G = (* pixel> 8) & 0xff; // B uses color4b. B = (* pixel> 16) & 0xff; // alpha channel. What we use is Alpha color4b. A = (* pixel> 24) & 0xff; cclog ("alpha = % d of the currently clicked vertex", color4b. a); If (color4b. a> 50) {istouched = true;} else {istouched = false;} // clear the canvas content after painting. This-> m_prendertexture-> clear (0, 0, 0, 0, 0);} If (this-> m_plabtips) {This-> m_plabtips-> removefromparentandcleanup (true); this-> m_plabtips = NULL;} return istouched;} void helloworld :: cctouchended (cocos2d: cctouch * ptouch, cocos2d: ccevent * pevent) {If (this-> m_plabtips) {This-> m_plabtips-> removefromparentandcleanup (true ); this-> m_plabtips = NULL;} This-> m_plabtips = cclabelttf: Create ("click to a non-transparent pixel", "Courier", 30 ); this-> m_plabtips-> setanchorpoint (ccpointzero); this-> m_plabtips-> setposition (CCP (300366f, 100366f); this-> m_plabtips-> setcolor (ccyellow ); this-> addchild (this-> m_plabtips, 1);} void helloworld: Pull () {ccdirector: shareddire()-> gettouchdispatcher ()-> addtargeteddelegate (this, cclayer: gettouchpriority (), false );}
When a colored point is clicked, I asked the screen to display a yellow prompt and observe the printed log information:



When a transparent black area is clicked, no text is displayed on the screen. Observe the printed log information:



Implementation principle: I re-paint the image when I click it. During the re-painting process, I can get all the pixel information on the entire canvas through rendertexture, that is, the canvas, I have the same size as the canvas, so I can ensure that every pixel on the canvas is the pixel of the image I want to draw, then, the alpha channel value of the pixel is determined to determine whether the vertex is transparent. If the pixel is transparent, no touch response is made.


This article by CC original summary, If You Need To reprint please note the Source: http://blog.csdn.net/oktears/article/details/37993871

Cocos2d-x pixel-level touch Processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.