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",20); onLabel->setcolor(color3B(0,0)); offLabel->setcolor(color3B(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"); }}总结
以上是内存溢出为你收集整理的cocos2d-x3.2 使用开关控制按钮 ControlSwitch全部内容,希望文章能够帮你解决cocos2d-x3.2 使用开关控制按钮 ControlSwitch所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)