cocos2dx 2.0 CCScrollView的用法以及滑动的原理
#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的用法以及滑动的原理的更多相关文章
- Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析
Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...
- (27)Cocos2d-x 3.0 Json用法
Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...
- 一步一步了解Cocos2dx 3.0 正式版本开发环境搭建(Win32/Android)
cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...
- Cocos2D-X v3.0 alpha1环境搭建
周末看了下Cocos2D,感觉用起来还是挺爽的样子,跨平台,支持Windows, Linux, Mac, IOS, Android,WP...N多平台..还是C++开源滴,果断下下来研究下.. 最新版 ...
- 6、Cocos2dx 3.0游戏开发找小三之游戏的基本概念
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27689713 郝萌主友情提示: 人是习惯的产物,当你 ...
- UICollectionView在Swift3.0中的用法
UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UI ...
- [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动
cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...
- 7、Cocos2dx 3.0游戏开发找小三之3.0版本号的代码风格
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27691337 Cocos2d-x代码风格 前面我们已 ...
- 6、Cocos2dx 3.0游戏开发的基本概念找个小三场比赛
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27689713 郝萌主友情提示: 人是习惯的产物,当你 ...
随机推荐
- 关于mysql jsp字符编码的问题解决
1.对于post表单的jsp界面,要采用与数据库统一字符编码,在页头设置 <%@ page language="java" import="java.util.*& ...
- select document library from certain list 分类: Sharepoint 2015-07-05 07:52 6人阅读 评论(0) 收藏
S using System; using Microsoft.SharePoint; namespace Test { class ConsoleApp { static void Main(str ...
- C语言样式的文件操作函数
使用C语言样式的文件操作函数,需要包含stdio.h头文件. 1.打开文件的函数: //oflag的取值为“w”或“r”,分别表示以写或读的方式打开 FILE* fd = fopen(filename ...
- lightoj1030
//Accepted 1688 KB 0 ms //http://kicd.blog.163.com/blog/static/126961911200910168335852/ //链接里的例子讲的很 ...
- CSS中相对定位与绝对定位
看了几个讲解定位的博客,觉得还不错,分享之: 博客一:http://blog.sina.com.cn/s/blog_4bcf4a5e010008o0.html 文章中,主要需要参考的有两点: 1,相对 ...
- windows核心编程---第四章 进程
上一章介绍了内核对象,这一节开始就要不断接触各种内核对象了.首先要给大家介绍的是进程内核对象.进程大家都不陌生,它是资源和分配的基本单位,而进程内核对象就是与进程相关联的一个数据结构.操作系统内核通过 ...
- iOS-Gdata XML解析配置和简单使用
简单介绍使用废话少说直接上图就能看明白... 导入libxml2,使用第三方AFNetworking网络请求,第三方XML解析GData GData需要的配置 Build Settings 里搜索,添 ...
- unix exec族函数 关于参数的疑惑
问题不出在这几个函数,而在于看后文解释器的时候发现一个很奇妙的问题. #include <unistd.h> int execl(const char *pathname, const c ...
- 【 D3.js 入门系列 --- 5 】 如何添加坐标轴
第3节中做了一个图标,但没有为它添加一个相应的坐标轴,这样不知道每一个柱形到底有多长.这一节做一个坐标轴. D3中的坐标轴都是以 svg 图的形式出现的,这也是为什么在第3节中要使用 svg 的方法做 ...
- 二叉排序树(BST)的建立
给一个非递归的吧. /* 已知,二叉树存储结构定义见bstree.h,请编写一个算法函数bstree creatBstree(int a[],int n), 以数组a中的数据作为输入建立一棵二叉排序树 ...