在游戏和应用中经常要实现左右滑动展示游戏帮助、以列表显示内容的UI效果,就像androID中的gallery和ListVIEw。本文通过CCScrollVIEw和CCtableVIEw分别来实现这两个效果,基于cocos2d-x 2.0.4版本。
首先来简单了解一下这两个东东,CCScrollVIEw本身是一个cclayer,而CCtableVIEw是CCScrollVIEw的子类,这是引擎已经帮我们封装好了的,CCtableVIEw可以设置成横向和纵向,用它可以实现类似于gallery和ListVIEw的效果。
1. 首先实现游戏帮助界面
(1) 创建头文件galleryLayer.h
#ifndef galLERY_LAYER_H#define galLERY_LAYER_H #include "cocos2d.h"#include "SimpleAudioEngine.h"#include "cocos-ext.h" USING_NS_CC;USING_NS_CC_EXT; class galleryLayer : public cocos2d::cclayer,public CCScrollVIEwDelegate{public: virtual bool init(); voID menuCloseCallback(CCObject* pSender); CREATE_FUNC(galleryLayer); public: //scrollvIEw滚动的时候会调用 voID scrollVIEwDIDScroll(CCScrollVIEw* vIEw); //scrollvIEw缩放的时候会调用 voID scrollVIEwDIDZoom(CCScrollVIEw* vIEw); 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: //根据手势滑动的距离和方向滚动图层 voID adjustScrollVIEw(float offset); CCScrollVIEw *m_pScrollVIEw; CCPoint m_touchPoint; int m_nCurPage;}; #endif
类galleryLayer继承了CCScrollVIEwDelegate,实现了它的两个纯虚函数,主要是为了当scrollvIEw滚动和缩放时回调这两函数,这样我们就可以在这两函数中做相关 *** 作了。
(2) 看源文件galleryLayer.cpp
<pre name="code" >#include "galleryLayer.h"#include "ListVIEwLayer.h" using namespace cocos2d;using namespace cocos2d::extension; bool galleryLayer::init(){ bool bRet = false; do { CC_BREAK_IF( !cclayer::init() ); m_nCurPage = 1; CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); cclayer *pLayer = cclayer::create(); char helpstr[30] = {0}; for (int i = 1; i <= 3; ++ i) { memset(helpstr,sizeof(helpstr)); sprintf(helpstr,"bg_%02d.png",i); CCSprite *pSprite = CCSprite::create(helpstr); pSprite->setposition(ccp(visibleSize.wIDth * (i-0.5f),visibleSize.height / 2)); pLayer->addChild(pSprite); } m_pScrollVIEw = CCScrollVIEw::create(CCSizeMake(960,640),pLayer); m_pScrollVIEw->setContentOffset(CCPointZero); m_pScrollVIEw->settouchEnabled(false); m_pScrollVIEw->setDelegate(this); m_pScrollVIEw->setDirection(kCCScrollVIEwDirectionHorizontal); pLayer->setContentSize(CCSizeMake(960*3,640)); this->addChild(m_pScrollVIEw); CCSpriteFrameCache *pCache = CCSpriteFrameCache::sharedSpriteFrameCache(); pCache->addSpriteFrame(CCSpriteFrame::create("button_normal.png",CCRectMake(0,64,64)),"button_normal.png"); pCache->addSpriteFrame(CCSpriteFrame::create("button_selected.png","button_selected.png"); for (int i = 1; i <= 3; ++ i) { CCSprite *pPoint = CCSprite::createWithSpriteFramename("button_normal.png"); pPoint->setTag(i); pPoint->setposition(ccp( origin.x + (visibleSize.wIDth - 3 * pPoint->getContentSize().wIDth)/2 + pPoint->getContentSize().wIDth * (i-1),origin.y + 30)); this->addChild(pPoint); } CCSprite *pPoint = (CCSprite *)this->getChildByTag(1); pPoint->setdisplayFrame(pCache->spriteFrameByname("button_selected.png")); bRet = true; }while(0); return bRet; } voID galleryLayer::menuCloseCallback(CCObject* pSender){ } voID galleryLayer::scrollVIEwDIDScroll(cocos2d::extension::CCScrollVIEw *vIEw){ cclOG("scroll");} voID galleryLayer::scrollVIEwDIDZoom(cocos2d::extension::CCScrollVIEw *vIEw){ cclOG("zoom");} voID galleryLayer::onEnter(){ cclayer::onEnter(); CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,1,false);} voID galleryLayer::onExit(){ CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this); cclayer::onExit(); CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames();} bool galleryLayer::cctouchBegan(CCtouch *ptouch,CCEvent *pEvent){ m_touchPoint = CCDirector::sharedDirector()->convertToGL(ptouch->getLocationInVIEw()); return true;} voID galleryLayer::cctouchmoved(CCtouch *ptouch,CCEvent *pEvent){ } voID galleryLayer::cctouchended(CCtouch *ptouch,CCEvent *pEvent){ CCPoint endPoint = CCDirector::sharedDirector()->convertToGL(ptouch->getLocationInVIEw()); float distance = endPoint.x - m_touchPoint.x; if(fabs(distance) > 50) { adjustScrollVIEw(distance); }} voID galleryLayer::cctouchCancelled(cocos2d::CCtouch *ptouch,cocos2d::CCEvent *pEvent){ CCPoint endPoint = CCDirector::sharedDirector()->convertToGL(ptouch->getLocationInVIEw()); float distance = endPoint.x - m_touchPoint.x; if(fabs(distance) > 50) { adjustScrollVIEw(distance); }} voID galleryLayer::adjustScrollVIEw(float offset){ CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCSpriteFrameCache *pCache = CCSpriteFrameCache::sharedSpriteFrameCache(); CCSprite *pPoint = (CCSprite *)this->getChildByTag(m_nCurPage); pPoint->setdisplayFrame(pCache->spriteFrameByname("button_normal.png")); if (offset<0) { m_nCurPage ++; }else { m_nCurPage --; } if (m_nCurPage <1) { m_nCurPage = 1; } if(m_nCurPage > 3) { cclayer *pLayer = ListVIEwLayer::create(); CCScene *pScene = CCScene::create(); pScene->addChild(pLayer); CCDirector::sharedDirector()->replaceScene(pScene); } else { pPoint = (CCSprite *)this->getChildByTag(m_nCurPage); pPoint->setdisplayFrame(pCache->spriteFrameByname("button_selected.png")); CCPoint adjustPos = ccp(origin.x - visibleSize.wIDth * (m_nCurPage-1),0); m_pScrollVIEw->setContentOffset(adjustPos,true); }}
这里一共有三张图,是从捕鱼达人中拿出来的背景图,当滚完三张图时就跳转到ListVIEwLayer场景去,上面的代码比较容易懂。首先创建一个cclayer,包含三张背景图,设置cclayer的ContentSize,并设置三张图片的位置然后设置cclayer为CCScrollvIEw的内容,并设置CCScrollVIEw的显示区域。最后根据用户滑动的方向和距离,通过设置scrollvIEw的setContentOffset,滚动视图。CCScrollvIEw.h文件中封装了一个枚举类型,一共有四个方向,常用横向和纵向,这里使用了横向。
typedef enum { kCCScrollVIEwDirectionNone = -1,kCCScrollVIEwDirectionHorizontal = 0,kCCScrollVIEwDirectionVertical,kCCScrollVIEwDirectionBoth} CCScrollVIEwDirection;
下面来看看这部分的效果:
2. 现在来实现列表展示(ListVIEw)的效果
(1)创建ListVIEwLayer.h
#ifndef ListVIEW_LAYER_H#define ListVIEW_LAYER_H #include "cocos2d.h"#include "cocos-ext.h" class ListVIEwLayer : public cocos2d::cclayer,public cocos2d::extension::CCtableVIEwDataSource,public cocos2d::extension::CCtableVIEwDelegate{public: virtual bool init(); virtual voID scrollVIEwDIDScroll(cocos2d::extension::CCScrollVIEw* vIEw); virtual voID scrollVIEwDIDZoom(cocos2d::extension::CCScrollVIEw* vIEw); //处理触摸事件,可以计算点击的是哪一个子项 virtual voID tableCelltouched(cocos2d::extension::CCtableVIEw* table,cocos2d::extension::CCtableVIEwCell* cell); //每一项的宽度和高度 virtual cocos2d::CCSize cellSizefortable(cocos2d::extension::CCtableVIEw *table); //生成列表每一项的内容 virtual cocos2d::extension::CCtableVIEwCell* tableCellAtIndex(cocos2d::extension::CCtableVIEw *table,unsigned int IDx); //一共多少项 virtual unsigned int numberOfCellsIntableVIEw(cocos2d::extension::CCtableVIEw *table); CREATE_FUNC(ListVIEwLayer);}; #endif
ListVIEwLayer继承了CCtableVIEwDataSource和CCtableVIEwDelegate。这两个抽象类封装了几个有用的函数,我们在下面的源码中将实现它们。
(2)源文件 ListVIEwLayer.cpp
#include "ListVIEwLayer.h" USING_NS_CC;USING_NS_CC_EXT; bool ListVIEwLayer::init(){ bool bRet = false; do { CC_BREAK_IF( !cclayer::init() ); CCtableVIEw* ptableVIEw = CCtableVIEw::create(this,CCSizeMake(960,640)); ptableVIEw->setDirection(kCCScrollVIEwDirectionVertical); ptableVIEw->setposition(CCPointZero); ptableVIEw->setDelegate(this); ptableVIEw->setVerticalFillOrder(kCCtableVIEwFilltopDown); this->addChild(ptableVIEw); ptableVIEw->reloadData(); bRet = true; }while(0); return bRet;} voID ListVIEwLayer::tableCelltouched(CCtableVIEw* table,CCtableVIEwCell* cell){ cclog("cell touched at index: %i",cell->getIDx());} CCSize ListVIEwLayer::cellSizefortable(CCtableVIEw *table){ return CCSizeMake(960,120);} CCtableVIEwCell* ListVIEwLayer::tableCellAtIndex(CCtableVIEw *table,unsigned int IDx){ CCString *pString = CCString::createWithFormat("%d",IDx); CCtableVIEwCell *pCell = table->dequeueCell(); if (!pCell) { pCell = new CCtableVIEwCell(); pCell->autorelease(); CCSprite *pSprite = CCSprite::create("Listitem.png"); pSprite->setAnchorPoint(CCPointZero); pSprite->setposition(CCPointZero); pCell->addChild(pSprite); cclabelTTF *pLabel = cclabelTTF::create(pString->getCString(),"Arial",20.0); pLabel->setposition(CCPointZero); pLabel->setAnchorPoint(CCPointZero); pLabel->setTag(123); pCell->addChild(pLabel); } else { cclabelTTF *pLabel = (cclabelTTF*)pCell->getChildByTag(123); pLabel->setString(pString->getCString()); } return pCell;} unsigned int ListVIEwLayer::numberOfCellsIntableVIEw(CCtableVIEw *table){ return 20;} voID ListVIEwLayer::scrollVIEwDIDScroll(CCScrollVIEw *vIEw){} voID ListVIEwLayer::scrollVIEwDIDZoom(CCScrollVIEw *vIEw){}
首先需要创建CCtableVIEw,设置它的显示区域和显示方向,这里使用了纵向。设置每个子项的宽度和高度,子项的数量以及每个子项对应的内容。每个子项是一个CCtableVIEwCell,这里进行了优化,复用了子项对象。
下面是效果图:
源码下载地址:http://download.csdn.net/detail/zhoujianghai/4975604
总结以上是内存溢出为你收集整理的cocos2d-x CCScrollView和CCTableView的使用全部内容,希望文章能够帮你解决cocos2d-x CCScrollView和CCTableView的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)