Cocos2d-x::TableView关卡设计,文…

一、关卡选择的设计

1.TableView相关类在扩展包里,需要声明:
#include “cocos-ext.h"
USING_NS_CC_EXT;

2. 自定义TableView继承于CCLayer,因此可以 捕获触摸,同时继承于扩展包的CCTableViewDataSource和CCTableViewDelegate。前者是TableView的数据 源,包含了一系列和TableView数据单元相关的操作,后者则是继承于CCScrollViewDelegate抽象类,声明了一些和滚动,放大,触 摸回调之类的接口,然后在内部包含了一个CCTableView,这用起来有点像Android的Adapter;

3.实现

3.1 init()
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCTableView* tableView = CCTableView::create(this,winSize);

tableView->setDirection(kCCScrollViewDirectionHorizontal);//设置方向
tableView->setPosition(ccp(0,winSize.height/2-50));
tableView->setDelegate(this);

this->addChild(tableView);
tableView->reloadData();

3.2 触摸回调tableCellTouched
void TableViewTestLayer::tableCellTouched(CCTableView* table,CCTableViewCell* cell)
{
     CCLOG(“cell touched at index: %i”,cell->getInd());
}

3.3 单元大小tableCellSizeForIndex
CCSize TableViewTestLayer::tableCellSizeForIndex(CCTableView* table,unsigned int idx)
{
     return CCSizeMake(120,120);
}

3.4 设置数据源tableCellAtIndex
CCTableViewCell* TableViewTestLayer::tableCellAtIndex(CCTableView* table,unsigned int idx)
{
      CCString* string = CCString::createWithFormat(“%d”,idx);
      CCTableViewCell* cell = table->dequeueCell();
      if(!cell){
                cell = new CCTableViewCell();
                cell->autorelease();
     
                CCSprite* sprite = CCSprite::create(“HelloWorld.png”);
                sprite->setScale(0.2f);
                sprite->setPosition(ccp(60,60));
                sprite->setTag(123);
                cell->addChild(sprite);

                CCLableTTF* label = CCLabelTTF::create(string->getCString(),”Helvetica”20.0);
                label->setPosition(ccp(60,10));
                label->setTag(456);
                cell->addChild(label);
      }else{
            CCLabelTTF* label = (CCLabelTTF* )cell->getChildByTag(456);
            label->setString(string->getCString());
      }
      return cell;
}

二、文字拖曳和定位(Text1222)

1.加入精灵和文字精灵(略)

2.触摸实现和拖拽定义
2.1 注册触摸事件
void AutoSet::registerWithTouchDispatcher(void)
{
     CCDirector* pDirector = CCDirector::sharedDirector();
     pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}
2.2 触摸开始
bool AutoSet::ccTouchBegan(cocos2d::CCTouch* pTouch,cocos2d::CCEvent* pEvent)
{
     return true;
}
2.3* 触摸过程
void AutoSet::ccTouchMoved(cocos2d::CCTouch* pTouch,cocos2d::CCEvent* pEvent)
{
     CCPoint beginPoint = pTouch->getLocationInView();
     beginPoint = CCDirector::sharedDirector()->convertToGL(beginPoint);
     CCPoint pt = text->getPosition();
      CCRect rect = CCRectMake(pt.x-30,pt.y-30,60,60);
     if(rect.containsPoint(beginPoint)
     {
          CCPoint endPoint = pTouch->getPreviousLocationInView();
          endPoint = CCDirector::sharedDirector()->convertToGL(endPoint);
     
          CCPoint offSet = ccpSub(beginPoint,endPoint);
          CCPoint toPoint = ccpAdd(text->getPosition(),offset);
          text->setPosition(toPoint);
     }
}
2.4 触摸结束
void AutoSet::ccTouchEnded(CCTouch* pTouch,CCEvent* pEvent)
{
     CCPoint lastPoint = pTouch->getLocationInView();
     lastPoint = CCDirector::sharedDirector()->convertToGL(lastPoint);
      CCRect rect = CCRectMake(330,130,60,60);
     CCMoveTo* moveto;
     if(!rect.containsPoint(lastPoint))
     {
          moveto = CCMoveTo::create(0.1f,cup(120,160));  
     }
     else
      {
          moveto = CCMoveTo::create(0.1f,ccp(360,160));
     }
     text->runAction(moveto); 
}

结后语:这个有点类似于iOS中的UITableView的写法,实际上是一样的东西,有学过iOS的同学会比较好理解!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值