日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Cocos2d-x3.0 不规则Button

發(fā)布時間:2025/6/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Cocos2d-x3.0 不规则Button 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這是我參照之前在iOS項目中用過的一個不規(guī)則形狀按鈕的第三方Button,這里用Cocos2d-x實現(xiàn)一個類似功能的按鈕。

原文地址:http://blog.csdn.net/qqmcy/article/details/26161339

代碼下載:http://download.csdn.net/detail/qqmcy/7365843

用法:

.h

// // TestScene.h // maptest // // Created by 杜甲 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);//設(shè)置tagdjShapeBtn->setShapeBtnTag(1000);djShapeBtn->setPosition(Point(0, 40));addChild(djShapeBtn);


回調(diào)函數(shù)
void TestScene::buttonCallBack(DJShapeButton *sender) {log("%d",sender->getTag()); }


DJShapeButton.h不規(guī)則形狀Button類

// // DJShapeButton.h // maptest // // Created by 杜甲 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 :public 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); private:bool isSwallow ;};#endif /* defined(__maptest__DJShapeButton__) */
DJShapeButton.cpp

// // DJShapeButton.cpp // maptest // // Created by 杜甲 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 = 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, 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);setTag(tag);}


轉(zhuǎn)載于:https://www.cnblogs.com/mqxnongmin/p/10895835.html

總結(jié)

以上是生活随笔為你收集整理的Cocos2d-x3.0 不规则Button的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。