UI开发之控件类CCControlSwitch

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
       
        CC_BREAK_IF(! CCLayer::init());

		//参数就不说了,看一下你的资源文件就明白了
		CCControlSwitch * controlSwitch = CCControlSwitch::create(
			CCSprite::create("extensions/switch-mask.png"),
			CCSprite::create("extensions/switch-on.png"),
			CCSprite::create("extensions/switch-off.png"),
			CCSprite::create("extensions/switch-thumb.png"),
			CCLabelTTF::create("On", "Arial-BoldMT", 16),
			CCLabelTTF::create("Off", "Arial-BoldMT", 16));

		//设置位置
		controlSwitch->setPosition(ccp(240,160));

		//这个函数对应初始时,开关的状态是开还是关。
		controlSwitch->setOn(true);
		//这个函数对应开关能否使用。
		controlSwitch->setEnabled(true);

		//添加事件监听
		controlSwitch->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::valueChanged),
			CCControlEventValueChanged);

		this->addChild(controlSwitch);
		
        bRet = true;
    } while (0);

    return bRet;
}

如果运行不出来这个界面,而是一片白的话,应该是版本的问题,我用的是2.1.4的时候就是一片白,然后升级到2.2的时候就好了。大家可以试一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值