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


A page view is similar to a scroll view, but it will be scrolled on a page-by-page basis. PageView is also a class in Cocos2d-x. In this recipe, we will explain how to use the PageView class.

How to do it...

Let's immediately get it implemented. Here, we will arrange three images of HelloWorld.png side-by-side in the page view. Create the page view by using the following code:

auto pageView = ui::PageView::create();
pageView->setPosition(Vec2());
pageView->setContentSize(size);
this->addChild(pageView);

for (int i=0; i<3; i++) {
    auto page = ui::Layout::create();
    page->setContentSize(pageView->getContentSize());

    auto sprite = Sprite::create("res/HelloWorld.png");
    sprite->setPosition(sprite->getContentSize()/2);
    page->addChild(sprite);
    pageView->insertPage(page, i);
}

pageView->addEventListener([](Ref* sender, ui::PageView::EventType type){
    if (type==ui::PageView::EventType::TURNING) {
        auto pageView =...