Cocos2d-X在SwitchControl使用

Cocos2d-X在SwitchControl使用

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

SwitchControl控制类中的一个开关的发挥了作用似在现实生活中开关

因为控制相对简单,我没有做过多的解释。直接在代码


首先在project文件夹下的Resource文件夹中加入三张图片

Cocos2d-X在SwitchControl使用

Cocos2d-X在SwitchControl使用

Cocos2d-X在SwitchControl使用


在SwitchControl.h加入以下代码

#ifndef   _SwitchControl_H_
#define  _SwitchControl_H_

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class SwitchControl : public CCLayer 
{
public:
    static CCScene* scene();
	CREATE_FUNC(SwitchControl);
	bool init();
	void switchValueChanged(CCObject*, CCControlEvent);
};

#endif

在SwitchControl.cpp中加入以下代码

#include "SwitchControl.h"

CCScene* SwitchControl::scene()
{
	CCScene* s = CCScene::create();
	SwitchControl* layer = SwitchControl::create();
	s->addChild(layer);
	return s;
}

bool SwitchControl::init()
{ 
    CCLayer::init();

    //得到窗体的大小
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();   

    //设置ControlSwitch控件打开的文字No"
	CCLabelTTF* on = CCLabelTTF::create("ON", "Arial", 16);
	
    //设置ControlSwitch控件关闭时的文字"OFF"
    CCLabelTTF* off = CCLabelTTF::create("OFF", "Arial", 16);
	
    //设置ControlSwitch控件打开的文字的颜色
    on->setColor(ccc3(0, 0, 0));

    //设置ControlSwitch控件关闭时的颜色
	off->setColor(ccc3(0, 0, 0));

    //创建ControlSwitch控件
    CCControlSwitch* control = CCControlSwitch::create(
	    CCSprite::create("switch-mask.png"),
	    CCSprite::create("switch-on.png"),
	    CCSprite::create("switch-off.png"),
	    CCSprite::create("switch-thumb.png"),
	    on,
	    off);

        //加入ControlSwitch控件
        addChild(control);
      
        //设置ControlSwitch控件的位置
        control->setPosition(ccp(winSize.width / 2, winSize.height / 2));

		// 注冊valuechange消息,当valuechange时。调用switchValueChanged函数
		control->addTargetWithActionForControlEvents(this, 
			cccontrol_selector(SwitchControl::switchValueChanged), 
			CCControlEventValueChanged);
		
		return true;
}

void SwitchControl::switchValueChanged(CCObject* sender, CCControlEvent ev)
{
	if (ev == CCControlEventValueChanged)
	{
		CCControlSwitch* control = (CCControlSwitch*)sender;
		if (control->isOn())
		{
			CCLog("Switch if ON");
		}
		else
		{
			CCLog("Swith is Off");
		}
	}
	else
	{
		CCLog("other events");
	}
}

运行结果:

Cocos2d-X在SwitchControl使用

演示效果:

Cocos2d-X在SwitchControl使用



版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117696.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 11111111

    1111111111111111

    2022年4月20日
    37
  • 状况与状态区别_402状态码

    状况与状态区别_402状态码http状态码301和302详解及区别一直对http状态码301和302的理解比较模糊,在遇到实际的问题和翻阅各种资料了解后,算是有了一定的理解。这里记录下,希望能有新的认识。大家也共勉。官方的比较简洁的说明:301redirect:301代表永久性转移(PermanentlyMoved)302redirect:302代表…

    2022年10月3日
    0
  • document.documentElement.clientWidth

    document.documentElement.clientWidth关于获取各种浏览器可见窗口大小的一点点研究functiongetInfo(){vars=””;s=”网页可见区域宽:”document.body.clientWidth;s=”网页可见区域高:”document.body.clientHeight;s=”网页可见区域宽:”document.body.offsetWidth”(包括边线和滚动

    2022年7月22日
    11
  • 数组类型的字符串转List<Map>

    数组类型的字符串转List<Map>需求:  格式为数组形式的字符串,数组中包含多个Map,需要将字符串转换为List&lt;Map&gt;形式。字符串示例:   [{"type":"text","content":"123"},{"type":"text","content":"456"}方法:  需要引入阿里巴巴的fastjson

    2022年5月14日
    94
  • Android错误之ListView加载错位_ListView图片错位

    又遇到ListView加载item时,多个item中的图片会错位的情况现象如下图,同一个人的头像显示的乱七八糟找了一张图,很好地说明了问题的原因问题原因就在于convertView的重用,当重用 convertView 时,最初一屏显示 7 条记录, getView 被调用 7 次,创建了 7 个 convertView,当 Item1 划出屏幕, Item8 进入屏幕时,这时没有为 Item8

    2022年3月11日
    40
  • 22、Windows10下局域网的两台电脑间传输文件

    22、Windows10下局域网的两台电脑间传输文件一、说明局域网内两台电脑传输文件使用windows自带的文件共享机制即可,不需要找专门的文件传输软件,下面来介绍使用方法。二、步骤1、控制面板2、网络和Internet3、网络和共享中心4、记住网络类型并点击更改高级共享设置5、根据对应的网络类型,在其下选择启用网络发现6、设置所有网络如下7、随便找一个文件夹设置为共享,这里在桌面新建一个名为“共享”的文件夹作文测试8、右击->属性->共享9、在其它电脑上访问自己的的文件夹,假设共享文件夹所在主机IP

    2022年5月4日
    339

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号