首页 > 代码库 > coco2d-x CCScrollView实现背包翻页
coco2d-x CCScrollView实现背包翻页
#include "CCCGameScrollView.h" USING_NS_CC; USING_NS_CC_EXT; CCCGameScrollView::CCCGameScrollView() :m_fAdjustSpeed(ADJUST_ANIM_VELOCITY) , m_nPrePage(0) { } CCCGameScrollView::~CCCGameScrollView() { } bool CCCGameScrollView::init() { if (!CCScrollView::init()) { return false; } return true; } bool CCCGameScrollView::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent ) { m_BeginOffset = getContentOffset(); return CCScrollView::ccTouchBegan(pTouch, pEvent); } void CCCGameScrollView::ccTouchMoved( CCTouch *pTouch, CCEvent *pEvent ) { CCScrollView::ccTouchMoved(pTouch, pEvent); } void CCCGameScrollView::ccTouchEnded( CCTouch *pTouch, CCEvent *pEvent ) { CCPoint touchPoint = pTouch->getLocationInView(); touchPoint = CCDirector::sharedDirector()->convertToGL( touchPoint ); CCScrollView::ccTouchEnded(pTouch, pEvent); CCPoint m_EndOffset=getContentOffset(); if (m_BeginOffset.equals(m_EndOffset)) { int nPage = abs(m_EndOffset.x / (int)m_CellSize.width); m_pDelegate->scrollViewClick(nPage); // pDele->scrollViewClick(m_EndOffset,touchPoint,m_pContainer->getChildByTag(nPage),nPage); return ; } adjustScrollView(m_BeginOffset,m_EndOffset); } void CCCGameScrollView::ccTouchCancelled( CCTouch *pTouch, CCEvent *pEvent ) { CCScrollView::ccTouchCancelled(pTouch, pEvent); CCPoint m_EndOffset=getContentOffset(); adjustScrollView(m_BeginOffset,m_EndOffset); } void CCCGameScrollView::adjustScrollView( const cocos2d::CCPoint& oBegin,const cocos2d::CCPoint & oEnd) { int nPage = abs(oBegin.x / (int)m_CellSize.width); int nAdjustPage = 0; int nDis = oEnd.x-oBegin.x; if (nDis<-getViewSize().width/5) { nAdjustPage=nPage+1; } else if (nDis>getViewSize().width/5) { nAdjustPage=nPage-1; } else { nAdjustPage=nPage; } nAdjustPage=MIN(nAdjustPage,m_nPageCount-1); nAdjustPage=MAX(nAdjustPage,0); scrollToPage(nAdjustPage); } void CCCGameScrollView::scrollToPage( int nPage ) { // 关闭CCScrollView中的自调整 unscheduleAllSelectors(); CCPoint oOffset=getContentOffset(); // 调整位置 CCPoint adjustPos=ccp(-m_CellSize.width*nPage,0); // 调整动画时间 float adjustAnimDelay=ccpDistance(adjustPos,oOffset)/m_fAdjustSpeed; // 调整位置 setContentOffsetInDuration(adjustPos, adjustAnimDelay); if (nPage!=m_nPrePage) { schedule(schedule_selector(CCCGameScrollView::onScrollEnd),adjustAnimDelay,0,0.0f); m_nPrePage=nPage; } } void CCCGameScrollView::onScrollEnd(float fDelay) { int nPage=getCurPage(); m_pDelegate->scrollViewScrollEnd(nPage); } void CCCGameScrollView::scrollToNextPage() { int nCurPage=getCurPage(); if (nCurPage>=m_nPageCount-1) { return ; } scrollToPage(nCurPage+1); } void CCCGameScrollView::scrollToPrePage() { int nCurPage=getCurPage(); if (nCurPage<=0) { return ; } scrollToPage(nCurPage-1); } bool CCCGameScrollView::createContainer(CCCGameScrollViewDelegate* pDele, int nCount, //总共的Page数量 const cocos2d::CCSize & oSize )//每一个Page的尺寸 { m_nPageCount=nCount; m_CellSize=oSize; m_pDelegate = pDele; CCLayer* pContainer=CCLayer::create(); pContainer->setAnchorPoint(CCPointZero); pContainer->setPosition(CCPointZero); CCSize winSize=CCDirector::sharedDirector()->getVisibleSize(); for (int i=0;i<nCount;++i) { CCNode* pNode=CCNode::create(); pDele->scrollViewInitPage(pNode,i); pNode->setPosition(ccp(winSize.width/2+i*oSize.width,winSize.height/2)); pNode->setTag(i); pContainer->addChild(pNode); } setContainer(pContainer); setContentSize(CCSizeMake(oSize.width*nCount,oSize.height)); return true; } int CCCGameScrollView::getCurPage() { return abs(getContentOffset().x / (int)m_CellSize.width); }
#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;class CCCGameScrollViewDelegate: public cocos2d::extension::CCScrollViewDelegate{public: //初始化每个单独Page的回调 virtual bool scrollViewInitPage(cocos2d::CCNode* pPage,int nPage){return true;}; //点击一个Page的回调 virtual void scrollViewClick(int nPage ){}; //每一次切换Page的回调 virtual void scrollViewScrollEnd(int nPage){};};
#include "cocos2d.h" #include "cocos-ext.h" #include "CCCGameScrollViewDelegate.h" // 校正滑动动画速度 #define ADJUST_ANIM_VELOCITY 2000 class CCCGameScrollView : public cocos2d::extension::CCScrollView { private: CCCGameScrollViewDelegate* m_pDelegate; public: CCCGameScrollView(); ~CCCGameScrollView(); public: CREATE_FUNC(CCCGameScrollView); bool init(); bool createContainer(CCCGameScrollViewDelegate* pDele, int nCount,const cocos2d::CCSize & oSize ); virtual bool ccTouchBegan( cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent ); virtual void ccTouchMoved( cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent ); virtual void ccTouchEnded( cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent ); virtual void ccTouchCancelled( cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent ); void scrollToPage(int nPage); void scrollToNextPage(); void scrollToPrePage(); int getCurPage(); protected: void adjustScrollView(const cocos2d::CCPoint& oBegin,const cocos2d::CCPoint & oEnd); virtual void onScrollEnd(float fDelay); protected: int m_nPageCount; int m_nPrePage; cocos2d::CCPoint m_BeginOffset; cocos2d::CCSize m_CellSize; float m_fAdjustSpeed; };
#include "cocos2d.h" #include "cocos-ext.h" #include "CCCGameScrollViewDelegate.h" #include "CCCGameScrollView.h" USING_NS_CC; USING_NS_CC_EXT; class PageViewLayer : public CCLayer, public CCCGameScrollViewDelegate { public: CCCGameScrollView* m_ScrollView; virtual bool init(); virtual void scrollViewDidScroll( cocos2d::extension::CCScrollView* view ) {}; virtual void scrollViewDidZoom( cocos2d::extension::CCScrollView* view ) {}; virtual bool scrollViewInitPage( cocos2d::CCNode* pPage,int nPage ); virtual void scrollViewClick(int nPage ); virtual void scrollViewScrollEnd(int nPage ); CREATE_FUNC(PageViewLayer); };
#include "PageViewLayer.h" bool PageViewLayer::init() { if (!CCLayer::init()) { return false; } CCSprite* pBackGround=CCSprite::create("btn_bar04.png"); pBackGround->setPosition(ccp(480,320)); addChild(pBackGround); // CCScrollView m_ScrollView = CCCGameScrollView::create(); m_ScrollView->createContainer( this, 4,//总共的Page数量 CCSizeMake(440,640));//每一个Page的尺寸 m_ScrollView->setPosition(ccp(240,0)); m_ScrollView->setContentOffset(CCPointZero); m_ScrollView->setViewSize(CCSizeMake(400, 640)); m_ScrollView->setDirection(kCCScrollViewDirectionHorizontal); this->addChild(m_ScrollView); return true; } bool PageViewLayer::scrollViewInitPage( cocos2d::CCNode* pPage,int nPage ) { CCSprite *sprite = CCSprite::create("gezi.png"); sprite->setPosition(ccp(-300,0)); pPage->addChild(sprite); return true; } void PageViewLayer::scrollViewScrollEnd(int nPage ) { } void PageViewLayer::scrollViewClick(int nPage ) { }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。