源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Cocos2d-x UI开发之CCControlColourPicker控件类使用实例

  • 时间:2021-09-30 18:27 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Cocos2d-x UI开发之CCControlColourPicker控件类使用实例
CCControlColourPicker实现颜色拾取器的功能。关于控件使用时的一些配置,请参见文章:[url=http://www.1sucai.cn/article/55023.htm]UI开发之控件类-CCControlButton[/url]。下边来看源代码。 [img]http://files.jb51.net/file_images/article/201409/2014911103216688.gif?2014811103229[/img]
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("#ddd",pPicker->getColor().r,pPicker->getColor().g,
  pPicker->getColor().b)->getCString());
 }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部