Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating checkboxes


In this recipe, we will create a checkbox. In Cocos2d-x version 2, a checkbox was created by using the MenuItemToggle class. However, doing so was quite cumbersome. In Cocos2d-x version 3, we can create a checkbox by using the Checkbox class that can be used in Cocos Studio.

Getting ready

So let's prepare the images of the checkbox before you start. Here, we have prepared the images of the required minimum On and Off status. Please add these images to the Resouces/res folder.

The Off status image will look something like this:

The On status image will look something like this:

How to do it...

Let's create a checkbox by using the Checkbox class. First, you will generate a checkbox instance by using the check_box_normal.png image and the check_box_active.png image. You will also specify the callback function as a lambda expression by using the addEventListener method when the checkbox status is changed. Create the checkbox by using the following code:

auto size = Director::getInstance...