cocos2dx开发 之 使用复选框CheckBox做性别选择

1.关于CheckBox

auto checkbox = CheckBox::create("check_box_normal.png",
                                 "check_box_normal_press.png",
                                 "check_box_active.png",
                                 "check_box_normal_disable.png",
                                 "check_box_active_disable.png");

复选框使用起来跟MenuItemTogger很相似,其实就是几张图片之间的点击事件封装起来了。

2.代码展示

头文件

#pragma once
#include "cocos2d.h"
using namespace cocos2d;

#include "ui/CocosGUI.h"
using namespace ui;


class CheckBoxDemo :public Scene
{
public:
	static Scene* createscene();
	virtual bool init();
	CREATE_FUNC(CheckBoxDemo);
	void comeback(Ref*);

private:
	CheckBox* male;     //男生复选框
	CheckBox* female;   //女生复选框
	Label * str_male;   //男生字体
	Label* str_female;  //女生字体
	Button* go_next;    //下一个场景的按钮
	Sprite* _male;      //男生的图片
	Sprite* _female;    //女生的图片
};

cpp文件

#include "CheckBoxDemo.h"

Scene* CheckBoxDemo::createscene()
{
	return CheckBoxDemo::create() ;
}

bool CheckBoxDemo::init()
{
	if (!Scene::init())
	{
		return false;
	};


    //前往下一个场景的按钮
	go_next = Button::create("go_next.png", "go_next2.png", "go_next3.png");
	this->addChild(go_next);

    //男生复选框
	male = CheckBox::create("box_3.png", "box_2.png", "box_1.png", "box_1.png", "box_1.png");
	this->addChild(male);

    //男生复选框的设置
	male->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
	male->setPosition(Vec2(Director::getInstance()->getVisibleSize()) / 2);

	//字节设置
	String* Male = String::create("male");
	str_male = Label::createWithSystemFont(Male->getCString(), "fonts/arial.ttf", 30);
	str_male->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
	str_male->setPosition(Vec2(male->getPosition()) + Vec2(100, 0));
	this->addChild(str_male);

	//男生复选框的按钮事件
	male->addEventListener([&](Ref* ref, ui::CheckBox::EventType type)
		{
			if (type == CheckBox::EventType::SELECTED)
			{
				if (female->isSelected()) //如果女生按钮选中了,那么就设置为未选中
				{
					female->setSelected(false);
				}
				go_next->setEnabled(true); //按钮权限开启
			}
			else if (type == CheckBox::EventType::UNSELECTED)//如果状态为没有选中
			{
				go_next->setEnabled(false);//按钮权限关掉
			}
		}
	);

	//女生复选框
	female = CheckBox::create("box_3.png", "box_2.png", "box_1.png", "box_1.png", "box_1.png");
	this->addChild(female);

	//女生的坐标
	female->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
	female->setPosition(Vec2(male->getPositionX() + 300, male->getPositionY()));

	//女生复选框按钮事件
	female->addEventListener([&](Ref* ref, ui::CheckBox::EventType type)
		{
			if (type == CheckBox::EventType::SELECTED)
			{
				if (male->isSelected())
				{
					male->setSelected(false);
				}
				go_next->setEnabled(true);
			}
			else if (type == CheckBox::EventType::UNSELECTED)
			{
				go_next->setEnabled(false);
			}
		}
	);

	//字节设置
	String* Female = String::create("female");
	str_female = Label::createWithSystemFont(Female->getCString(), "fonts/arial.ttf", 30);
	str_female->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
	str_female->setPosition(Vec2(female->getPosition()) + Vec2(100, 0));
	this->addChild(str_female);

    //去往下个场景的设置
	go_next->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
	go_next->setPosition(Vec2(female->getPosition()) + Vec2(400, 0));
	go_next->setEnabled(false);
	go_next->addTouchEventListener([&](Ref* ref,ui::Button::Widget::TouchEventType type)
		{
			if (type == ui::Button::Widget::TouchEventType::ENDED)
			{
				if (male->isSelected() == 1 || female->isSelected() == 1)
				{
					Director::getInstance()->end();
				}

			}
		});
    
    //显示男生图片
	_male = Sprite::create("male2.jpeg");
	this->addChild(_male);
	_male->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
	_male->setPosition(male->getPosition()+Vec2(0,300));
	_male->setScale(1.2);
    
    //显示女生图片
	_female = Sprite::create("female2.jpeg");
	this->addChild(_female);
	_female->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
	_female->setPosition(female->getPosition() + Vec2(0, 300));
	_female->setScale(0.3);

	return true;
}

Button按钮的介绍:https://blog.csdn.net/qq_42428486/article/details/89969830

喜欢cocos2dx开发的朋友可以关注一下哦!一起学习一起进步!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值