Cocos2d-x滚动列表具体解释(CCScrollView的使用)

Cocos2d-x滚动列表具体解释(CCScrollView的使用)

今天要写一个滚动列表功能,类似以下这样。(图片资源都是自己从天天酷跑里面抠的,仅用于学习方便)

<span>Cocos2d-x滚动列表具体解释(CCScrollView的使用)</span>

首先,这样一个列表就和iOS里面的UITableView没什么两样,当然,Android中肯定也存在类似的控件。

在cocos2d-x引擎中參照ios中的UITableView实现了一个叫做CCTableView的类,用于创建列表,对于熟悉ios程序设计的人来说,这个内容应该是非常好理解的。

以下就介绍下CCTableView。

首先,mark几个比較好的博文。

Cocos2d-x CCTableView实现列表:http://www.tuicool.com/articles/viaQn2

另外.先介绍下涉及的几个经常用法。

必须实现:

1
2
3
4
5
6
7
8
9
10
11
//触摸到某个cell的事件
    
virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
     
    
//定制每一个cell的size
    
virtual cocos2d::CCSize tableCellSizeForIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
     
    
//定制每一个cell的内容
    
virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
     
    
//确定这个tableview的cell行数
    
virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);

选择实现:

1
2
3
4
//设置单元格高亮的状态
    
virtual void tableCellHighlight(CCTableView* table, CCTableViewCell* cell);
    
//设置单元格非高亮的状态
    
virtual void tableCellUnhighlight(CCTableView* table, CCTableViewCell* cell);

必须实现:

1
2
3
//因为CCTableView是继承CCScrollView,所以要继承这两个方法
    
virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view) {}
    
virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view) {}

以下介绍实现方法。

1。使用的时候要注意要引入扩展库文件: #include “cocos-ext.h” ,而且最好要加入�: USING_NS_CC_EXT ; 这样就不用老是加前缀cocos2d::extension。

2。要继承CCTableView的两个代理 CCTableViewDelegate 和 CCTableViewDataSource。比方:
1
2
class HelloWorld : public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDelegate,public cocos2d::extension::CCTableViewDataSource{ 
};

3。实现须要的方法(上述列举的三类中, 当中两类必须实现。 另一类可选。)

简单三不,就能定制属于你自己的列表了。非常easy吧。

以下给出上述天天酷跑道具列表的实现代码。


GameInfo.h
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
//
//  GameInfo.h
//  RunGame
//
//  Created by zhanglin on 14-05-14.
//
//
 
#ifndef __RunGame__GameInfo__
#define __RunGame__GameInfo__
 
#include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
//cocos2dx定义的宏
using namespace cocos2d;
 
class GameInfo : public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDelegate,cocos2d::extension::CCTableViewDataSource
{
public:
    
virtual bool init();
 
    
static cocos2d::CCScene* scene();
     
    
void menuCloseCallback(CCObject* pSender);
     
     
public:
     
    
//CCTableViewDelegate继承自CCScrollViewDelegate
    
virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view);
     
    
virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view);
     
    
//点击哪个cell
    
virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
    
//每一个cell的size
    
virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
    
//生成cell
    
virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
    
//cell的数量
    
virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
     
    
//按下去的时候,就是高亮显示,这里能够设置高亮状态
    
virtual void tableCellHighlight(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
     
    
//松开的时候,取消高亮状态
    
virtual void tableCellUnhighlight(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
     
     
     
    
void scrollBar(cocos2d::extension::CCTableView* table);
 
    
CREATE_FUNC(GameInfo);
};
#endif /* defined(__RunGame__GameInfo__) */

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
//
//  GameInfo.cpp
//  RunGame
//
//  Created by zhanglin on 14-05-14.
//
//
 
#include "GameInfo.h"
#include "SimpleAudioEngine.h"
 
using namespace cocos2d;
using namespace CocosDenshion;
 
CCScene* GameInfo::scene()
{
    
CCScene *scene = CCScene::create();
     
    
GameInfo *layer = GameInfo::create();
 
    
scene->addChild(layer);
     
    
return
scene;
}
 
bool GameInfo::init()
{
    
if
( !CCLayer::init() )
    
{
        
return
false
;
    
}
     
    
//获取屏幕大小
    
CCSize visibSize=CCDirector::sharedDirector()->getVisibleSize();
     
    
//设置背景
    
CCSprite *bg_ = CCSprite::create(
"pic_InfoBg.png"
);
    
this
->setPosition(ccp(visibSize.width/2, visibSize.height/2));
    
this
->addChild(bg_);
     
    
//加入�列表
    
CCTableView *tableView=CCTableView::create(
this
, CCSizeMake(620, 450));
     
    
tableView->setDirection(kCCScrollViewDirectionVertical);
     
    
tableView->setPosition(ccp(-525, -275));
     
    
tableView->setAnchorPoint(ccp(0, 0));
    
tableView->setDelegate(
this
);
     
    
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
     
    
this
->addChild(tableView,1);
     
    
tableView->reloadData();
     
    
return
true
;
}
 
void GameInfo::menuCloseCallback(CCObject* pSender)
{
    
CCDirector::sharedDirector()->end();
 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
exit(0);
#endif
}
 
//cell的数量
unsigned int GameInfo::numberOfCellsInTableView(CCTableView *table)
{
    
return
6;
}
 
//生成cell
CCTableViewCell* GameInfo::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
    
CCString *nameString=CCString::createWithFormat(
"cell_%d.png"
,idx);
     
    
CCTableViewCell *cell = table->dequeueCell();
     
    
if
(!cell)
    
{
         
        
cell =
new
CCTableViewCell();
         
        
cell->autorelease();
         
        
//设置当前cell图片
        
CCSprite *iconSprite = CCSprite::create(nameString->getCString());
        
iconSprite->setAnchorPoint(CCPointZero);
        
iconSprite->setPosition(ccp(0, 0));
        
iconSprite->setTag(123);
        
cell->addChild(iconSprite);
         
    
}
    
else
    
{
         
        
//创建了就不须要再又一次创建了,不然你会发现图片跟文字都不正确
        
CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage(nameString->getCString());
         
        
CCSprite *pSprite=(CCSprite *)cell->getChildByTag(123);
         
        
pSprite->setTexture(aTexture);
 
    
}
     
     
    
return
cell;
     
     
}
 
 
CCSize GameInfo::cellSizeForTable(CCTableView *table)
{
    
return
CCSizeMake(605, 105);
}
 
