cocos2d-x开关按钮类CCControlSwitch

在test项目中的ControlExtensionText\ CCControlSwitchTest目录下面的CCControlSwitchTest.cpp中,通过这个例子,我们也可以制作出不错的开关效果,以下是我尝试的代码:

首先声明文件:

#ifndef __loading__LoadingScene__
#define __loading__LoadingScene__

#include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
class LoadingScene :public CCControlSwitch   //注意继承的类
{
public:
    bool init();
    CREATE_FUNC(LoadingScene);
    static cocos2d::CCScene *scene();
    void valueChanged(CCObject* sender, CCControlEvent controlEvent);
    cocos2d::CCLabelTTF *ttf;    
};

#endif /* defined(__loading__LoadingScene__) */

类的实现文件:

#include "LoadingScene.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
bool LoadingScene::init()
{
    if (!CCControlSwitch::init())     //原示例中为CCControlScene::init(),但我写后一直不识别,不知道什么原因,估计是我继承的类不同吧
    {
        return false;
    }
    CCSprite *bg=CCSprite::create("fullbg.png");
    this->addChild(bg);
    CCSize size=CCDirector::sharedDirector()->getWinSize();
    CCControlSwitch *switch1=CCControlSwitch::create(
        CCSprite::create("switch-mask.png"),             //背景图片
        CCSprite::create("switch-on.png"),               //开状态背景图片
        CCSprite::create("switch-off.png"),              //关状态背景图片
        CCSprite::create("switch-thumb.png"),            //开关背景图片
        CCLabelTTF::create("On", "Arial-BoldMT", 16),    //开文字标签
        CCLabelTTF::create("Off", "Arial-BoldMT", 16)    //关文字标签
    );
    this->addChild(switch1,1);
    this->setPosition(ccp(size.width/2, size.height/2));                       //添加动作事件
    switch1->addTargetWithActionForControlEvents(this, cccontrol_selector(LoadingScene::valueChanged), CCControlEventValueChanged);
    
    CCScale9Sprite *sp=CCScale9Sprite::create("buttonBackground.png");     //添加九妹图片
    sp->setContentSize(CCSizeMake(200, 300));                               //设置九妹图片大小
    this->addChild(sp,1);
    sp->setAnchorPoint(ccp(1,1.3));
    sp->setPosition(ccp(size.width/3, size.height/3));
    ttf=CCLabelTTF::create("On", "Arial-BoldMT", 22);
    ttf->setPosition(ccp(sp->getContentSize().width/2, sp->getContentSize().height/2));
    sp->addChild(ttf,1);                                                       //在九妹图片上添加文字标签
    return true;
}
CCScene *LoadingScene::scene()
{
    CCScene *scene=CCScene::create();
    LoadingScene *layer=LoadingScene::create();
    scene->addChild(layer);
    return scene;
}
void LoadingScene::valueChanged(CCObject* sender, CCControlEvent controlEvent)
{
    CCControlSwitch* pSwitch = (CCControlSwitch*)sender;
    if (pSwitch->isOn())                  //判断标签是否是on状态
    {
        ttf->setString("On");
    }
    else
    {
        ttf->setString("Off");
    }

}
最后,实现的效果如下:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值