SwitchControl控件起到了一个开关的作用类似于现实生活中的开关
由于控件比较简单,我就不做过多的解释,直接上代码
首先在工程目录下的Resource文件夹中添加三张图片
在SwitchControl.h添加下面代码
#ifndef _SwitchControl_H_#define _SwitchControl_H_#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class SwitchControl : public cclayer {public: static CCScene* scene(); CREATE_FUNC(SwitchControl); bool init(); voID switchValueChanged(CCObject*,CCControlEvent);};#endif
在SwitchControl.cpp中添加下面代码
#include "SwitchControl.h"CCScene* SwitchControl::scene(){ CCScene* s = CCScene::create(); SwitchControl* layer = SwitchControl::create(); s->addChild(layer); return s;}bool SwitchControl::init(){ cclayer::init(); //得到窗口的大小 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //设置ControlSwitch控件打开的文字No" cclabelTTF* on = cclabelTTF::create("ON","Arial",16); //设置ControlSwitch控件关闭时的文字"OFF" cclabelTTF* off = cclabelTTF::create("OFF",16); //设置ControlSwitch控件打开的文字的颜色 on->setcolor(ccc3(0,0)); //设置ControlSwitch控件关闭时的颜色 off->setcolor(ccc3(0,0)); //创建ControlSwitch控件 CCControlSwitch* control = CCControlSwitch::create( CCSprite::create("switch-mask.png"),CCSprite::create("switch-on.png"),CCSprite::create("switch-off.png"),CCSprite::create("switch-thumb.png"),on,off); //添加ControlSwitch控件 addChild(control); //设置ControlSwitch控件的位置 control->setposition(ccp(winSize.wIDth / 2,winSize.height / 2)); // 注册valuechange消息,当valuechange时,调用switchValueChanged函数 control->addTargetWithActionForControlEvents(this,cccontrol_selector(SwitchControl::switchValueChanged),CCControlEventValueChanged); return true;}voID SwitchControl::switchValueChanged(CCObject* sender,CCControlEvent ev){ if (ev == CCControlEventValueChanged) { CCControlSwitch* control = (CCControlSwitch*)sender; if (control->isOn()) { cclog("Switch if ON"); } else { cclog("Swith is Off"); } } else { cclog("other events"); }}
执行结果:
演示效果:
总结以上是内存溢出为你收集整理的Cocos2d-X中SwitchControl的用法全部内容,希望文章能够帮你解决Cocos2d-X中SwitchControl的用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)