void GameInfo::tableCellHighlight(CCTableView *table, CCTableViewCell *cell)
{
     
}
 
void GameInfo::tableCellUnhighlight(CCTableView *table, CCTableViewCell *cell)
{
 
}
 
void GameInfo::tableCellTouched(CCTableView *table, CCTableViewCell *cell)
{
    
CCBlink *blink_ = CCBlink::create(1.0f, 7);
    
cell->runAction(blink_);
}
 
void GameInfo::scrollViewDidScroll(cocos2d::extension::CCScrollView *view)
{
}
 
void GameInfo::scrollViewDidZoom(cocos2d::extension::CCScrollView *view)
{
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118996.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 电脑安装winpe_北京科兴中维新冠疫苗最新消息

    电脑安装winpe_北京科兴中维新冠疫苗最新消息在WinPE安装操作系统,最好用虚拟光驱打开安装镜像文件,或者把镜像文件解压后直接安装。最好不要用工具盘里所带的一键安装,复制等等功能,因为这些功能往往会安装一些其他的附带功能,不是清洁版的。转载于:https://www.cnblogs.com/cuihongyu3503319/p/4657993.html…

    2025年5月28日
    3
  • JAVA对象、数组转JSON[通俗易懂]

    JAVA对象、数组转JSON[通俗易懂]1.需要库:commons-beanutils-1.7.0.jarcommons-collections-3.1.jarcommons-lang-2.1.jarcommons-logging-1.1.3.jarezmorph-1.0.2.jarjson-lib-2.4-jdk15.jar2.转换方法:  //对象转json sqlModelmodel…

    2022年6月21日
    58
  • linux网卡驱动怎么看_电脑网卡驱动在哪里查看

    linux网卡驱动怎么看_电脑网卡驱动在哪里查看Linux下查看网卡驱动和版本信息查看网卡生产厂商和信号查看基本信息:lspci查看详细信息:lspci-vvv#3个小写的v查看网卡信息:lspci|grepEthernet查看网卡驱动查看网卡驱动信息:lspci-vvv#找到网卡设备的详细信息,包括网卡驱动列出加载的所有驱动,包括网卡驱动:lsmod查看模块

    2022年10月18日
    4
  • centos系统大量time wait占用的解决

    centos系统大量time wait占用的解决统计在一台前端机上高峰时间TCP连接的情况,统计命令:netstat-n|awk’/^tcp/{++S[$NF]}END{for(ainS)printa,S[a]}’除了ESTABLISHED,可以看到连接数比较多的几个状态是:FIN_WAIT1,TIME_WAIT,CLOSE_WAIT,SYN_RECV和…

    2022年5月9日
    252
  • 什么是CMS_SiteServer CMS

    什么是CMS_SiteServer CMS纵观现如今国内CMS程序,大有百花争艳的感觉,随着企业建站需求的复杂度和功能的不断扩展,传统的三五个人写一段代码即告网站建已经行不通,于是以内容管理为应用核心的CMS产品大行其道。  这些CMS系统大体上基于两套框架编写:PHP+MySQL和.NET+MSSQL。在PHP中比较有名的就有DeDeCMS、PHP168、帝国CMS、Supesite等,在.NET方面就有zoomla!…

    2022年9月29日
    4
  • C#中using语句是什么意思「建议收藏」

    C#中using语句是什么意思「建议收藏」使用using语句最终生成的其实是一个try,finally代码块,在finally代码块里释放资源。要求是:为using语句提供的对象必须实现 IDisposable接口。此接口提

    2022年8月1日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号