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