【转】CCScale9Sprite和CCControlButton

简介: 转自:http://blog.csdn.net/nat_myron/article/details/12975145 在2dx下用到了android下的.9.png图片,下面是原图   查了一下2dx里有CCScale9Sprite,直接贴上背景图,毫无问题,   [cpp] ...

转自: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.  
  3. #include "cocos2d.h" 
  4. #include "HelloWorldScene.h" 
  5. #include "cocos-ext.h" 
  6. USING_NS_CC_EXT; 
  7. USING_NS_CC; 
  8.  
  9. class DialogShare: public CCLayerColor 
  10.     // 模态对话框菜单 
  11.     CCMenu *m_pMenu; 
  12.     // 记录菜单点击 
  13.     bool m_bTouchedMenu; 
  14. public
  15.     DialogShare(); 
  16.     ~DialogShare(); 
  17.     static cocos2d::CCScene* scene(); 
  18.     virtualbool init(); 
  19.     // 初始化对话框内容 
  20.     void initDialog(); 
  21.      
  22.     CREATE_FUNC(DialogShare); 
  23.      
  24.     void onEnter(); 
  25.     void onExit(); 
  26.     virtualbool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); 
  27.     virtualvoid ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); 
  28.     virtualvoid ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); 
  29.     virtualvoid ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); 
  30.      
  31.     void MenuItemCallback(CCObject *pSender); 
  32. }; 
#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.     CCScene *scene = CCScene::create(); 
  10.     DialogShare *layer = DialogShare::create(); 
  11.     scene->addChild(layer); 
  12.     return scene; 
  13. DialogShare::DialogShare() 
  14.  
  15. DialogShare::~DialogShare() 
  16.  
  17. bool DialogShare::init() 
  18.     bool bRet = false;     
  19.     do
  20.         CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 125)));        
  21.         this->initDialog();     
  22.         bRet = true
  23.     } while (0);    
  24.     return bRet; 
  25.  
  26. void DialogShare::initDialog() 
  27.     CCSize size = CCDirector::sharedDirector()->getWinSize(); 
  28.  
  29.     CCSize bgRect = CCSizeMake(size.width,size.height/3); 
  30.     CCScale9Sprite *background   = CCScale9Sprite::create("dialog_bg.png"); 
  31.     background->setContentSize(bgRect); 
  32.     background->setPosition(ccp(bgRect.width/2,-bgRect.height/2)); 
  33.     this->addChild(background,5); 
  34.  
  35.     CCSize btnRect = CCSizeMake(bgRect.width/2,bgRect.height/5); 
  36.  
  37.     CCScale9Sprite* sp1 = CCScale9Sprite::create("dialog_normal.png"); 
  38.     sp1->setContentSize(btnRect); 
  39.     CCScale9Sprite* sp2 = CCScale9Sprite::create("dialog_pressed.png"); 
  40.     sp2->setContentSize(btnRect); 
  41.     CCMenuItemSprite* btn_takephoto = CCMenuItemSprite::create(sp1 
  42.         ,sp2,this,menu_selector(DialogShare::MenuItemCallback)); 
  43.     btn_takephoto->setPosition(ccp(bgRect.width/2,bgRect.height/5*4)); 
  44.     btn_takephoto->setTag(DialogTakePhoto); 
  45.     CCLabelTTF* pLabel1 = CCLabelTTF::create("拍照", "Arial", 20); 
  46.     pLabel1->setColor(ccc3(0, 0, 0)); 
  47.     pLabel1->setPosition(ccp(btnRect.width/2,btnRect.height/2)); 
  48.     btn_takephoto->addChild(pLabel1); 
  49.  
  50.     CCScale9Sprite* sp3 = CCScale9Sprite::create("dialog_normal.png"); 
  51.     sp3->setContentSize(btnRect); 
  52.     CCScale9Sprite* sp4 = CCScale9Sprite::create("dialog_pressed.png"); 
  53.     sp4->setContentSize(btnRect); 
  54.     CCMenuItemSprite* btn_photoalbum = CCMenuItemSprite::create(sp3 
  55.         ,sp4,this,menu_selector(DialogShare::MenuItemCallback)); 
  56.     btn_photoalbum->setPosition(ccp(bgRect.width/2,bgRect.height/5*5/2)); 
  57.     btn_photoalbum->setTag(DialogPhotoAlbum); 
  58.     CCLabelTTF* pLabel2 = CCLabelTTF::create("相册中选取", "Arial", 18); 
  59.     pLabel2->setColor(ccc3(0, 0, 0)); 
  60.     pLabel2->setPosition(ccp(btnRect.width/2,btnRect.height/2)); 
  61.     btn_photoalbum->addChild(pLabel2); 
  62.  
  63.     CCScale9Sprite* sp5 = CCScale9Sprite::create("dialog_cancel_normal.png"); 
  64.     sp5->setContentSize(btnRect); 
  65.     CCScale9Sprite* sp6 = CCScale9Sprite::create("dialog_pressed.png"); 
  66.     sp6->setContentSize(btnRect); 
  67.     CCMenuItemSprite* btn_cancel = CCMenuItemSprite::create(sp5 
  68.         ,sp6,this,menu_selector(DialogShare::MenuItemCallback)); 
  69.     btn_cancel->setPosition(ccp(bgRect.width/2,bgRect.height/5)); 
  70.     btn_cancel->setTag(DialogCancel); 
  71.     CCLabelTTF* pLabel3 = CCLabelTTF::create("取消", "Arial", 16); 
  72.     pLabel3->setColor(ccc3(0, 0, 0)); 
  73.     pLabel3->setPosition(ccp(btnRect.width/2,btnRect.height/2)); 
  74.     btn_cancel->addChild(pLabel3); 
  75.  
  76.     m_pMenu = CCMenu::create(btn_takephoto,btn_photoalbum,btn_cancel, NULL); 
  77.     m_pMenu->setPosition(CCPointZero); 
  78.     background->addChild(m_pMenu); 
  79.  
  80.     background->runAction(CCEaseExponentialOut::create(CCMoveTo::create(0.5f,ccp(bgRect.width/2,bgRect.height/2)))); 
  81.  
  82. void DialogShare::onEnter() 
  83.     CCLayerColor::onEnter(); 
  84.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,kCCMenuHandlerPriority-1, true); 
  85.  
  86. void DialogShare::onExit() 
  87.     CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); 
  88.     CCLayerColor::onExit(); 
  89.  
  90. bool DialogShare::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) 
  91.     m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch, pEvent);    
  92.     returntrue
  93.  
  94. void DialogShare::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) 
  95.     if (m_bTouchedMenu) { 
  96.         m_pMenu->ccTouchMoved(pTouch, pEvent); 
  97.     } 
  98.  
  99. void DialogShare::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) 
  100.     if (m_bTouchedMenu) { 
  101.         m_pMenu->ccTouchEnded(pTouch, pEvent); 
  102.         m_bTouchedMenu = false
  103.     } 
  104.  
  105. void DialogShare::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) 
  106.     if (m_bTouchedMenu) { 
  107.         m_pMenu->ccTouchEnded(pTouch, pEvent); 
  108.         m_bTouchedMenu = false
  109.     } 
  110.  
  111. void DialogShare::MenuItemCallback(cocos2d::CCObject *pSender) 
  112.     CCMenuItemImage* button=(CCMenuItemImage*)pSender; 
  113.     switch (button->getTag()) 
  114.     { 
  115.     case DialogTakePhoto: 
  116.         CCLog("DialogTakePhoto++++++"); 
  117.         break
  118.     case DialogPhotoAlbum: 
  119.         CCLog("DialogPhotoAlbum++++++"); 
  120.         break
  121.     case DialogCancel: 
  122.         CCLog("DialogCancel++++++"); 
  123.         this->removeFromParentAndCleanup(true); 
  124.         break
  125.     } 
