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開發的朋友可以關注一下哦!一起學習一起進步!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章