cocos2d-x中CCTableView的使用

2024-06-12 19:32
文章标签 使用 cocos2d cctableview

本文主要是介绍cocos2d-x中CCTableView的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。

http://blog.csdn.net/azhou_hui/article/details/8167498

 

CCTableView用来实现滑动列表的效果。本来想用CCScrollView,不过好像有bug,一直达不到效果。最后还是选用了CCTableView

 

------------------------------------------------------------------------------------相关代码----------------------------------------------------------------------------

(1)CustomTableViewCell.h

[cpp]  view plain copy
  1. #ifndef __CUSTOMTABELVIEWCELL_H__  
  2. #define __CUSTOMTABELVIEWCELL_H__  
  3.   
  4. #include "cocos2d.h"  
  5. #include "cocos-ext.h"  
  6.   
  7. class CustomTableViewCell : public cocos2d::extension::CCTableViewCell  
  8. {  
  9. public:  
  10.     virtual void draw();  
  11. };  
  12.   
  13. #endif /* __CUSTOMTABELVIEWCELL_H__ */  

 

(2)CustomTableViewCell.cpp

[html]  view plain copy
  1. #include "CustomTableViewCell.h"  
  2.   
  3. USING_NS_CC;  
  4.   
  5. void CustomTableViewCell::draw()  
  6. {  
  7.     CCTableViewCell::draw();  
  8.     // draw bounding box  
  9.     //  CCPoint pos = getPosition();  
  10.     //  CCSize size = CCSizeMake(178, 200);  
  11.     //  CCPoint vertices[4]={  
  12.     //      ccp(pos.x+1, pos.y+1),  
  13.     //      ccp(pos.x+size.width-1, pos.y+1),  
  14.     //      ccp(pos.x+size.width-1, pos.y+size.height-1),  
  15.     //      ccp(pos.x+1, pos.y+size.height-1),  
  16.     //  };  
  17.     //  ccDrawColor4B(0, 0, 255, 255);  
  18.     //  ccDrawPoly(vertices, 4, true);  
  19. }  


(3)TableViewMenuLayer.h

[cpp]  view plain copy
  1. #ifndef __TABLEVIEWMENULAYER_H__  
  2. #define __TABLEVIEWMENULAYER_H__  
  3.   
  4. //城市界面右边的滑动菜单项列表Layer,这个用的是CCTableView  
  5.   
  6. #include "cocos2d.h"  
  7. #include "cocos-ext.h"  
  8.   
  9. using namespace cocos2d;  
  10. using namespace cocos2d::extension;  
  11.   
  12. class TableViewMenuLayer : public cocos2d::CCLayer, public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate  
  13. {  
  14. public:  
  15.     static TableViewMenuLayer *instance;  
  16.   
  17. public:  
  18.     TableViewMenuLayer(void);  
  19.     ~TableViewMenuLayer(void);  
  20.   
  21. public:  
  22.     virtual bool init();    
  23.   
  24.     CREATE_FUNC(TableViewMenuLayer);  
  25.   
  26.     virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view) {};  
  27.     virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view) {}  
  28.     virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);  
  29.     virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);  
  30.     virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);  
  31.     virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);  
  32.   
  33. };  
  34.   
  35. #endif //__TABLEVIEWMENULAYER_H__  


 

(4)TableViewMenuLayer.cpp

