Creating buttons
In this recipe, we will explain how to create buttons. Before the Button
class was released, we created a button by using the Menu
class that was introduced in the previous recipe. Due to the Button
class, it has become possible to finely control the button press.
Getting ready
To use the Button
class and other GUI parts mentioned in this chapter, you must include the CocosGUI.h
file. Let's add the following line of code in HelloWorldScene.cpp
:
#include "ui/CocosGUI.h"
How to do it...
Let's create a button using the Button
class. Firstly, you will generate a button instance by using item1.png
image that was used in the previous recipe. We will also specify the callback function as a lambda expression by using the addEventListener
method when the button is pressed. You can create the button by using the following code:
auto size = Director::getInstance()->getVisibleSize(); auto button = ui::Button::create("res/item1.png"); button->setPosition(size/2); this->addChild(button...