CCControlSwitch *pSwitch= CCControlSwitch::create(
CCSprite::create("switch-mask.png"), //switch开关的外框
CCSprite::create("switch-on.png"), //switch开关处于开
CCSprite::create("switch-off.png"), //switch开关处于关
CCSprite::create("switch-thumb.png"), //标志的小圆点
CCLabelTTF::create("开", "Arial-BoldMT", 16),
CCLabelTTF::create("关", "Arial-BoldMT", 16)
);
pSwitch->addTargetWithActionForControlEvent(this, cccontrol_selector(HelloWorld::swithEvent), CCControlEventValueChanged);
pSwitch->setPosition(ccp(200,200));
//设置关闭状态
pSwitch->setOn(false);
//设置可操作
pSwitch->setEnabled(true);
//获取是否为打开(on)
CCLOG("是否打开状态:%i",pSwitch->isOn());
//获取当前开关状态是否为手动拖动开关进行的
CCLOG("是否手动拖动的开关:%i",pSwitch->hasMoved());
addChild(pSwitch);
void HelloWorld::swithEvent(CCObject *pSender){
CCControlSwitch *controlSwith = (CCControlSwitch*)pSender;
if (controlSwith->isOn()) {
CCLOG("on");
}else{
CCLOG("close");
}
}