转自:http://blog.csdn.net/nat_myron/article/details/12975145

在2dx下用到了android下的.9.png图片,下面是原图

 

查了一下2dx里有CCScale9Sprite,直接贴上背景图,毫无问题,

  1. CCSize bgRect = CCSizeMake(size.width,size.height/3);
  2. CCScale9Sprite *background   = CCScale9Sprite::create("dialog_bg.png");
  3. background->setContentSize(bgRect);
  4. background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
  5. 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

  1. CCLabelTTF *title1 = CCLabelTTF::create("拍照", "Marker Felt", 30);
  2. CCControlButton* btn_takephoto = CCControlButton::create(title1,CCScale9Sprite::create("dialog_normal.png"));
  3. /* 设置按钮按下时的图片 */
  4. btn_takephoto->setBackgroundSpriteForState(CCScale9Sprite::create("dialog_pressed.png"), CCControlStateSelected);
  5. btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
  6. btn_takephoto->setPreferredSize(btnRect);
  7. btn_takephoto->addTargetWithActionForControlEvents(this, cccontrol_selector(DialogPhoto::MenuItemCallback), CCControlEventTouchUpInside);
  8. 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

  1. CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
  2. sp1->setContentSize(btnRect);
  3. CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
  4. sp2->setContentSize(btnRect);
  5. CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1,sp2,this,menu_selector(DialogShare::MenuItemCallback));
  6. btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
  7. btn_takephoto->setTag(DialogTakePhoto);
  8. CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
  9. pLabel1->setColor(ccc3(0, 0, 0));
  10. pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
  11. 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);
  1. <pre class="cpp" name="code">m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
  2. m_pMenu->setPosition(CCPointZero);
  3. background->addChild(m_pMenu);</pre>
  1. m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
  2. m_pMenu->setPosition(CCPointZero);
  3. background->addChild(m_pMenu);
m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
m_pMenu->setPosition(CCPointZero);
background->addChild(m_pMenu);

下面就是我们所需的效果

这里用到了对话框的思想,点击按钮之后从底部弹出菜单,下面贴出全部代码

  1. #pragma once
  2. #include "cocos2d.h"
  3. #include "HelloWorldScene.h"
  4. #include "cocos-ext.h"
  5. USING_NS_CC_EXT;
  6. USING_NS_CC;
  7. class DialogShare: public CCLayerColor
  8. {
  9. // 模态对话框菜单
  10. CCMenu *m_pMenu;
  11. // 记录菜单点击
  12. bool m_bTouchedMenu;
  13. public:
  14. DialogShare();
  15. ~DialogShare();
  16. static cocos2d::CCScene* scene();
  17. virtualbool init();
  18. // 初始化对话框内容
  19. void initDialog();
  20. CREATE_FUNC(DialogShare);
  21. void onEnter();
  22. void onExit();
  23. virtualbool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
  24. virtualvoid ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
  25. virtualvoid ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
  26. virtualvoid ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
  27. void MenuItemCallback(CCObject *pSender);
  28. };
