This is a third-party Button that I used to refer to an irregular Button in the iOS project, where a Button with similar functionality is implemented with a Cocos2d-x.
Address: http://blog.csdn.net/qqmcy/article/details/26161339
Code download: http://download.csdn.net/detail/qqmcy/7365843
Usage:
. H
/// TestScene. h // maptest /// Created by du jia on 14-5-18. /// # ifndef _ maptest _ TestScene __# define _ maptest _ TestScene __# include "cocos2d. h "# include" ui/CocosGUI. h "# include" DJShapeButton. h "USING_NS_CC; class TestScene: public Layer, public DJShapeButtonDelegate {public: static Scene * createScene (); virtual bool init (); CREATE_FUNC (TestScene ); void buttonCallBack (DJShapeButton * sender) ;}; # endif/* defined (_ maptest _ TestScene __)*/
DJShapeButton * djShapeBtn = DJShapeButton: create ("res/red/1300_r.png"); djShapeBtn-> setDelegate (this); // set the tag djShapeBtn-> setShapeBtnTag (1000 ); djShapeBtn-> setPosition (Point (0, 40); addChild (djShapeBtn );
Callback Function
void TestScene::buttonCallBack(DJShapeButton *sender){ log("%d",sender->getTag());}
DJShapeButton. h irregular Button class
//// DJShapeButton. h // maptest /// Created by du jia on 14-5-18. /// # ifndef _ maptest _ DJShapeButton __# define _ maptest _ DJShapeButton _ class DJShapeButton; class completion {public: virtual void buttonCallBack (DJShapeButton * sender) = 0 ;};# include "cocos2d. h "# include" ui/CocosGUI. h "USING_NS_CC; class DJShapeButton: public ui: Layout {public: static DJShapeButton * create (const std: string & normalImage); CC_SYNTHESIZE (interval *, _ delegateBtn, Delegate ); virtual bool init (const std: string & normalImage); CC_SYNTHESIZE_RETAIN (Sprite *, shapeBtn, ShapeBtn); void setShapeBtnTag (int tag); private: bool isSwallow ;}; # endif/* defined (_ maptest _ DJShapeButton __)*/
DJShapeButton. cpp
//// DJShapeButton. cpp // maptest /// Created by du jia on 14-5-18. //// # include "DJShapeButton. h "DJShapeButton * DJShapeButton: create (const std: string & normalImage) {DJShapeButton * pRet = new DJShapeButton (); if (pRet & pRet-> init (normalImage )) {pRet-> autorelease (); return pRet;} else {delete pRet; pRet = NULL; return NULL ;}} bool DJShapeButton: init (const std: string & normalImage) {bool bRet = fal Se; do {CC_BREAK_IF (! Ui: Layout: init (); Image * myImg = new Image (); myImg-> initWithImageFile (normalImage); Texture2D * temp = new Texture2D (); temp-> initWithImage (myImg); shapeBtn = Sprite: createWithTexture (temp); log ("% f, % f", shapeBtn-> getAnchorPoint (). x, shapeBtn-> getAnchorPoint (). y); log ("width = % f, height = % f", shapeBtn-> getContentSize (). width, shapeBtn-> getContentSize (). height); addChild (shapeBtn); this-> setSize (ShapeBtn-> getContentSize (); shapeBtn-> setPosition (Point (getSize (). width/2, getSize (). height/2); auto listener1 = EventListenerTouchOneByOne: create (); listener1-> onTouchBegan = [=] (Touch * touch, Event * event) {auto target = static_cast <Sprite *> (event-> getCurrentTarget (); Point locationInNode = target-> convertToNodeSpace (touch-> getLocation (); Color4B c = {0, 0, 0, 0}; Point pt = Point (low.o NInNode. x, target-> getContentSize (). height-locationInNode. y); Size s = target-> getContentSize (); Rect rect = Rect (0, 0, s. width, s. height); if (rect. containsPoint (locationInNode) {log ("sprite began... x = % f, y = % f ", locationInNode. x, locationInNode. y); unsigned int x = pt. x, y = pt. y; unsigned char * data = myImg-> getData (); unsigned int * pixel = (unsigned int *) data; pixel = pixel + (y * 116) + X; c. r = * pixel & 0xff; c. g = (* pixel> 8) & 0xff; c. B = (* pixel> 16) & 0xff; c. a = (* pixel> 24) & 0xff; log ("8888888888% d", c. a); if (c. a <= 4) {isSwallow = false;} else {isSwallow = true;} else {isSwallow = false;} listener1-> setSwallowTouches (isSwallow); return isSwallow ;}; listener1-> onTouchEnded = [=] (Touch * touch, Event * event) {if (_ delegateBtn! = Nullptr) {_ delegateBtn-> buttonCallBack (this) ;};_ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener1, shapeBtn); bRet = true;} while (0 ); return bRet;} void DJShapeButton: setShapeBtnTag (int tag) {shapeBtn-> setTag (tag );}