这是我參照之前在iOS项目中用过的一个不规则形状按钮的第三方Button,这里用Cocos2d-x实现一个类似功能的按钮。

原文地址: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);//设置tagdjShapeBtn->setShapeBtnTag(1000);djShapeBtn->setPosition(Point(0, 40));addChild(djShapeBtn);

回调函数

void TestScene::buttonCallBack(DJShapeButton *sender)
{log("%d",sender->getTag());
}

DJShapeButton.h不规则形状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);}

转载于:https://www.cnblogs.com/mqxnongmin/p/10895835.html

Cocos2d-x3.0 不规则Button相关推荐

  1. cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)

    原文地址:cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)作者:七贤林子 在cocos2d  0.9及以下版本中,CCAnimation中可以使用animationWit ...

  2. DISCUZ X3.0 积分增减以及记录

    http://www.bubuko.com/infodetail-663248.html DISCUZ X3.0 积分增减以及记录 1.调用source/function/function_core. ...

  3. Discuz X3.0,X2.5最新帖子单页展示教程,附懒人包,站长基地zzjidi.com

    这是一个从X2移植过来的教程,现升级至X3.0,X2.5,教程分享给各位. DiscuzX2的版本可查看<最新回复.查看新帖单页显示 For Discuz!X2> 都知道Discuz默认的 ...

  4. cocos2d-x3.0中数据类型vector,map、value

    在3.0中,已经不再使用以前的ccarray,ccdictionary,ccint等从以前的oc继承过来的数据类型,转而加入了自己的数据结构,更加符合c++的开发习惯和思考模式,其中就包括了vecto ...

  5. Cocos2D v2.0至v3.x简洁转换指南(一)

    在该指南开头,我们假设你应经很熟悉Cocos2d 2.x版本. 我们将指出新版本重要的改变,并且给出一些你已经从Cocos2d 2.x版本中熟知的实现. CCNodes,CCScenes和CCLaye ...

  6. Cocos2d 3.0继承自Sprite的类在addChild后出现故障

    当继承自Sprite的类被addChild到其它的Node里后出现例如以下图问题,说明没有调用父类Sprite::init()的方法.由于父类Sprite里的_textureAtlas须要初始化为nu ...

  7. DiscuzX2.5,X3.0,X3.1,X3.2完整目录结构【模板目录template】

    /template/default/common  公共模板目录全局加载 block_forumtree.htm  DIY论坛树形列表模块 block_thread.htm  DIY帖子模块调用文件 ...

  8. Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现

    Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现 暂时还未有时间开发这效果,所以先贴出来. 先贴一张效果图,这是一张手机截屏: 左上方的风景图:背景图片 右上方的人物图:前景图 ...

  9. 7、Cocos2dx 3.0游戏开发找小三之3.0版本号的代码风格

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27691337 Cocos2d-x代码风格 前面我们已 ...

最新文章

  1. 主从配置_MySQL主从复制配置详解
  2. 什么是JAVA内容仓库(Java Content Repository)(3)
  3. 命令行是你的至交好友
  4. SAP ABAP ALV(LVC)的一个自定义事件(F4帮助事件,回车ENTER按钮事件)的一个实例
  5. luogu P5304 [GXOI/GZOI2019]旅行者
  6. mysql数据库的各种锁_关于MySQL各类锁的总结
  7. StyleGAN如何定制人脸生成
  8. 用随机投点法计算pi值matlab,用蒙特卡洛法求圆周率PI的近似值,其算法如下:使用随机函数Rnd,随机生成两个[0,1)之间的随机数,由此构成一个投点坐标(x,y),然后根据x...
  9. ios弧形进度条_iOS手把手教你实现圆形进度条
  10. fields and vector spaces
  11. Matlab中tic和toc用法
  12. 关于react、vue的一些问题
  13. [mysql] ERROR 2002 (HY000): Can‘t connect to local MySQL server...问题解决
  14. Pinta 1.6发布!在 Ubuntu 和 Linux Mint 上安装
  15. 我的世界服务器修改npc指令,我的世界自定义npc指令 | 手游网游页游攻略大全
  16. linux系统fflush函数
  17. Spring(3)--Spring示例再演示
  18. 汽车芯片短缺潮“拐点”已至?下一波“网络安全”升级战悄然开始
  19. csdn图片去水印 | 其他方法无效时的解决方案
  20. python 提取pdf表格_用Python提取pdf文件中的表格数据

热门文章

  1. Oracle11g R2学习系列 之六数据库链接,快照及序列
  2. Sqlite c/c++ api 学习
  3. vs.net设计器里加载派生窗口类时要执行父窗口的Form_Load方法
  4. Jobs(三) HTML的form表单提交中文后,后台取出乱码的问题
  5. 内存泄露问题改进(转自vczh)
  6. (转载)VS2010/MFC编程入门之五十四(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)...
  7. angularJS的controller之间如何正确的通信
  8. mybatis generator用法
  9. arm-linux-readelf
  10. 创业故事:腾讯的创始人们