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


When a huge map is displayed in your game, a scroll view is required. It can be scrolled by a swipe, and bounce at the edge of the area. In this recipe, we explain the ScrollView class of Cocos2d-x.

How to do it...

Let's implement it right away. In this case, we doubled the size of HelloWorld.png. Further, we try to display this huge image in ScrollView. Create the scroll view by using the following code:

auto scrollView = ui::ScrollView::create();
scrollView->setPosition(Vec2());
scrollView->setDirection(ui::ScrollView::Direction::BOTH);
scrollView->setBounceEnabled(true);
this->addChild(scrollView);

auto sprite = Sprite::create("res/HelloWorld.png");
sprite->setScale(2.0f);
sprite->setPosition(sprite->getBoundingBox().size/2);
scrollView->addChild(sprite);
scrollView->setInnerContainerSize(sprite->getBoundingBox().size);
scrollView->setContentSize(sprite->getContentSize());

Let's run this code. You will see the huge HelloWorld.png...