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 list views


ListView is a class in Cocos2d-x. It is like UITableView for iOS or List View for Android. ListView is useful for creating a lot of buttons as required in the case of setting a scene. In this recipe, we will explain how to use the ListView class.

How to do it...

Here, we try to display ListView that has 20 buttons. Each button is identified with a number such as "list item 10." In addition, we display the number of the button that you selected on the log when you tap any button. Create the list view by using the following code:

auto listView = ui::ListView::create();
listView->setPosition(Vec2(size.width/2 - 200, 0.0f));
listView->setDirection(ui::ListView::Direction::VERTICAL);
listView->setBounceEnabled(true);
listView->setContentSize(size);
this->addChild(listView);

for (int i=0; i<20; i++) {
    auto layout = ui::Layout::create();
    layout->setContentSize(Size(400, 50));
    layout->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID...