zoukankan      html  css  js  c++  java
  • cocos2d-x3.2 使用开关控制按钮 ControlSwitch


    ContolSwitch 控件起到了一个开关的作用类似于现实生活中的开关,直接上代码:

    .h文件

    //
    //  SwitchBtnScene.h
    //  LSWGameIOS
    //
    //  Created by lsw on 14-10-17.
    //
    //
    
    #ifndef LSWGameIOS_SwitchBtnScene_h
    #define LSWGameIOS_SwitchBtnScene_h
    
    #include "cocos2d.h"
    #include "cocos-ext.h"
    
    class SwitchBtnScene : public cocos2d::Layer
    {
    public:
        static cocos2d::Scene *createScene();
        bool init();
        CREATE_FUNC(SwitchBtnScene);
        
        void valueChanged(cocos2d::Ref *sender, cocos2d::extension::Control::EventType evt);
    };
    
    #endif
    

    .cpp文件

    //
    //  SwitchBtnScene.cpp
    //  LSWGameIOS
    //
    //  Created by lsw on 14-10-17.
    //
    //
    
    #include "SwitchBtnScene.h"
    #include "GUI/CCControlExtension/CCControlSwitch.h"
    
    USING_NS_CC;
    USING_NS_CC_EXT;
    
    Scene *SwitchBtnScene::createScene()
    {
        auto scene = Scene::create();
        auto layer = SwitchBtnScene::create();
        scene->addChild(layer);
        return scene;
    }
    
    bool SwitchBtnScene::init()
    {
        if (!Layer::init())
        {
            return false;
        }
        
        auto winSize = Director::getInstance()->getWinSize();
        auto onLabel = Label::createWithSystemFont("on", "Arail", 20);
        auto offLabel = Label::createWithSystemFont("off", "Arail", 20);
        onLabel->setColor(Color3B(0, 0, 0));
        offLabel->setColor(Color3B(0, 0, 0));
        
        auto maskSprite = Sprite::create("switchButton/switchGreen.png");
        auto onSprite = Sprite::create("switchButton/switchGreen.png");
        auto offSprite = Sprite::create("switchButton/switchRed.png");
        auto thumbSprite = Sprite::create("switchButton/switchBtn.png");
        //设置按钮的截取范围 开关图片和显示文字以及按钮
        ControlSwitch *switchBtn = ControlSwitch::create(maskSprite, onSprite, offSprite, thumbSprite, onLabel, offLabel);
        addChild(switchBtn);
        switchBtn->setPosition(Vec2(winSize.width/2, winSize.height/2));
        //设置监听事件
        switchBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(SwitchBtnScene::valueChanged), Control::EventType::VALUE_CHANGED);
        
        return true;
    }
    
    void SwitchBtnScene::valueChanged(Ref *sender, Control::EventType evt)
    {
        if (evt == Control::EventType::VALUE_CHANGED)
        {
            ControlSwitch *btn = (ControlSwitch *)sender;
            if (btn->isOn())
            {
                CCLOG("btn is on");
            }
            else
            {
                CCLOG("btn is off");
            }
        }
        else
        {
            CCLOG("is other state");
        }
    }



  • 相关阅读:
    使用Loadrunner监控Windows资源
    Tomcat使用线程池配置高并发连接
    性能测试中遇到的坑
    本地eclipse启动tomcat后无法访问
    Linux常用命令汇总
    Dubbo底层采用Socket进行通信详解
    今天遇到了一个Spring出现的一个未知错误,分享下
    maven pom.xml 详细
    Oracle 数据库中在使用中文模糊查询时输入中文查询不到结果的解决方法
    mybatis属性详解
  • 原文地址:https://www.cnblogs.com/shiweihappy/p/4246417.html
Copyright © 2011-2022 走看看