#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);
};
  1. #include "DialogShare.h"
  2. #include "HelloWorldScene.h"
  3. enum {
  4. DialogTakePhoto,
  5. DialogPhotoAlbum,
  6. DialogCancel,
  7. };
  8. CCScene* DialogShare::scene()
  9. {
  10. CCScene *scene = CCScene::create();
  11. DialogShare *layer = DialogShare::create();
  12. scene->addChild(layer);
  13. return scene;
  14. }
  15. DialogShare::DialogShare()
  16. {
  17. }
  18. DialogShare::~DialogShare()
  19. {
  20. }
  21. bool DialogShare::init()
  22. {
  23. bool bRet = false;
  24. do {
  25. CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 125)));
  26. this->initDialog();
  27. bRet = true;
  28. } while (0);
  29. return bRet;
  30. }
  31. void DialogShare::initDialog()
  32. {
  33. CCSize size = CCDirector::sharedDirector()->getWinSize();
  34. CCSize bgRect = CCSizeMake(size.width,size.height/3);
  35. CCScale9Sprite *background   = CCScale9Sprite::create("dialog_bg.png");
  36. background->setContentSize(bgRect);
  37. background->setPosition(ccp(bgRect.width/2,-bgRect.height/2));
  38. this->addChild(background,5);
  39. CCSize btnRect = CCSizeMake(bgRect.width/2,bgRect.height/5);
  40. CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png");
  41. sp1->setContentSize(btnRect);
  42. CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png");
  43. sp2->setContentSize(btnRect);
  44. CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1
  45. ,sp2,this,menu_selector(DialogShare::MenuItemCallback));
  46. btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4));
  47. btn_takephoto->setTag(DialogTakePhoto);
  48. CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20);
  49. pLabel1->setColor(ccc3(0, 0, 0));
  50. pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2));
  51. btn_takephoto->addChild(pLabel1);
  52. CCScale9Sprite* sp3 = CCScale9Sprite::create("dialog_normal.png");
  53. sp3->setContentSize(btnRect);
  54. CCScale9Sprite* sp4 = CCScale9Sprite::create("dialog_pressed.png");
  55. sp4->setContentSize(btnRect);
  56. CCMenuItemSprite* btn_photoalbum = CCMenuItemSprite::create(sp3
  57. ,sp4,this,menu_selector(DialogShare::MenuItemCallback));
  58. btn_photoalbum->setPosition(ccp(bgRect.width/2,bgRect.height/5*5/2));
  59. btn_photoalbum->setTag(DialogPhotoAlbum);
  60. CCLabelTTF* pLabel2 = CCLabelTTF::create("相册中选取", "Arial", 18);
  61. pLabel2->setColor(ccc3(0, 0, 0));
  62. pLabel2->setPosition(ccp(btnRect.width/2,btnRect.height/2));
  63. btn_photoalbum->addChild(pLabel2);
  64. CCScale9Sprite* sp5 = CCScale9Sprite::create("dialog_cancel_normal.png");
  65. sp5->setContentSize(btnRect);
  66. CCScale9Sprite* sp6 = CCScale9Sprite::create("dialog_pressed.png");
  67. sp6->setContentSize(btnRect);
  68. CCMenuItemSprite* btn_cancel = CCMenuItemSprite::create(sp5
  69. ,sp6,this,menu_selector(DialogShare::MenuItemCallback));
  70. btn_cancel->setPosition(ccp(bgRect.width/2,bgRect.height/5));
  71. btn_cancel->setTag(DialogCancel);
  72. CCLabelTTF* pLabel3 = CCLabelTTF::create("取消", "Arial", 16);
  73. pLabel3->setColor(ccc3(0, 0, 0));
  74. pLabel3->setPosition(ccp(btnRect.width/2,btnRect.height/2));
  75. btn_cancel->addChild(pLabel3);
  76. m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL);
  77. m_pMenu->setPosition(CCPointZero);
  78. background->addChild(m_pMenu);
  79. background->runAction(CCEaseExponentialOut::create(CCMoveTo::create(0.5f,ccp(bgRect.width/2,bgRect.height/2))));
  80. }
  81. void DialogShare::onEnter()
  82. {
  83. CCLayerColor::onEnter();
  84. CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,kCCMenuHandlerPriority-1, true);
  85. }
  86. void DialogShare::onExit()
  87. {
  88. CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
  89. CCLayerColor::onExit();
  90. }
  91. bool DialogShare::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
  92. {
  93. m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch, pEvent);
  94. returntrue;
  95. }
  96. void DialogShare::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
  97. {
  98. if (m_bTouchedMenu) {
  99. m_pMenu->ccTouchMoved(pTouch, pEvent);
  100. }
  101. }
  102. void DialogShare::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
  103. {
  104. if (m_bTouchedMenu) {
  105. m_pMenu->ccTouchEnded(pTouch, pEvent);
  106. m_bTouchedMenu = false;
  107. }
  108. }
  109. void DialogShare::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
  110. {
  111. if (m_bTouchedMenu) {
  112. m_pMenu->ccTouchEnded(pTouch, pEvent);
  113. m_bTouchedMenu = false;
  114. }
  115. }
  116. void DialogShare::MenuItemCallback(cocos2d::CCObject *pSender)
  117. {
  118. CCMenuItemImage* button=(CCMenuItemImage*)pSender;
  119. switch (button->getTag())
  120. {
  121. case DialogTakePhoto:
  122. CCLog("DialogTakePhoto++++++");
  123. break;
  124. case DialogPhotoAlbum:
  125. CCLog("DialogPhotoAlbum++++++");
  126. break;
  127. case DialogCancel:
  128. CCLog("DialogCancel++++++");
  129. this->removeFromParentAndCleanup(true);
  130. break;
  131. }
  132. }

