#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC;
#include "cocos-ext.h"
USING_NS_CC_EXT;
// 校正滑动动画速度
#define ADJUST_ANIM_VELOCITY 800; class HelloWorld : public cocos2d::CCLayer, public cocos2d::extension::CCScrollViewDelegate
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually
CREATE_FUNC(HelloWorld); public:
void scrollViewDidScroll(cocos2d::extension::CCScrollView* view);
void scrollViewDidZoom(CCScrollView* view); public:
virtual void onEnter();
virtual void onExit(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); private:
// 当触摸事件结束时,校正CCScrollView的位置
void adjustScrollView(); private:
// 菜单回调
void menu1Callback(CCNode *pSender);
void menu2Callback(CCNode *pSender); private:
// 显示当前页
CCLabelTTF* pLabel; CCScrollView *scrollView; }; #endif // __HELLOWORLD_SCENE_H__

实现文件

 #include "HelloWorldScene.h"
#include "SimpleAudioEngine.h" using namespace cocos2d;
using namespace CocosDenshion;
USING_NS_CC_EXT; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
}
CCLayer *layer;
CCSprite *sprite2;
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object // ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize(); layer = CCLayerColor::create(ccc4(, , ,));
layer->setAnchorPoint(CCPointZero);
layer->setPosition(CCPointZero); // CCScrollView
scrollView = CCScrollView::create(CCSizeMake(, ), layer); // scrollView->setBounceable(false); CCSprite *sprite1 = CCSprite::create("HelloWorld.png");
sprite2 = CCSprite::create("HelloWorld.png");
CCSprite *sprite3 = CCSprite::create("HelloWorld.png"); // Menu
//CCMenuItemSprite *menuItem1 = CCMenuItemSprite::create(sprite1, sprite1, this, menu_selector(HelloWorld::menu1Callback));
// CCMenuItemSprite *menuItem1 = CCMenuItemSprite::create(sprite1, sprite1, this, NULL);
// menuItem1->setPosition(ccpAdd(CCPointZero, ccp(size.width / 2, size.height / 2)));
//
// CCMenuItemSprite *menuItem2 = CCMenuItemSprite::create(sprite2, sprite2, this, NULL);
// menuItem2->setPosition(ccpAdd(ccp(480, 0), ccp(size.width / 2, size.height / 2)));
//
// CCMenu *menu = CCMenu::create(menuItem1, menuItem2, NULL);
//
// menu->setPosition(CCPointZero);
// layer->addChild(menu); sprite1->setPosition(ccpAdd(CCPointZero, ccp(size.width / , size.height / )));
sprite2->setPosition(ccpAdd(ccp(, ), ccp(size.width / , size.height / )));
sprite3->setPosition(ccpAdd(ccp(, ), ccp(size.width / , size.height / ))); layer->addChild(sprite1);
layer->addChild(sprite2);
layer->addChild(sprite3); layer->setContentSize(CCSizeMake(, )); sprite1->setScale(0.5);
sprite2->setScale(0.5); scrollView->setPosition(CCPointZero);
scrollView->setContentOffset(CCPointZero); scrollView->setContentSize(CCSizeMake(*, ));
scrollView->setContainer(layer); scrollView->setDirection(kCCScrollViewDirectionHorizontal);
scrollView->setDelegate(this); this->addChild(scrollView); return true;
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit();
#endif
} void HelloWorld::scrollViewDidScroll(cocos2d::extension::CCScrollView *view)
{
} void HelloWorld::scrollViewDidZoom(cocos2d::extension::CCScrollView *view)
{
} void HelloWorld::onEnter()
{
CCLayer::onEnter();
// 这里的第三个参数一定要设置成false,
// true 即HelloWorld层吞噬掉触摸事件
// false 即HelloWorld层与CCScrollView对象先后处理触摸事件
// CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);
} void HelloWorld::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
} bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
auto postion= pTouch->getLocation();
auto p2= layer->convertToNodeSpace(postion);
// printf("p1.x=%f,p1.y=%f\n",postion.x,postion.y);
// printf("p2.x=%f,p2.y=%f\n",p2.x,p2.y); auto rect =sprite2->boundingBox();
if(rect.containsPoint(p2)){
int ffff=;
ffff++;
// printf("摸到了\n");
} int ffffr=;
ffffr++; return true;
} void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
// CCLOG("move");
} void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
adjustScrollView();
} void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
adjustScrollView();
} void HelloWorld::adjustScrollView()
{
// 关闭CCScrollView中的自调整
scrollView->unscheduleAllSelectors(); int x = scrollView->getContentOffset().x;
int offset = (int) x % ; printf("x=%d,offset=%d\n",x,offset);
// 调整位置
CCPoint adjustPos;
// 调整动画时间
float adjustAnimDelay; if (offset < -) {
// 计算下一页位置,时间
adjustPos = ccpSub(scrollView->getContentOffset(), ccp( + offset, ));
adjustAnimDelay = (float) ( + offset) / ADJUST_ANIM_VELOCITY;
printf("-240调整后的坐标=%f\n",adjustPos.x);
}
else {
// 计算当前页位置,时间
adjustPos = ccpSub(scrollView->getContentOffset(), ccp(offset, )); // 这里要取绝对值,否则在第一页往左翻动的时,保证adjustAnimDelay为正数
adjustAnimDelay = (float) abs(offset) / ADJUST_ANIM_VELOCITY; printf("现在的位移=%f\n",scrollView->getContentOffset().x); printf(">240调整后的坐标=%f\n",adjustPos.x);
} // 调整位置
scrollView->setContentOffsetInDuration(adjustPos, adjustAnimDelay);
} void HelloWorld::menu1Callback(cocos2d::CCNode *pSender)
{
CCLOG("menu1Callback");
} void HelloWorld::menu2Callback(cocos2d::CCNode *pSender)
{
CCLOG("menu2Callback");
}