[cpp]  view plain copy
  1. #include "TableViewMenuLayer.h"  
  2. #include "CustomTableViewCell.h"  
  3. #include "MenuInfoLayer.h"  
  4.   
  5.   
  6. using namespace cocos2d;  
  7. using namespace cocos2d::extension;  
  8.   
  9. TableViewMenuLayer::TableViewMenuLayer(void)  
  10. {  
  11. }  
  12.   
  13. TableViewMenuLayer::~TableViewMenuLayer(void)  
  14. {  
  15. }  
  16.   
  17. //初始化静态成员    
  18. TableViewMenuLayer* TableViewMenuLayer::instance=NULL;    
  19.   
  20. // on "init" you need to initialize your instance  
  21. bool TableViewMenuLayer::init()  
  22. {  
  23.     if ( !CCLayer::init() )  
  24.     {  
  25.         return false;  
  26.     }  
  27.   
  28.     CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
  29.   
  30.     // Add the BG  
  31.     CCSprite* tableViewBG = CCSprite::create("right.png");  
  32.     tableViewBG->setPosition(ccp(winSize.width-45, winSize.height/2));  
  33.     this->addChild(tableViewBG);  
  34.     CCSprite* upBG = CCSprite::create("up.png");  
  35.     upBG->setPosition(ccp(winSize.width-45, winSize.height-30));  
  36.     this->addChild(upBG);  
  37.     CCSprite* downBG = CCSprite::create("down.png");  
  38.     downBG->setPosition(ccp(winSize.width-45, 10));  
  39.     this->addChild(downBG);  
  40.   
  41.     // Add the CCTableView  
  42.     CCTableView *tableView = CCTableView::create(this, CCSizeMake(80, 690));  
  43.     tableView->setDirection(kCCScrollViewDirectionVertical);  
  44.     tableView->setPosition(ccp(winSize.width-85,28));  
  45.     tableView->setDelegate(this);  
  46.     tableView->setVerticalFillOrder(kCCTableViewFillTopDown);  
  47.     this->addChild(tableView);  
  48.     tableView->reloadData();  
  49.   
  50.     return true;  
  51. }  
  52.   
  53. void TableViewMenuLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)  
  54. {  
  55.     //打印相应cell的Idx  
  56.     CCLOG("cell touched at index: %i", cell->getIdx());  
  57.   
  58.     //调用相应的菜单功能  
  59.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  60.     // add the MenuInfoLayer  
  61.     if (this->getChildByTag(101))  
  62.     {  
  63.         this->removeChildByTag(101,true);  
  64.     }  
  65.     MenuInfoLayer *menuInfoLayer = MenuInfoLayer::create();  
  66.     this->addChild(menuInfoLayer,0,101);  
  67.     menuInfoLayer->runAction(CCMoveBy::create(0.3,ccp(-size.width,0)));  
  68. }  
  69.   
  70. CCSize TableViewMenuLayer::cellSizeForTable(CCTableView *table)  
  71. {  
  72.     return CCSizeMake(80, 115);  
  73. }  
  74.   
  75. CCTableViewCell* TableViewMenuLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)  
  76. {  
  77.     CCString *string;  
  78.     switch(idx)  
  79.     {  
  80.         case 0: string = CCString::createWithFormat("%s""CITY"); break;  
  81.         case 1: string = CCString::createWithFormat("%s""WORLD"); break;  
  82.         case 2: string = CCString::createWithFormat("%s""BATTLE"); break;  
  83.         case 3: string = CCString::createWithFormat("%s""CityInfo"); break;  
  84.         case 4: string = CCString::createWithFormat("%s""Commander"); break;  
  85.         case 5: string = CCString::createWithFormat("%s""Equipment"); break;  
  86.         case 6: string = CCString::createWithFormat("%s""Quest"); break;  
  87.         case 7: string = CCString::createWithFormat("%s""Items"); break;  
  88.         case 8: string = CCString::createWithFormat("%s""Military"); break;  
  89.         case 9: string = CCString::createWithFormat("%s""Legion"); break;  
  90.         case 10: string = CCString::createWithFormat("%s""Rank"); break;  
  91.         case 11: string = CCString::createWithFormat("%s""Report"); break;  
  92.         case 12: string = CCString::createWithFormat("%s""News"); break;  
  93.         case 13: string = CCString::createWithFormat("%s""System"); break;  
  94.         case 14: string = CCString::createWithFormat("%s""Shop"); break;  
  95.         case 15: string = CCString::createWithFormat("%s""Friend"); break;  
  96.         default:string = CCString::createWithFormat("%s""Error");  
  97.     }  
  98.       
  99.     // the cell  
  100.     CCTableViewCell *cell = table->dequeueCell();  
  101.     if (!cell) {  
  102.         // the sprite  
  103.         cell = new CustomTableViewCell();  
  104.         cell->autorelease();  
  105.         CCSprite *sprite = CCSprite::create("CloseNormal.png");  
  106.         sprite->setAnchorPoint(ccp(0, 0));  
  107.         sprite->setPosition(ccp(0, 30));  
  108.         sprite->setTag(456);   
  109.         cell->addChild(sprite);  
  110.   
  111.         // the label  
  112.         CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Thonburi", 13);  
  113.         label->setPosition(ccp(40, 20));  
  114.         label->setTag(123);   
  115.         cell->addChild(label);  
  116.     }  
  117.     else  
  118.     {  
  119.         // the sprite  
  120.         cell->removeChildByTag(456,true);  
  121.         CCSprite *sprite ;  
  122.         switch(idx)  
  123.         {  
  124.             case 0: sprite = CCSprite::create("city.png");break;  
  125.             case 1: sprite = CCSprite::create("world.png");break;  
  126.             case 2: sprite = CCSprite::create("battle.png");break;  
  127.             case 3: sprite = CCSprite::create("cityInfo.png");break;  
  128.             case 4: sprite = CCSprite::create("hero.png");break;  
  129.             case 5: sprite = CCSprite::create("equipment.png");break;  
  130.             case 6: sprite = CCSprite::create("task.png");break;  
  131.             case 7: sprite = CCSprite::create("treasure.png");break;  
  132.             case 8: sprite = CCSprite::create("army.png");break;  
  133.             case 9: sprite = CCSprite::create("guild.png");break;  
  134.             case 10: sprite = CCSprite::create("rank.png");break;  
  135.             case 11: sprite = CCSprite::create("report.png");break;  
  136.             case 12: sprite = CCSprite::create("message.png");break;  
  137.             case 13: sprite = CCSprite::create("system.png");break;  
  138.             case 14: sprite = CCSprite::create("mall.png");break;  
  139.             case 15: sprite = CCSprite::create("friend.png");break;  
  140.             default:sprite = CCSprite::create("CloseNormal.png");  
  141.         }  
  142.         sprite->setAnchorPoint(ccp(0, 0));  
  143.         sprite->setPosition(ccp(0, 35));  
  144.         sprite->setTag(456);   
  145.         cell->addChild(sprite);  
  146.           
  147.         // the label  
  148.         CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(123);  
  149.         label->setString(string->getCString());  
  150.     }  
  151.   
  152.     return cell;  
  153. }  
  154.   
  155. unsigned int TableViewMenuLayer::numberOfCellsInTableView(CCTableView *table)  
  156. {  
  157.     return 16;  
  158. }  
[cpp]  view plain copy
  1.    
[cpp]  view plain copy
  1. (5)CityScene.cpp 调用界面,调用时直接在scene里调用该layer(包含包含的头文件)  
  2. <pre class="cpp" name="code">#include "TableViewMenuLayer.h"  
  3.           
  4. // add TableViewMenuLayer  
  5. TableViewMenuLayer *tableViewMenuLayer = TableViewMenuLayer::create();  
  6. TableViewMenuLayer::instance=tableViewMenuLayer;  
  7. this->addChild(tableViewMenuLayer,10);</pre>  
  8. <pre></pre>  
  9. <pre></pre> 

这篇关于cocos2d-x中CCTableView的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1055130

相关文章

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA