服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - Cocos2d-x UI开发之CCControlSwitch控件类使用实例

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

2021-02-01 14:24皂荚花 C/C++

这篇文章主要介绍了Cocos2d-x UI开发之CCControlSwitch控件类使用实例,本文代码中含大量注释讲解了CCControlSwitch控件类的使用,需要的朋友可以参考下

CCControlSwitch是开关按钮,关于控件使用时的一些配置,请参见文章:UI开发之控件类-CCControlButton。以下的演示中出现的key和value代表什么意思,知道的人说一声。

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

 

  1. bool HelloWorld::init() 
  2.   bool bRet = false
  3.   do 
  4.   { 
  5.  
  6.     CC_BREAK_IF(! CCLayer::init()); 
  7.  
  8.         //参数就不说了,看一下你的资源文件就明白了 
  9.         CCControlSwitch * controlSwitch = CCControlSwitch::create( 
  10.             CCSprite::create("extensions/switch-mask.png"), 
  11.             CCSprite::create("extensions/switch-on.png"), 
  12.             CCSprite::create("extensions/switch-off.png"), 
  13.             CCSprite::create("extensions/switch-thumb.png"), 
  14.             CCLabelTTF::create("On""Arial-BoldMT", 16), 
  15.             CCLabelTTF::create("Off""Arial-BoldMT", 16)); 
  16.  
  17.         //设置位置 
  18.         controlSwitch->setPosition(ccp(240,160)); 
  19.  
  20.         //这个函数对应初始时,开关的状态是开还是关。 
  21.         controlSwitch->setOn(true); 
  22.         //这个函数对应开关能否使用。 
  23.         controlSwitch->setEnabled(true); 
  24.  
  25.         //添加事件监听 
  26.         controlSwitch->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::valueChanged), 
  27.             CCControlEventValueChanged); 
  28.  
  29.         this->addChild(controlSwitch); 
  30.  
  31.     bRet = true
  32.   } while (0); 
  33.  
  34.   return bRet; 
  35.  
  36. void HelloWorld::valueChanged(CCObject * pSender,CCControlEvent controlEvent) 
  37.     CCControlSwitch * controlSwitch = (CCControlSwitch *)pSender; 
  38.     CCLog("click"); 

延伸 · 阅读

精彩推荐