这里主要研究的是他的显示,以及当滑动停止的时候,有个加速度还要滑动一段,具体如何实现,是研究的CCScrollView源码,如下

 void CCScrollView::deaccelerateScrolling(float dt)
{
if (m_bDragging)
{
this->unschedule(schedule_selector(CCScrollView::deaccelerateScrolling));
return;
} float newX, newY;
CCPoint maxInset, minInset; m_pContainer->setPosition(ccpAdd(m_pContainer->getPosition(), m_tScrollDistance)); if (m_bBounceable)
{
maxInset = m_fMaxInset;
minInset = m_fMinInset;
}
else
{
maxInset = this->maxContainerOffset();
minInset = this->minContainerOffset();
}
printf("maxinset.x=%f,mininset.x=%f\n",maxInset.x,minInset.x);
//96,-1056 //check to see if offset lies within the inset bounds
newX = MIN(m_pContainer->getPosition().x, maxInset.x);
newX = MAX(newX, minInset.x);
newY = MIN(m_pContainer->getPosition().y, maxInset.y);
newY = MAX(newY, minInset.y); newX = m_pContainer->getPosition().x;
newY = m_pContainer->getPosition().y; m_tScrollDistance = ccpSub(m_tScrollDistance, ccp(newX - m_pContainer->getPosition().x, newY - m_pContainer->getPosition().y));
m_tScrollDistance = ccpMult(m_tScrollDistance, SCROLL_DEACCEL_RATE);
this->setContentOffset(ccp(newX,newY)); if ((fabsf(m_tScrollDistance.x) <= SCROLL_DEACCEL_DIST &&
fabsf(m_tScrollDistance.y) <= SCROLL_DEACCEL_DIST) ||
newY > maxInset.y || newY < minInset.y ||
newX > maxInset.x || newX < minInset.x ||
newX == maxInset.x || newX == minInset.x ||
newY == maxInset.y || newY == minInset.y)
{
this->unschedule(schedule_selector(CCScrollView::deaccelerateScrolling));
this->relocateContainer(true);
}
}

每次坐标增加

m_tScrollDistance,但是
m_tScrollDistance会每次变为0.95*m_tScrollDistance,达到减速的效果

cocos2dx 2.0 CCScrollView的用法以及滑动的原理的更多相关文章

  1. Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析

    Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...

  2. (27)Cocos2d-x 3.0 Json用法

    Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...

  3. 一步一步了解Cocos2dx 3.0 正式版本开发环境搭建(Win32/Android)

    cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...

  4. Cocos2D-X v3.0 alpha1环境搭建

    周末看了下Cocos2D,感觉用起来还是挺爽的样子,跨平台,支持Windows, Linux, Mac, IOS, Android,WP...N多平台..还是C++开源滴,果断下下来研究下.. 最新版 ...

  5. 6、Cocos2dx 3.0游戏开发找小三之游戏的基本概念

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27689713 郝萌主友情提示: 人是习惯的产物,当你 ...

  6. UICollectionView在Swift3.0中的用法

    UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UI ...

  7. [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动

    cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...

  8. 7、Cocos2dx 3.0游戏开发找小三之3.0版本号的代码风格

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27691337 Cocos2d-x代码风格 前面我们已 ...

  9. 6、Cocos2dx 3.0游戏开发的基本概念找个小三场比赛

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27689713 郝萌主友情提示: 人是习惯的产物,当你 ...

随机推荐

  1. 网页中插入QQ在线功能

    网页中插入QQ在线功能 本随笔记录的是网页中如何插入qq在线聊天,这里讲解的是 普通QQ在线聊天操作. 例:第一种方式  使用 tencent://message/?uin=QQ号码&Site ...

  2. AsyncTask的基本使用和各个参数的说明

    AsyncTask 的执行分为四个步骤,每一步都对应一个回调方法,需要注意的是这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法.在任务的执行过程中,这些方法被自动调用. * onPreEx ...

  3. Cloudera-Manager修改集群的IP

    1.业务需求说明:由于公司网络进行了整改,随之而来的就是对应的ip网段发生了变化,其中我的hadoop的集群各主机的ip也相应的发生了改变,因此需要对各主机进行修改ip. 2.具体操作: 首先停止cd ...

  4. css2----实现三角形和带角框

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. IP地址的分类——a,b,c 类是如何划分的

    现在的IP网络使用32位地址,以点分十进制表示,如172.16.0.0.地址格式为:IP地址=网络地址+主机地址 或 IP地址=主机地址+子网地址+主机地址. IP地址类型 最初设计互联网络时,为了便 ...

  6. Ubuntu Command-Line: Enable Unlimited Scrolling in the Terminal

    At times when using the terminal, the output from a command can be so long, you simply can’t scroll ...

  7. SSH basics

    SSH, Secure SHell, was designed and created to provide the best security when accessing another comp ...

  8. 第三个Sprint冲刺第四天

    讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:强化界面功能

  9. P,NP,NPC,NPC-HARD

    P: 能在多项式时间内解决的问题 NP: 不能在多项式时间内解决或不确定能不能在多项式时间内解决,但能在多项式时间验证的问题 NPC: NP完全问题,所有NP问题在多项式时间内都能约化(Reducib ...

  10. SVD

    SVD分解(奇异值分解),本应是本科生就掌握的方法,然而却经常被忽视.实际上,SVD分解不但很直观,而且极其有用.SVD分解提供了一种方法将一个矩阵拆分成简单的,并且有意义的几块.它的几何解释可以看做 ...