今天要写一个滚动列表功能,类似以下这样。(图片资源都是自己从天天酷跑里面抠的,仅用于学习方便)
首先,这样一个列表就和iOS里面的UITableView没什么两样,当然,Android中肯定也存在类似的控件。
在cocos2d-x引擎中參照ios中的UITableView实现了一个叫做CCTableView的类,用于创建列表,对于熟悉ios程序设计的人来说,这个内容应该是非常好理解的。
以下就介绍下CCTableView。
首先,mark几个比較好的博文。
Cocos2d-x CCTableView实现列表:http://www.tuicool.com/articles/viaQn2
cocos2d-x CCTableView:http://www.cnblogs.com/sevenyuan/archive/2013/07/25/3214534.html
cocos2d-x CCScrollView和CCTableView的使用:http://www.tuicool.com/articles/fuemq2
另外.先介绍下涉及的几个经常用法。
必须实现:
1
2
3
4
5
6
7
8
9
10
11
|
//触摸到某个cell的事件
virtual
//定制每一个cell的size
virtual
//定制每一个cell的内容
virtual
//确定这个tableview的cell行数
virtual
|
选择实现:
1
2
3
4
|
//设置单元格高亮的状态
virtual
//设置单元格非高亮的状态
virtual
|
必须实现:
1
2
3
|
//因为CCTableView是继承CCScrollView,所以要继承这两个方法
virtual
virtual
|
以下介绍实现方法。
1。使用的时候要注意要引入扩展库文件: #include "cocos-ext.h" ,而且最好要加入�: USING_NS_CC_EXT ; 这样就不用老是加前缀cocos2d::extension。
1
2
|
class
};
|
3。实现须要的方法(上述列举的三类中, 当中两类必须实现。 另一类可选。)
简单三不,就能定制属于你自己的列表了。非常easy吧。
以下给出上述天天酷跑道具列表的实现代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
//
//
//
//
//
//
//
#ifndef
#define
#include
#include
#include
USING_NS_CC_EXT; //cocos2dx定义的宏
using
class
{
public:
virtual
static
void
public:
//CCTableViewDelegate继承自CCScrollViewDelegate
virtual
virtual
//点击哪个cell
virtual
//每一个cell的size
virtual
//生成cell
virtual
//cell的数量
virtual
//按下去的时候,就是高亮显示,这里能够设置高亮状态
virtual
//松开的时候,取消高亮状态
virtual
void
CREATE_FUNC(GameInfo);
};
#endif
|
GameInfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
//
//
//
//
//
//
//
#include
#include
using
using
CCScene*
{
CCScene
GameInfo
scene->addChild(layer);
return scene;
}
bool
{
if (
{
return false ;
}
//获取屏幕大小
CCSize
//设置背景
CCSprite "pic_InfoBg.png" );
this ->setPosition(ccp(visibSize.width/2,
this ->addChild(bg_);
//加入�列表
CCTableView this ,
tableView->setDirection(kCCScrollViewDirectionVertical);
tableView->setPosition(ccp(-525,
tableView->setAnchorPoint(ccp(0,
tableView->setDelegate( this );
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this ->addChild(tableView,1);
tableView->reloadData();
return true ;
}
void
{
CCDirector::sharedDirector()->end();
#if
exit(0);
#endif
}
//cell的数量
unsigned
{
return 6;
}
//生成cell
CCTableViewCell*
{
CCString "cell_%d.png" ,idx);
CCTableViewCell
if (!cell)
{
cell new CCTableViewCell();
cell->autorelease();
//设置当前cell图片
CCSprite
iconSprite->setAnchorPoint(CCPointZero);
iconSprite->setPosition(ccp(0,
iconSprite->setTag(123);
cell->addChild(iconSprite);
}
else
{
//创建了就不须要再又一次创建了,不然你会发现图片跟文字都不正确
CCTexture2D
CCSprite
pSprite->setTexture(aTexture);
}
return cell;
}
CCSize
{
return CCSizeMake(605,
}
void
{
}
void
{
}
void
{
CCBlink
cell->runAction(blink_);
}
void
{
}
void
{
}
|