一、关卡选择的设计
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的同学会比较好理解!