先导入先关文件
#include "cocos-ext.h”//要使用控件必须要导入的文件
using namespace cocos2d;
using namespace cocos2d::extension;//控件是Cocos2dx拓展功能所以要加上拓展标志
using namespace cocos2d::extension;//控件是Cocos2dx拓展功能所以要加上拓展标志
CCControlSwitch *pSwitch = CCControlSwitch::create(CCSprite::create("CloseNormal.png"),
CCSprite::create("CloseSelected.png"),
CCSprite::create("CloseNormal.png"),
CCSprite::create("CloseSelected.png"),
CCLabelTTF::create("开", "Arial", 40),
CCLabelTTF::create("关", "Arial", 40)
);
pSwitch->setScale(2);
pSwitch->setPosition(ccp(200,200));
//设置关闭状态
pSwitch->setOn(false);
//设置可操作
pSwitch->setEnabled(true);
//获取是否为打开(on)
CCLOG("是否打开状态:%i",pSwitch->isOn());
//获取当前开关状态是否为手动拖动开关进行的
CCLOG("是否手动拖动的开关:%i",pSwitch->hasMoved());
addChild(pSwitch);