Cocos2d-x UI開辟之CCControlColourPicker控件類應用實例。本站提示廣大學習愛好者:(Cocos2d-x UI開辟之CCControlColourPicker控件類應用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Cocos2d-x UI開辟之CCControlColourPicker控件類應用實例正文
CCControlColourPicker完成色彩拾取器的功效。關於控件應用時的一些設置裝備擺設,請拜見文章:UI開辟之控件類-CCControlButton。下邊來看源代碼。
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
//設置一個顯示字符串的label
CCLabelTTF * title = CCLabelTTF::create("#128128128","Arial",32);
title->setPosition(ccp(240,280));
//設置label的tag為1,便利今後獲得
this->addChild(title,0,1);
//這裡有一個成績須要留意,在create之前,應當在resource目次下新建一個文件夾叫做extensions,然後把源代碼中
//和CCControlColourPicker相干的資本導入出來
CCControlColourPicker * colorPicker = CCControlColourPicker::create();
colorPicker->setColor(ccc3(128,128,128));
//設置一張配景圖片,然則卻不起感化,至今沒處理,有誰處理了,說一聲
//colorPicker->setBackground(CCSprite::create("HelloWorld.png"));
//為colorPicker添加事宜監聽函數
colorPicker->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::colorValueChanged),
CCControlEventValueChanged);
//設置地位
colorPicker->setPosition(ccp(240,160));
this->addChild(colorPicker);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::colorValueChanged(CCObject * pSender,CCControlEvent controlEvent)
{
CCLabelTTF * title = (CCLabelTTF *)this->getChildByTag(1);
CCControlColourPicker * pPicker = (CCControlColourPicker *)pSender;
//這裡須要留意了,自己用的cocos2d-x的版本是2.2,應當用pPicker挪用getColor函數,但據自己檢查別人的
//博客,他們都是用的getColorValue函數,他們應當是早一點的版本
title->setString(CCString::createWithFormat("#%03d%03d%03d",pPicker->getColor().r,pPicker->getColor().g,
pPicker->getColor().b)->getCString());
}