Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Created September 5, 2014 12:37
Show Gist options
  • Save sag333ar/52a4876e0cec85d663a1 to your computer and use it in GitHub Desktop.
Save sag333ar/52a4876e0cec85d663a1 to your computer and use it in GitHub Desktop.
Add a scroll-view in cocos2d-x having a layer with scrollable text.
// step 1. include it from extensions.
#include "extensions/GUI/CCScrollView/CCScrollView.h"
// get the screen size
Size visibleSize = Director::getInstance()->getVisibleSize();
// get the origin
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// create a container-layer with white color-background & having 90% of screen size & 1200px vertical size
auto containerLayer = LayerColor::create(Color4B(255,255,255,255.0), visibleSize.width*0.9, 1200);
// get the Lorem Ipsum Text for the Label
const char *helpText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
// create a label with 35 font size
label = LabelTTF::create(helpText, "Marker Felt", 35);
// set the color of the label. Dark-Gray here.
label->setColor(Color3B(70.0, 70.0, 70.0));
// Set the position of lable in such a way that
// adds 5% space on left & 5% on right
label->setPosition(Vec2(containerLayer->getContentSize().width*0.5,containerLayer->getContentSize().height*.5f));
// set the size of the layer
label->setDimensions(Size(containerLayer->getContentSize().width*0.9,1200));
// add the label into the container-layer
containerLayer->addChild(label);
// NOW create a Scrollview
auto scrollView = cocos2d::extension::ScrollView::create();
// set the scrollview size = container layer size
scrollView->setContentSize(Size(containerLayer->getContentSize().width, containerLayer->getContentSize().height));
// set the position of the scroll-view (here center of the screen
scrollView->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
// set the scroll-direction for scroll-view
scrollView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
// set the size of the scrollview - here 90% of visible screen.
scrollView->setViewSize(Size(visibleSize.width*0.9,visibleSize.height*0.9));
// set the content offset of the scrollview
scrollView->setContentOffset(Vec2(0, 0));
// add / set the container-layer to the scrollview.
scrollView->setContainer(containerLayer);
// add scroll-view to your scene-layer.
this->addChild(scrollView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment