Original article. For more information, see the source!
In the game, it is sometimes used to determine whether to click the colored part of a picture, that is, the non-transparent area; or to determine whether the collision of irregular images is judged.
// Determine whether there is any point to the part with materials. The point is relative to the point where the CCSprite coordinates bool Tools: isClickTheRealSprite (cocos2d: CCSprite * p_sprite, const cocos2d: CCPoint p_point) {CCSize l_sizeInPixel = p_sprite-> getContentSize (); // get the relative offset int l_iX = (int) (l_sizeInPixel.width/2.0f + p_point.x) in the lower left corner of Sprite; int l_iY = (int) (l_sizeInPixel.height/2.0f + (int) p_point.y); CCPoint l_pointBL = ccp (l_iX, l_iY); CCRect l_rect = CCRect (0, 0, l_sizeInPixel. Width, l_sizeInPixel.height); if (! (Maid (l_rect.containsPoint (l_pointBL) {return false;} // open out the empty memory buffer // unsigned char * l_buffer = (unsigned char *) malloc (l_sizeInPixel.width * l_sizeInPixel.height * 4 ); // memset (l_buffer, 0, l_sizeInPixel.width * l_sizeInPixel.height * 4); uint8_t l_buffer [4]; // write memory CCSize size = p_sprite-> getParent ()-> getContentSize (); CCRenderTexture * l_renderTexture = CCRenderTexture: create (values, l_sizeInPixel.height, values); // l_renderTexture-> begin (); l_renderTexture-> beginWithClear (0, 0, 0, 0 ); p_sprite-> draw (); glReadPixels (l_pointBL.x, l_pointBL.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, l_buffer); l_renderTexture-> end (); // transparency // int l_iValue_A = l_buffer [4 * (int) (l_pointBL.y * l_sizeInPixel.width + l_pointBL.x) + 3]; int l_iValue_A = l_buffer [3]; // free (l_buffer); if (l_iValue_A <5) {return false;} return true ;}
P_point is the offset relative to the Sprite anchpoint.
Record for memo!