【转】CCScale9Sprite和CCControlButton的更多相关文章

  1. 12.解决CCScale9Sprite或者CCControlButton无法使用的问题。

    问题: 使用CCScale9Sprite或者CCControlButton等控件的时候,会出现无法识别的情况. 解决方式: 1.include对应的头部,即#include "cocos-e ...

  2. Cocos2d-x中jsb结构剖析

    libs/javascript下有两部分bindings和spidermonkey.其中spidermonkey为js虚拟机,暂时不去管它.bindings下分为四部分,分别为主干部分,generat ...

  3. 1cocos2dx扩展UI控制,CCControlSlider,CCScale9Sprite(九妹图。),CCControlSwitch,CCControlButton

     UI控件来自cocos2dx的扩展库.完好了UI方面的元素,使cocos2dx更加丰富多彩.使用扩展库需包括: #include "cocos-ext.h" USING_NS ...

  4. cocos2d-x中CCScale9Sprite的另一种实现

    cocos2d 2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间部分进行拉伸,来满足一些控件的自适应(PS: 比如包括按钮,对话框,最直观 ...

  5. cocos2dx CCControlButton button大事

    =================================.cpp文件 <pre name="code" class="cpp">bool ...

  6. CCControlExtension/CCControlButton

    #ifndef __CCCONTROL_BUTTON_H__ #define __CCCONTROL_BUTTON_H__ #include "CCControl.h" #incl ...

  7. CCControlSwitch 、CCControlSlider、CCControlButton

    /* *bool hasMoved(); 这里获取的不是开关是否正在被用户拨动,而是开关最终的状态是由用户手动拨动开关进行的, *还是用户点击开关进行的状态更改 */ CCControlSwitch* ...

  8. cocos2dx基础篇(10) 按钮控件CCControlButton

    [3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)按钮事件回调依旧为 cccontrol_selector ,没有使用 CC_CALLBACK_2 (4)按钮状态  ...

  9. cocos2d-x CCScale9Sprite

    转自:http://www.cocos2dev.com/?p=295 前段时间看CCEditBox的时候,发现里面有个利用9宫格图缩放图片的,也就是缩放带圆角的图片. 这个比较有用处,很多游戏中有很多 ...

随机推荐

  1. Android Studio 项目中,哪些文件应该忽略而不提交到svn的服务器中?

    Android Studio 中建议过滤的文件: - .idea 文件夹 - .gradle 文件夹 - 所有的 build 文件夹 - 所有的 .iml 文件 - local.properties  ...

  2. 【WP8】让TextBox文本支持滑动(Scroll)

    通过修改样式让TextBox支持文本滑动 在Silverlight上,TextBox是有文本滚动的功能的,当TextBox的文本过长时,可以进行拖动的,TextBox使用 VerticalScroll ...

  3. 【WPF】附加属性

    一直都对附加属性理解很模糊,今天看了一篇文章,恍然大悟,用个Demo掩饰一下对附加属性的理解 附加属性,简单的理解就是给一个对象外在的定义一个属性,使得该对象拥有和使用该属性,最典型的是Grid.Ro ...

  4. Objective-C语法之NSPredicate的使用

    正则表达式判断手机号码和电话号码的方法: #import <Foundation/Foundation.h> /** 正则判断手机号码地址格式 */ BOOL isMobileNumber ...

  5. textarea标签内容为(英文或数字不自动换行)的解决方法

    textarea 显示一串英文时不会发生换行. 以下是两种解决方法:1.限制textarea的大小 width 设置为 00px (不要设置为00%)cols  设置为 30+ (也有类似效果) 2. ...

  6. 用好这6个APP,学英语SO EASY!

      http://www.jianshu.com/p/30a27af18340  

  7. vue的iview列表table render函数设置DOM属性值的方法

    { title: '负责人社保照片', key: 'leaderIdNumber', render: (h, params) => { return h('img',{domProps:{ sr ...

  8. 在mvc4中多语言建站的实例

    环境:vs2012 asp.net mvc4. 实现方式:resource 资源文件,根据路由规则中Lang参数来判断载入哪种语言方式 在网上找到了相关资料,顺便自己做了个练习,新建工程之类的步骤就免 ...

  9. 英文版Ubuntu18.10安装搜狗输入法过程(图文并茂,亲自尝试!)

    英文版Ubuntu18.10安装搜狗输入法过程 过程比较艰辛,折腾了好长的时间,不过最终还是装好了,特记录一下! 首先去搜狗输入法网址下载Linux版本:https://pinyin.sogou.co ...

  10. c#POST请求php接口

    POST请求php接口 /// <summary> /// 指定Post地址使用Get 方式获取全部字符串 /// </summary> /// <param name= ...