cocos代码研究(15)Widget子类CheckBox学习笔记

理论基础

复选框是一种特定类型的“两状态”按钮,可以处于“选中”和“未选中状态”。继承自AbstractCheckButton。注 AbstractCheckButton继承自Widget类。

代码部分

Public属性类型

enum EventType
复选框事件类型,目前只有“选中”和“未选中”两种事件。

ccCheckBoxCallback
一个回调函数,它将在某个复选框事件触发后被调用。

Public成员函数

void setSelectedState (bool selected)
变更复选框选中

bool getSelectedState () const
查询复选框是否被选中。

bool isSelected () const
查询是否选中复选框。

void setSelected (bool selected)
修改复选框的状态 设置True会使复选框变为“选中状态”,否则传False会使复选框变为“未选中状态”

void addEventListenerCheckBox (Ref *target, SEL_SelectedStateEvent selector)
注册一个回调函数,它会在复选框的选择事件发生时被调用。

void addEventListener (const ccCheckBoxCallback &callback)
注册一个回调函数,它将在复选框被选中或者取消选中时被调用。

virtual void onTouchEnded (Touch *touch, Event *unusedEvent) override
触摸事件结束时的回调函数。

void loadTextures (const std::string &background, //背景普通状态图片的名字。
const std::string &backgroundSelected, //背景选择状态图片的名字。
const std::string &cross, //勾选选中状态图片的名字。
const std::string &backgroundDisabled, //背景禁用状态图片名字。
const std::string &frontCrossDisabled, //勾选禁用状态图片名字
TextureResType texType=TextureResType::LOCAL)
加载所有纹理并初始化一个复选框。

void loadTextureBackGround (const std::string &backGround, //背景普通状态图片的名字。
TextureResType type=TextureResType::LOCAL)
加载背景普通状态纹理

void loadTextureBackGroundSelected (const std::string &backGroundSelected, //背景选中状态纹理图片的名字。
TextureResType texType=TextureResType::LOCAL)
加载背景选中状态纹理的图片

void loadTextureFrontCross (const std::string &crossTextureName, //勾选选中状态图片的名字。
TextureResType texType=TextureResType::LOCAL)
加载勾选选中状态的纹理图片

void loadTextureBackGroundDisabled (const std::string &backGroundDisabled, //背景禁用状态纹理的图片名称
TextureResType texType=TextureResType::LOCAL)
加载背景禁用状态纹理的图片

void loadTextureFrontCrossDisabled (const std::string &frontCrossDisabled, //勾选禁用状态图片名字。
TextureResType texType=TextureResType::LOCAL)
加载勾选禁用状态的纹理图片

void setZoomScale (float scale)
当用户按下复选框时,按钮将会缩放到一个比例 最后复选框的缩放值等于(复选框原尺寸+ _zoomScale)

float getZoomScale () const
返回一个缩放比例

Sprite * getRendererBackground () const
返回背景普通状态的渲染节点

Sprite * getRendererBackgroundSelected () const
返回背景选择状态的渲染节点

Sprite * getRendererFrontCross () const
返回前景普通状态的渲染节点

实例:

        // Create the checkbox
        CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
                                              "cocosui/check_box_normal_press.png",
                                              "cocosui/check_box_active.png",
                                              "cocosui/check_box_normal_disable.png",
                                              "cocosui/check_box_active_disable.png");
        checkBox->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
        
        checkBox->addEventListener(CC_CALLBACK_2(UICheckBoxTest::selectedEvent, this));
        _uiLayer->addChild(checkBox);
        // Create the checkbox
        CheckBox* checkBox2 = CheckBox::create("cocosui/check_box_normal.png",
                                              "cocosui/check_box_active.png");
        checkBox2->setPosition(Vec2(widgetSize.width / 2.0f - 150, widgetSize.height / 2.0f));
        checkBox2->ignoreContentAdaptWithSize(false);
        checkBox2->setZoomScale(0.5);
        checkBox2->setContentSize(Size(80,80));
        checkBox2->setName("bigCheckBox");
        _uiLayer->addChild(checkBox2);
原文地址:https://www.cnblogs.com/damowang/p/4861158.html