This is the third-party button that I used to refer to an irregular shape button in iOS projects, where a similar function is implemented with Cocos2d-x.
Original address: http://blog.csdn.net/qqmcy/article/details/26161339
Code Download: http://download.csdn.net/detail/qqmcy/7365843
How to use:
. h
testscene.h// maptest//// Created by du Jia Li 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:p ublic 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 Tag djshapebtn->setshapebtntag (+); Djshapebtn->setposition (Point (0, +)); AddChild (DJSHAPEBTN);
callback function
void Testscene::buttoncallback (Djshapebutton *sender) { log ("%d", Sender->gettag ());}
DJShapeButton.h Irregular Shape Button class
djshapebutton.h// maptest//// Created by du Jia Li on 14-5-18.////#ifndef __maptest__djshapebutton__# Define __maptest__djshapebutton__class djshapebutton;class djshapebuttondelegate {public : virtual void Buttoncallback (djshapebutton* sender) = 0; }; #include "cocos2d.h" #include "ui/cocosgui.h" Using_ns_cc;class Djshapebutton:p ublic ui::layout{public : Static djshapebutton* Create (const std::string& normalimage); Cc_synthesize (djshapebuttondelegate*, _delegatebtn, Delegate); virtual BOOL Init (const std::string& normalimage); Cc_synthesize_retain (sprite*, shapebtn, shapebtn); void Setshapebtntag (int tag);p rivate: bool isswallow; }; #endif/* Defined (__maptest__djshapebutton__) */
DJShapeButton.cpp
djshapebutton.cpp//maptest////Created by du Jia Li on 14-5-18.////#include "DJShapeButton.h" djshapebutton* Djshapebutto N::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 = false; 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 (locationinnode.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, Locationi NNODE.Y); unsigned int x = pt.x, y = pt.y; unsigned char* data = Myimg->getdata (); unsigned int* pixel = (unsigned int*) data; Pixel = pixel + (Y *) + x; C.R = *pixel & 0xFF; C.G = (*pixel >> 8) & 0xFF; C.B = (*pixel >>) & 0xFF; C.A = (*pixel >>) & 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); Settag (tag); }