在游戏中,经常需要用到列表展示,例如我现在做的卡牌游戏中就有卡牌列表好友列表需要用到CCTableView,下面简单介绍一下使用方法。

CCTableView位于扩展库文件cocos-ext.h中,它是CCScrollView的子类。引擎已经帮我们封装好了,而我们要实现列表展示只需要重写下面4个函数:

	// 处理触摸事件,可以计算点击的是哪个子项
virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell);
// 定制单元格cell的尺寸
virtual CCSize cellSizeForTable(CCTableView *table);
// 生成列表的每一项内容
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx);
// cell的数量
virtual unsigned int numberOfCellsInTableView(CCTableView *table);

由于CCTableView继承自CCScrollView,所以要实现这两个方法,但是什么都不做。

	virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view);

下面我们来写一个好友列表展示:

FriendListLayer.h

#ifndef __FRIENDLISTLAYER_H__
#define __FRIENDLISTLAYER_H__ #include "cocos2d.h"
#include "cocos-ext.h" USING_NS_CC;
USING_NS_CC_EXT; class FriendListLayer : public CCLayer, public CCTableViewDataSource, public CCTableViewDelegate
{
public:
virtual bool init(); // CCTableViewDelegate继承自CCScrollViewDelegate
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view); // 处理触摸事件,可以计算点击的是哪个子项
virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell); // 定制单元格的尺寸
virtual CCSize cellSizeForTable(CCTableView *table); // 生成列表的每一项内容
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx); // cell的数量
virtual unsigned int numberOfCellsInTableView(CCTableView *table); // 返回按钮回调函数
void menuCallback(CCObject* pSender); CREATE_FUNC(FriendListLayer);
}; #endif

FriendListLayer.cpp

#include "FriendListLayer.h"

USING_NS_CC;
USING_NS_CC_EXT; bool FriendListLayer::init()
{
if (!CCLayer::init())
{
return false;
} CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); // CCTableView使用
CCTableView* tableView = CCTableView::create(this, CCSizeMake(visibleSize.width-200, visibleSize.height-100));
tableView->setDirection(kCCScrollViewDirectionVertical); // 设置方向
tableView->setPosition(ccp(40, 30));
//tableView->setAnchorPoint(CCPointZero);
tableView->setDelegate(this);
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this->addChild(tableView);
tableView->reloadData(); // "好友列表"标题
CCSprite* title = CCSprite::create("title.png");
title->setPosition(ccp(40, visibleSize.height - title->getContentSize().height - 10));
title->setAnchorPoint(CCPointZero);
this->addChild(title); // 返回按钮
CCMenuItemImage *pBackItem = CCMenuItemImage::create(
"button1.png",
"button1.png",
this,
menu_selector(FriendListLayer::menuCallback));
pBackItem->setPosition(visibleSize.width - pBackItem->getContentSize().width - 20, visibleSize.height - 100);
CCMenu* pMenu = CCMenu::create(pBackItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1); return true;
} // 响应触摸事件
void FriendListLayer::tableCellTouched( CCTableView* table, CCTableViewCell* cell )
{
CCLOG("Cell touched at index: %i", cell->getIdx()+1);
} // 设置cell的size
CCSize FriendListLayer::cellSizeForTable( CCTableView *table )
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
return CCSizeMake(visibleSize.width, 64);
} // 具体实现每个cell
CCTableViewCell* FriendListLayer::tableCellAtIndex( CCTableView *table, unsigned int idx )
{
CCString* name = CCString::createWithFormat("Name: friend_%d", idx+1);
CCString* attack = CCString::createWithFormat("Money: %d", (idx+1)*((rand()%100)+900)); CCTableViewCell* cell = table->dequeueCell(); // 头像icon路径
std::string imagePath[10] = {"icon0.png","icon1.png","icon2.png","icon3.png","icon4.png","icon5.png","icon6.png","icon7.png","icon8.png","icon9.png"}; if (!cell)
{
cell = new CCTableViewCell();
cell->autorelease(); // 加入自动释放池
}
cell->removeAllChildrenWithCleanup(true); // 背景
CCSprite* bgSprite = CCSprite::create("cell_background.png");
bgSprite->setAnchorPoint(CCPointZero); // 设置锚点
bgSprite->setPosition(CCPointZero); // 设置位置
bgSprite->setTag(12);
cell->addChild(bgSprite); // 加入cell // 头像icon
CCSprite* iconSprite = CCSprite::create(imagePath[idx].c_str());
iconSprite->setScale(0.9);
iconSprite->setAnchorPoint(CCPointZero);
iconSprite->setPosition(ccp(25,5));
iconSprite->setTag(34);
cell->addChild(iconSprite); // 文字
CCLabelTTF* pLabel = CCLabelTTF::create(name->getCString(), "Arial", 15);
pLabel->setAnchorPoint(CCPointZero);
pLabel->setPosition(ccp(100, 35));
pLabel->setTag(56);
cell->addChild(pLabel); CCLabelTTF* pLabel1 = CCLabelTTF::create(attack->getCString(), "Arial", 15);
pLabel1->setAnchorPoint(CCPointZero);
pLabel1->setPosition(ccp(100, 10));
pLabel1->setTag(78);
cell->addChild(pLabel1); return cell;
} // cell的数量
unsigned int FriendListLayer::numberOfCellsInTableView( CCTableView *table )
{
return 10;
} // CCTableView继承自CCScrollView,所以要实现这两个方法,但是什么都不做
void FriendListLayer::scrollViewDidScroll( CCScrollView* view ){}
void FriendListLayer::scrollViewDidZoom( CCScrollView* view ){} // menu回调函数
void FriendListLayer::menuCallback( CCObject* pSender )
{
}