相关文章
|
2月前
|
前端开发
ThreeJs通过canvas和Sprite添加标签
这篇文章介绍了在Three.js中利用Canvas和Sprite实现动态文本标签的方法,使得标签可以跟随模型并在3D空间中始终保持面向摄像机。
81 0
ThreeJs通过canvas和Sprite添加标签
|
5月前
|
编解码 缓存 前端开发
什么是CSS Sprite
【7月更文挑战第14天】 **CSS Sprite** 是一种图像合并技术,通过将多个图标整合到一张大图并利用CSS背景定位显示所需部分,减少HTTP请求,提升页面加载速度和降低服务器压力。优点包括减少请求次数、降低服务器负担、加快速度和简化图片管理,但制作与维护成本高且定位复杂。使用工具可降低工作难度,适应不同分辨率设备。需权衡利弊适时应用。
51 1
|
6月前
|
Python
Pygame基础2-精灵类 Sprite
Pygame基础2-精灵类 Sprite
|
6月前
|
Python
Pygame基础5-Surface & Rect
Pygame基础5-Surface & Rect
|
7月前
|
XML 前端开发 JavaScript
canvas和svg的比较
canvas和svg的比较
|
7月前
|
XML 移动开发 前端开发
Canvas和SVG:你应该选择哪一个?
Canvas和SVG:你应该选择哪一个?
109 2
|
7月前
|
XML 移动开发 前端开发
Canvas和SVG有什么区别?
Canvas和SVG有什么区别?
71 1
|
7月前
dragonBones使用Sprite换肤后mesh问题
dragonBones使用Sprite换肤后mesh问题
65 0
|
7月前
|
XML 移动开发 前端开发
canvas与svg区别与实际应用
canvas与svg区别与实际应用
59 0
|
前端开发
雪碧图 Sprite图
雪碧图 Sprite图
119 0