CCScale9Sprite和CCControlButton

在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);
然後按鈕裏也需要用到這個素材圖,拉伸圖片到我們需要的,用到了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);
當然也可以用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);
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();
    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);   
	return true;
}

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;
	}
}







發佈了37 篇原創文章 · 獲贊 19 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章