另外还可以重写这两个函数实现cell的点击和释放时的响应效果:

// 按下
void FriendListLayer::tableCellHighlight(CCTableView *table, CCTableViewCell *cell)
{
CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("cell_selected.png");
CCSprite *pSprite=(CCSprite *)cell->getChildByTag(12);
pSprite->setTexture(aTexture);
}
// 释放
void FriendListLayer::tableCellUnhighlight(CCTableView *table, CCTableViewCell *cell)
{
CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("cell_background.png");
CCSprite *pSprite=(CCSprite *)cell->getChildByTag(12);
pSprite->setTexture(aTexture);
}

效果图:

【Cocos2dx游戏开发】CCTableView实现滑动列表的更多相关文章

  1. cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发

     cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发 的产生 视持续更新中.... 视频存放地址例如以下:http://ipd.pps.tv/user/1058663622     ...

  2. Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇

    懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...

  3. cocos2d-x游戏开发系列教程-前言

    cocos2d-x游戏开发前景: 最近企业对于Cocos2D-X开发人才的用人需求很大,而且所提供的薪资相当可观. 为满足广大向往游戏开发行业同学的需求,特推出适合新手的Cocos2D-X手游开发教程 ...

  4. cocos2d-x 游戏开发之有限状态机(FSM) (四)

    cocos2d-x 游戏开发之有限状态机(FSM) (四) 虽然我们了解了FSM,并且可以写自己的FSM,但是有更好的工具帮我们完成这个繁琐的工作.SMC(http://smc.sourceforge ...

  5. cocos2d-x 游戏开发之有限状态机(FSM) (三)

    cocos2d-x 游戏开发之有限状态机(FSM) (三) 有限状态机简称FSM,现在我们创建一个专门的FSM类,负责管理对象(Monkey)的状态.然后Monkey类就实现了行为与状态分离.Monk ...

  6. cocos2d-x 游戏开发之有限状态机(FSM) (一)

    cocos2d-x 游戏开发之有限状态机(FSM) (一) 参考:http://blog.csdn.net/mgphuang/article/details/5845252<Cocos2d-x游 ...

  7. cocos2d-x 游戏开发之有限状态机(FSM) (二)

    cocos2d-x 游戏开发之有限状态机(FSM)  (二) 1 状态模式

  8. 《Cocos2d-x游戏开发实战精解》学习笔记4--实战一个简单的钢琴

    上一节学习了使用Cocos2d-x播放音乐的方法,但是那种方法一般只适合于播放较大的音乐,而一般比较短小的音乐(如游戏中的打斗.按键音效等)则要通过playEffect来播放.本节使用该方法以及之前学 ...

  9. 《Cocos2d-x游戏开发实战精解》学习笔记3--在Cocos2d-x中播放声音

    <Cocos2d-x游戏开发实战精解>学习笔记1--在Cocos2d中显示图像 <Cocos2d-x游戏开发实战精解>学习笔记2--在Cocos2d-x中显示一行文字 之前的内 ...

  10. 《Cocos2d-x游戏开发实战精解》学习笔记1--在Cocos2d中显示图像

    Cocos2d-x中的图像是通过精灵类来显示的.在Cocos2d-x中游戏中的每一个角色.怪物.道具都可以理解成是一个精灵,游戏背景作为一种特殊的单位将其理解成是一个精灵也没有什么不妥.在源文件本章目 ...

随机推荐

  1. maven idea

    写在前面的话:此篇文章教程是在IntelliJ IDEA中搭建的maven项目.(建议eclipse党快点转IDEA吧,IDEA大法好.逃… 1.maven的安装 前往Apache Maven官网点击 ...

  2. 第八届蓝桥杯省赛C/C++ A组第8题 包子凑数

    参考了http://blog.csdn.net/y1196645376/article/details/69718192 思路: 数论+完全背包. 实现: #include <iostream& ...

  3. WordPress主题reBorn最新破解版发布

    今天上班的时候,没事浏览网页! 突然之间发现了这么一个标题,顿时让我产生了兴趣. 标题:WordPress主题reBorn最新破解版发布 不知道什么原因,现在原网址打不开了,可能是作者怕骚扰吧. 其实 ...

  4. python自动化--mock、webservice及webdriver模拟手机浏览器

    一.mock实现 自定义一个类,用来模拟未完成部分的开发代码 class Say(): def say_hello(self): pass 自定义返回值 import unittest from un ...

  5. 通过重写.htaccess文件添加404

    如果说是用linux服务器的系统 想要给自己的网站设置404怎么弄?如果你不会给自己的Ecs服务器添加服务器管理系统,或是你购买的云虚拟主机没有304.404设置,那么就要通过自己重写文件来设置404 ...

  6. 网络编程基础_4.1TCP_服务端

    TCP_服务端 #include <stdio.h> // 1. 包含必要的头文件和库, 必须位于 windows之前 #include <WinSock2.h> #pragm ...

  7. mfc按钮悬停显示文字

    .h CToolTipCtrl m_toopTip; .cpp oninitdialog void CDlgDwgLibMan::InitTooltips(){ EnableToolTips(); m ...

  8. RabbitMQ系列(八)--顺序消费模式和迅速消息发送模式

    MQ使用过程中,有些业务场景需要我们保证顺序消费,而如果一个Producer,一个Queue,多个Consumer的情况下是无法保证顺序的 举例: 1.业务上产生三条消息,分别是对数据的增加.修改.删 ...

  9. top命令的用法

    top命令的用法 2018年07月15日 09:50:04 zhuoya_ 阅读数:1858    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/z ...

  10. python3.7实现九九乘法表

    for i in range(1,10): for j in range(1,i+1): print("%d*%d=%d" % (i,j,i*j),end=" " ...