cocos2d js-x2.x HelloWorld.cpp 没法#include其他文件

> 使用Cocos2d-x 3.x封装PageView
之前使用PageView觉得用起来好像不太爽,没办法达到我想要实现的功能,又不想修改源码。最近花了半天时间捣鼓一个出来,就命名叫XKPageView吧,XK嘛…大家好像都这么命名~
废话不多说,先看显示效果:
因为是在模拟器上录像的,所以看上去会有点卡,真机测试就不会了。直接看代码。
我就直接上代码啦,代码里面都有注释了,一些太简单的东西就不多啰嗦。
PageView.h
XKPageView.h
XKPageView
Created by Joueu on 14-11-26.
#ifndef __XKPageView__XKPageView__
#define __XKPageView__XKPageView__
#include “cocos2d.h”
#include “cocos-ext.h”
USING_NS_CC;
USING_NS_CC_EXT;
class XKPageV
class XKPageViewDelegate
virtual ~XKPageViewDelegate(){};
XKPageViewDelegate(){};
virtual Size sizeForPerPage() = 0;
virtual void pageViewDidScroll(XKPageView *pageView){};
class XKPageView:public ScrollView
static XKPageView *create(Size size,XKPageViewDelegate *delegate);
virtual bool init(Size size,XKPageViewDelegate *delegate);
void setPageSize(Size size);
virtual void setContentOffsetInDuration(Vec2 offset, float dt);
virtual void setContentOffset(Vec2 offset);
void performedAnimatedScroll(float dt);
int current_
float current_
//调整offset 的函数
void adjust(float offset);
Size pageS
CC_SYNTHESIZE(XKPageViewDelegate *, _delegate, Delegate);
void addPage(Node *node);
Node *getPageAtIndex(int index);
#endif /* defined(__XKPageView__XKPageView__) */
XKPageView.cpp
XKPageView.cpp
XKPageView
Created by Joueu on 14-11-26.
#include “XKPageView.h”
#define XKPAGEVIEW_TAG 10086
XKPageView *XKPageView::create(Size size,XKPageViewDelegate *delegate)
XKPageView *page = new XKPageView();
if (page && page -> init(size,delegate)) {
page -> autorelease();
CC_SAFE_RELEASE(page);
bool XKPageView::init(Size size,XKPageViewDelegate *delegate)
if (!ScrollView::initWithViewSize(size)) {
//必须有delegate,否则断掉
CCASSERT(delegate, “delegate should not be NULL!”);
setDelegate(delegate);
if (_delegate) {
//获取page的大小
pageSize = _delegate -> sizeForPerPage();
//init Data
pageCount = 0;
current_index = 0;
this -> setTouchEnabled(false);
auto listener = EventListenerTouchOneByOne::create();
listener -> onTouchBegan = [&](Touch *touch, Event *event){
_dragging =
if (_direction == ScrollView::Direction::HORIZONTAL) {
current_offset = this -> getContentOffset().x;
current_offset = this -> getContentOffset().y;
listener -> onTouchMoved = [&](Touch *touch, Event *event){
float start,
if (_direction == ScrollView::Direction::HORIZONTAL) {
start = touch -> getStartLocation().x;
end = touch -> getLocation().x;
start = touch -> getStartLocation().y;
end = touch -> getLocation().y;
float offset = end –
// * 2的作用是调节滚动速度,需要调滑动速度的 可以改这个值
if (_direction == ScrollView::Direction::HORIZONTAL)
this -> setContentOffset(Vec2(current_offset + offset * 2, 0));
this -> setContentOffset(Vec2(0, current_offset + offset * 2));
listener -> onTouchEnded = [&](Touch *touch, Event *event){
float start = current_offset,
if (_direction == ScrollView::Direction::HORIZONTAL) {
end = this -> getContentOffset().x;
end = this -> getContentOffset().y;
float offset = end –
this -> adjust(offset);
_dragging =
Director::getInstance() -> getEventDispatcher() -> addEventListenerWithSceneGraphPriority(listener, this);
void XKPageView::adjust(float offset)
float xOrY;
if (_direction == ScrollView::Direction::HORIZONTAL) {
vec = Vec2(-( current_index * (pageSize.width)),0);
xOrY = pageSize.
vec = Vec2(0, -( current_index * (pageSize.height)));
xOrY = pageSize.
//小于50回到原来位置
(abs(offset) < 50){
this -> setContentOffsetInDuration(vec,0.1f);
int i = abs(offset / (xOrY)) + 1;
if (offset < 0) {
current_index +=
current_index -=
if (current_index < 0) {
current_index = 0;
}else if(current_index > 10){
current_index = 10;
if (_direction == ScrollView::Direction::HORIZONTAL) {
vec = Vec2(-( current_index * (pageSize.width)),0);
vec = Vec2(0, -( current_index * (pageSize.height)));
this -> setContentOffsetInDuration(vec, 0.15f);
void XKPageView::setContentOffset(Vec2 offset)
ScrollView::setContentOffset(offset);
if (_delegate != nullptr)
_delegate -> pageViewDidScroll(this);
void XKPageView::setContentOffsetInDuration(Vec2 offset, float dt)
ScrollView::setContentOffsetInDuration(offset, dt);
this->schedule(CC_SCHEDULE_SELECTOR(XKPageView::performedAnimatedScroll));
void XKPageView::performedAnimatedScroll(float dt)
if (_dragging)
this->unschedule(CC_SCHEDULE_SELECTOR(XKPageView::performedAnimatedScroll));
if (_delegate != nullptr)
_delegate -> pageViewDidScroll(this);
void XKPageView::addPage(Node *node)
if (_direction == ScrollView::Direction::HORIZONTAL) {
node -> setPosition(Point(pageCount * pageSize.width + node -> getPositionX(),node -> getPositionY()));
this -> setContentSize(Size((pageCount + 1) * pageSize.width,pageSize.height));
node -> setPosition(Point(node -> getPositionX(),pageCount * pageSize.height + node -> getPositionY()));
this -> setContentSize(Size(pageSize.width,(pageCount + 1) *pageSize.height));
node -> setTag(pageCount + XKPAGEVIEW_TAG);
_container -> addChild(node);
pageCount ++;
Node *XKPageView::getPageAtIndex(int index)
if (index = 0) {
return _container -> getChildByTag(index + XKPAGEVIEW_TAG);
测试用的HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include &#8220;cocos2d.h&#8221;
#include &#8220;cocos-ext.h&#8221;
#include &#8220;XKPageView.h&#8221;
USING_NS_CC_EXT;
class HelloWorld : public cocos2d::Layer, public XKPageViewDelegate
// there&#8217;s no &#8216;id&#8217; in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here&#8217;s a difference. Method &#8216;init&#8217; in cocos2d-x returns bool, instead of returning &#8216;id&#8217; in cocos2d-iphone
virtual bool init();
// implement the &#8220;static create()&#8221; method manually
CREATE_FUNC(HelloWorld);
Layer *getContainer();
// PageViewDelegate
virtual Size sizeForPerPage();
virtual void pageViewDidScroll(XKPageView *pageView);
XKPageView *pageV
void addPages();
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
#include &#8220;HelloWorldScene.h&#8221;
USING_NS_CC;
#define COIN_WIDTH 212
#define COIN_GAP 30
#define COIN_COUNT 11
Scene* HelloWorld::createScene()
// &#8216;scene&#8217; is an autorelease object
auto scene = Scene::create();
// &#8216;layer&#8217; is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
// on &#8220;init&#8221; you need to initialize your instance
bool HelloWorld::init()
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
Size visibleSize = Director::getInstance() -> getVisibleSize();
//XKPageView::create(可视范围, XKPageViewDelegate, container);
auto page = XKPageView::create(Size(visibleSize.width ,COIN_WIDTH), this, this -> getContainer());
pageView = XKPageView::create(Size(visibleSize.width, COIN_WIDTH), this);
pageView -> setDirection(ScrollView::Direction::HORIZONTAL);
//XKPageView的滚动区域
page -> setContentSize(Size((COIN_WIDTH + COIN_GAP)* COIN_COUNT ,COIN_WIDTH));
//container 定位在屏幕中间
pageView -> setPosition(Point((visibleSize.width &#8211; COIN_WIDTH) * 0.5, (visibleSize.height &#8211; COIN_WIDTH) * 0.5));
addPages();
//设置裁切为false, 这样layer 溢出pageView的Size还能显示,只是为了演示效果而已~
pageView -> setClippingToBounds(false);
this -> addChild(pageView);
/*测试功能而已*/
Node *node = pageView -> getPageAtIndex(5);
log(&#8220;tag = %d&#8221;,node -> getTag());
node -> setScale(1.3f);
void HelloWorld::addPages()
Size coinSize = Sprite::create(&#8220;coin.png&#8221;) -> getContentSize();
//11个layer 加到layer 上
for (int i = 0; i < COIN_COUNT; i++) {
auto sprite = Sprite::create("coin.png");
sprite -> setPosition(coinSize.width * 0.5, coinSize.height * 0.5);
std::string str = StringUtils::format(&#8220;%d&#8221;, i);
Label *label = Label::createWithSystemFont(str, &#8220;Arial&#8221;, 60);
label -> setTextColor(Color4B(0,0,0,255));
Size size = sprite -> getContentSize();
label -> setPosition(size.width * 0.5, size.height * 0.5);
sprite -> addChild(label);
pageView -> addPage(sprite);
Layer *HelloWorld::getContainer()
auto layer = Layer::create();
Size coinSize = Sprite::create(&#8220;coin.png&#8221;) -> getContentSize();
//11个sprite 加到_container 上
for (int i = 0; i < COIN_COUNT; i++) {
auto sprite = Sprite::create("coin.png");
sprite -> setPosition(coinSize.width * 0.5 + i * (coinSize.width + COIN_GAP) , coinSize.height * 0.5);
layer -> addChild(sprite);
std::string str = StringUtils::format(&#8220;%d&#8221;, i);
Label *label = Label::createWithSystemFont(str, &#8220;Arial&#8221;, 60);
label -> setTextColor(Color4B(0,0,0,255));
Size size = sprite -> getContentSize();
label -> setPosition(size.width * 0.5, size.height * 0.5);
sprite -> addChild(label);
Size HelloWorld::sizeForPerPage()
//Delegate 的东西,返回每个Page 的Size
return Size(COIN_WIDTH + COIN_GAP, COIN_WIDTH);
void HelloWorld::pageViewDidScroll(XKPageView *pageView)
//监听滚动时间,可以再这里写滚动时候要添加的代码,比如缩放~
log(&#8220;pageViewDidScroll&#8221;);
来源网址:http://helkyle.tk/cocos-2dx-3xPageView/
转载请注明: &
or分享 (0)Cocos2d-x3.1及3.2实现截屏功能 - 推酷
Cocos2d-x3.1及3.2实现截屏功能
1、Cocos2d-x3.1
在Cocos2d-x3.2之前,Cocos引擎没有提供截图功能,但是可以通过RenderTexture实现,
1.1首先在CCDirector.h中添加如下代码:并在其中添加头文件
&2d/CCRenderTexture.h&
void saveScreenshot(const std::string& fileName,const std::function&void(const std::string&)&& callback);
1.2然后在CCDirector.cpp中添加如下代码:
void Director::saveScreenshot(const std::string &fileName, const std::function&void (const std::string &)& &callback)
//进行后缀判断
if(std::string::npos != fileName.find_last_of(&.&)){
auto extension = fileName.substr(fileName.find_last_of(&.&),fileName.length());
if (!pare(&.png&)) {
format = Image::Format::PNG;
} else if(!pare(&.jpg&)) {
format = Image::Format::JPG;
log(&cocos2d: the image can only be saved as JPG or PNG format&);
log(&cocos2d: the image can only be saved as JPG or PNG format&);
//获取屏幕尺寸,初始化一个空的渲染纹理对象
auto renderTexture = RenderTexture::create(getWinSize().width, getWinSize().height, Texture2D::PixelFormat::RGBA8888);
//清空并开始获取
renderTexture-&beginWithClear(0.0f, 0.0f, 0.0f, 0.0f);
//遍历场景节点对象,填充纹理到RenderTexture中
getRunningScene()-&visit();
//结束获取
renderTexture-&end();
//保存文件
renderTexture-&saveToFile(fileName , format);
//使用schedule在下一帧中调用callback函数
auto fullPath = FileUtils::getInstance()-&getWritablePath() + fileN
auto scheduleCallback = [&,fullPath,callback](float dt){
callback(fullPath);
auto _schedule = getRunningScene()-&getScheduler();
_schedule-&schedule(scheduleCallback, this, 0.0f,0,0.0f, false, &screenshot&);
1.3在HelloWorld.cpp中添加如下代码:
bool HelloWorld::init()
bool bRet =
CC_BREAK_IF(!Layout::init());
auto button = ui::Button::create(&CloseNormal.png&,&CloseSelected.png&);
button-&setPosition(Vec2(200,200));
addChild(button);
button-&addTouchEventListener(CC_CALLBACK_2(HomeLayer::touchEvent,this));
Director::getInstance()-&getEventDispatcher();
}while(0);
void HelloWorld::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
switch (type)
case Widget::TouchEventType::ENDED:
&span style=&white-space:pre&& &/span& //参数依次为保存图片的名字,在控制台打印保存路径信息
Director::getInstance()-&saveScreenshot(&homeLayer.png&, [&](const std::string &str){
log(&str = %s&,str.c_str());
1.4点击按钮,实现截图功能,最后图片保存在沙盒中,可在Xcode控制台下看到文件保存路径。
2、Cocos2d-x3.2
Cocos2d-x3.2中已经封装了截图共,使用如下
void Util::captureScreen(const std::function&void(bool, const std::string&)&& afterCaptured, const std::string& filename);
已发表评论数()
&&登&&&陆&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见

我要回帖

更多关于 cocos2d 的文章

 

随机推荐