I doubt that we need to explain buttons much. We all know them as those things we click on to make something happen in our games. Perhaps you might use a button to change scenes or to add Sprite objects into your game play. A button intercepts a touch event and calls a predefined callback when tapped. A Button has a normal and selected state. The appearance of the Button can change based upon it's state. Creating a Button and defining its callback is simple, make sure to #include "ui/CocosGUI.h":

auto button = Button::create("normal_image.png", "selected_image.png", "disabled_image.png");

button->setTitleText("Button Text");

button->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
        switch (type)
                case ui::Widget::TouchEventType::BEGAN:
                case ui::Widget::TouchEventType::ENDED:
                        std::cout << "Button 1 clicked" << std::endl;


As you can see in the above example we specify a .png image for each of the possible states the button can be in. A Button is made up of 3 graphics that might look like this:

On screen a Button might look like this:

results matching ""

    No results matching ""