【转】CCScale9Sprite和CCControlButton
转自:http://blog.csdn.net/nat_myron/article/details/12975145
在2dx下用到了android下的.9.png图片,下面是原图
查了一下2dx里有CCScale9Sprite,直接贴上背景图,毫无问题,
- CCSize bgRect = CCSizeMake(size.width,size.height/3);
- CCScale9Sprite *background = CCScale9Sprite::create("dialog_bg.png");
- background->setContentSize(bgRect);
- background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
- this->addChild(background,5);
CCSize bgRect = CCSizeMake(size.width,size.height/3);
CCScale9Sprite *background = CCScale9Sprite::create("dialog_bg.png");
background->setContentSize(bgRect);
background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
this->addChild(background,5);
然后按钮里也需要用到这个素材图,拉伸图片到我们需要的,用到了CCControlButton
- CCLabelTTF *title1 = CCLabelTTF::create("拍照", "Marker Felt", 30);
- CCControlButton* btn_takephoto = CCControlButton::create(title1,CCScale9Sprite::create("dialog_normal.png"));
- /* 设置按钮按下时的图片 */
- btn_takephoto->setBackgroundSpriteForState(CCScale9Sprite::create("dialog_pressed.png"), CCControlStateSelected);
- btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
- btn_takephoto->setPreferredSize(btnRect);
- btn_takephoto->addTargetWithActionForControlEvents(this, cccontrol_selector(DialogPhoto::MenuItemCallback), CCControlEventTouchUpInside);
- background->addChild(btn_takephoto);
CCLabelTTF *title1 = CCLabelTTF::create("拍照", "Marker Felt", 30);
CCControlButton* btn_takephoto = CCControlButton::create(title1,CCScale9Sprite::create("dialog_normal.png"));/* 设置按钮按下时的图片 */
btn_takephoto->setBackgroundSpriteForState(CCScale9Sprite::create("dialog_pressed.png"), CCControlStateSelected);
btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
btn_takephoto->setPreferredSize(btnRect);
btn_takephoto->addTargetWithActionForControlEvents(this, cccontrol_selector(DialogPhoto::MenuItemCallback), CCControlEventTouchUpInside);
background->addChild(btn_takephoto);
当然也可以用CCMenuItemSprite
- CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
- sp1->setContentSize(btnRect);
- CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
- sp2->setContentSize(btnRect);
- CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1,sp2,this,menu_selector(DialogShare::MenuItemCallback));
- btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
- btn_takephoto->setTag(DialogTakePhoto);
- CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
- pLabel1->setColor(ccc3(0, 0, 0));
- pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_takephoto->addChild(pLabel1);
CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
sp1->setContentSize(btnRect);
CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
sp2->setContentSize(btnRect);
CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1,sp2,this,menu_selector(DialogShare::MenuItemCallback));
btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
btn_takephoto->setTag(DialogTakePhoto);
CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
pLabel1->setColor(ccc3(0, 0, 0));
pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
btn_takephoto->addChild(pLabel1);
- <pre class="cpp" name="code">m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
- m_pMenu->setPosition(CCPointZero);
- background->addChild(m_pMenu);</pre>
- m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
- m_pMenu->setPosition(CCPointZero);
- background->addChild(m_pMenu);
m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
m_pMenu->setPosition(CCPointZero);
background->addChild(m_pMenu);
下面就是我们所需的效果
这里用到了对话框的思想,点击按钮之后从底部弹出菜单,下面贴出全部代码
- #pragma once
- #include "cocos2d.h"
- #include "HelloWorldScene.h"
- #include "cocos-ext.h"
- USING_NS_CC_EXT;
- USING_NS_CC;
- class DialogShare: public CCLayerColor
- {
- // 模态对话框菜单
- CCMenu *m_pMenu;
- // 记录菜单点击
- bool m_bTouchedMenu;
- public:
- DialogShare();
- ~DialogShare();
- static cocos2d::CCScene* scene();
- virtualbool init();
- // 初始化对话框内容
- void initDialog();
- CREATE_FUNC(DialogShare);
- void onEnter();
- void onExit();
- virtualbool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
- virtualvoid ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
- virtualvoid ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
- virtualvoid ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
- void MenuItemCallback(CCObject *pSender);
- };
#pragma once#include "cocos2d.h"
#include "HelloWorldScene.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;class DialogShare: public CCLayerColor
{// 模态对话框菜单CCMenu *m_pMenu;// 记录菜单点击bool m_bTouchedMenu;
public:DialogShare();~DialogShare();static cocos2d::CCScene* scene();virtual bool init();// 初始化对话框内容void initDialog();CREATE_FUNC(DialogShare);void onEnter();void onExit();virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);void MenuItemCallback(CCObject *pSender);
};
- #include "DialogShare.h"
- #include "HelloWorldScene.h"
- enum {
- DialogTakePhoto,
- DialogPhotoAlbum,
- DialogCancel,
- };
- CCScene* DialogShare::scene()
- {
- CCScene *scene = CCScene::create();
- DialogShare *layer = DialogShare::create();
- scene->addChild(layer);
- return scene;
- }
- DialogShare::DialogShare()
- {
- }
- DialogShare::~DialogShare()
- {
- }
- bool DialogShare::init()
- {
- bool bRet = false;
- do {
- CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 125)));
- this->initDialog();
- bRet = true;
- } while (0);
- return bRet;
- }
- void DialogShare::initDialog()
- {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCSize bgRect = CCSizeMake(size.width,size.height/3);
- CCScale9Sprite *background = CCScale9Sprite::create("dialog_bg.png");
- background->setContentSize(bgRect);
- background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
- this->addChild(background,5);
- CCSize btnRect = CCSizeMake(bgRect.width/2,bgRect.height/5);
- CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
- sp1->setContentSize(btnRect);
- CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
- sp2->setContentSize(btnRect);
- CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1
- ,sp2,this,menu_selector(DialogShare::MenuItemCallback));
- btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
- btn_takephoto->setTag(DialogTakePhoto);
- CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
- pLabel1->setColor(ccc3(0, 0, 0));
- pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_takephoto->addChild(pLabel1);
- CCScale9Sprite* sp3 = CCScale9Sprite::create("dialog_normal.png");
- sp3->setContentSize(btnRect);
- CCScale9Sprite* sp4 = CCScale9Sprite::create("dialog_pressed.png");
- sp4->setContentSize(btnRect);
- CCMenuItemSprite* btn_photoalbum = CCMenuItemSprite::create(sp3
- ,sp4,this,menu_selector(DialogShare::MenuItemCallback));
- btn_photoalbum->setPosition(ccp(bgRect.width/2,bgRect.height/5*5/2));
- btn_photoalbum->setTag(DialogPhotoAlbum);
- CCLabelTTF* pLabel2 = CCLabelTTF::create("相册中选取", "Arial", 18);
- pLabel2->setColor(ccc3(0, 0, 0));
- pLabel2->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_photoalbum->addChild(pLabel2);
- CCScale9Sprite* sp5 = CCScale9Sprite::create("dialog_cancel_normal.png");
- sp5->setContentSize(btnRect);
- CCScale9Sprite* sp6 = CCScale9Sprite::create("dialog_pressed.png");
- sp6->setContentSize(btnRect);
- CCMenuItemSprite* btn_cancel = CCMenuItemSprite::create(sp5
- ,sp6,this,menu_selector(DialogShare::MenuItemCallback));
- btn_cancel->setPosition(ccp(bgRect.width/2,bgRect.height/5));
- btn_cancel->setTag(DialogCancel);
- CCLabelTTF* pLabel3 = CCLabelTTF::create("取消", "Arial", 16);
- pLabel3->setColor(ccc3(0, 0, 0));
- pLabel3->setPosition(ccp(btnRect.width/2,btnRect.height/2));
- btn_cancel->addChild(pLabel3);
- m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
- m_pMenu->setPosition(CCPointZero);
- background->addChild(m_pMenu);
- background->runAction(CCEaseExponentialOut::create(CCMoveTo::create(0.5f,ccp(bgRect.width/2,bgRect.height/2))));
- }
- void DialogShare::onEnter()
- {
- CCLayerColor::onEnter();
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,kCCMenuHandlerPriority-1, true);
- }
- void DialogShare::onExit()
- {
- CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
- CCLayerColor::onExit();
- }
- bool DialogShare::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch, pEvent);
- returntrue;
- }
- void DialogShare::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (m_bTouchedMenu) {
- m_pMenu->ccTouchMoved(pTouch, pEvent);
- }
- }
- void DialogShare::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (m_bTouchedMenu) {
- m_pMenu->ccTouchEnded(pTouch, pEvent);
- m_bTouchedMenu = false;
- }
- }
- void DialogShare::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- if (m_bTouchedMenu) {
- m_pMenu->ccTouchEnded(pTouch, pEvent);
- m_bTouchedMenu = false;
- }
- }
- void DialogShare::MenuItemCallback(cocos2d::CCObject *pSender)
- {
- CCMenuItemImage* button=(CCMenuItemImage*)pSender;
- switch (button->getTag())
- {
- case DialogTakePhoto:
- CCLog("DialogTakePhoto++++++");
- break;
- case DialogPhotoAlbum:
- CCLog("DialogPhotoAlbum++++++");
- break;
- case DialogCancel:
- CCLog("DialogCancel++++++");
- this->removeFromParentAndCleanup(true);
- break;
- }
- }
【转】CCScale9Sprite和CCControlButton相关推荐
- 1cocos2dx扩展库UI控件,CCControlSlider,CCScale9Sprite(九妹图),CCControlSwitch,CCControlButton
UI控件来自cocos2dx的扩展库,完善了UI方面的元素,使cocos2dx更加丰富多彩.使用扩展库需包含: #include "cocos-ext.h" USING_NS ...
- CCControlSwitch 、CCControlSlider、CCControlButton
/**bool hasMoved(); 这里获取的不是开关是否正在被用户拨动,而是开关最终的状态是由用户手动拨动开关进行的,*还是用户点击开关进行的状态更改*/CCControlSwitch* pSw ...
- cocos2d-x按钮CCControlButton的用法
转载请注明,原文地址: http://blog.csdn.net/musicvs/article/details/8242963 1. 按钮创建 [cpp] view plaincopyprint? ...
- cocos2d-x中CCScale9Sprite的另一种实现
2019独角兽企业重金招聘Python工程师标准>>> cocos2d 2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间 ...
- cocos2d-x2.2九宫格CCScale9Sprite有bug
使用Cocosbuilder, 添加九宫格控件,然后设置图片,如果图片是从plist中加载的,在游戏中就可能有问题. 当plist中的图片旋转过的话,在游戏中就会显示错误. 找了大半天,终于发现是CC ...
- cocos2d-x编程之CCScale9Sprite典型用法
cocos2d-x编程之CCScale9Sprite典型用法 STEP 1:使用PHOTOSHOP制作原始的小的图片(SIZE: 200 x100),如下图所示. STEP 2: C++编码. CCS ...
- Cocos2d-x 九宫格 CCScale9Sprite
文章转载自:http://www.cnblogs.com/sevenyuan/p/3195688.html 在cocos2d-x引擎中,CCScale9Sprite这个文件的位置为: 一.九宫格的实现 ...
- cocos2d-x 使用 CCScale9Sprite 实现微信对话框
1.有底框的文字 微信的对话框特点在于它有一个可变大小的底图衬托,显得非常舒服,怎么实现的呢?先上图,然后慢慢说: 2.什么是九宫格构图? 有个概念叫九宫格构图,说的就是这种会变化背 ...
- cocos2d 解密ccbi_cocos2d-x高级学习
弱联网开发技术: libcurl 添加lib文件:libcurl_imp.lib pthreadVCE2.lib 添加头文件:#include"curl/curl.h" curl ...
最新文章
- 想让进程后台运行,试试Linux的nohup命令,3分钟学会。
- SLAM: 图像角点检测的Fast算法(OpenCV文档)
- 标签 href 怎么拼接_【微信】用户-标签的兴趣建模
- C++获取当前所有进程的完整路径
- solidworks属性管理器_老式经典|如何提高SOLIDWORKS的运行速度
- python flask webapi_在将Python Flask webapi部署到azurep时遇到依赖性问题
- java如何快速抛出异常,异常 - 如何抛出异常 - 《Java 编程要点(Essential Java)》 - 书栈网 · BookStack...
- php微信接口调用,PHP调用微信接口报错
- 评定数据之间的相似度指标----距离
- android 远程控制工具,安卓版远程协助软件
- 1人30天44587行代码,分享舍得网开发过程
- png批量转换成jpg
- 用无线局域网设置服务器,无线局域网怎样配置
- 正定方言—正定少占鱼欢迎您,快速做个正定人
- 科学解释超级计算机,人工智能、现代科学证明:宇宙是虚拟的,真实世界是超级计算机...
- matlab中exp(liangx),Matlab基础知识
- 线程池:酷我音乐网站热歌排行榜里面的歌曲!
- nRF52833及蓝牙5系列NRF52832低功耗蓝牙芯片对比
- qu32调音台说明书_使用QU32调音台录制辩论赛音频
- chrome控制台中console
热门文章
- 解决Eclipse无法添加Tomcat服务器的问题
- 全网最详细的HBase启动以后,HMaster进程启动了,几秒钟以后自动关闭问题的解决办法(图文详解)
- Hive数据倾斜解决方法总结
- Bash脚本和/ bin / bash ^ M:错误的解释器:没有这样的文件或目录[重复]
- python数据模型_#PYTHON#数据模型 | 学步园
- 常见移动机器人轮直径校准
- 电脑无限重启rpc服务器不可用,StarUML启动时候出现System Error. Code:1722. RPC服务器不可用.错误的解决办法...
- 解决属性名和字段名不一致的问题(Mybatis)
- vue 引入自定义js方法并调用
- 海康威视相机SDK开发(1个相机)