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

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

概述一、关卡选择的设计 1.TableView相关类在扩展包里,需要声明: #include “cocos-ext.h" USING_NS_CC_EXT; 2. 自定义TableView继承于CCLayer,因此可以 捕获触摸,同时继承于扩展包的CCTableViewDataSource和CCTableViewDelegate。前者是TableView的数据 源,包含了一系列和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,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); 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的同学会比较好理解! 总结

以上是内存溢出为你收集整理的Cocos2d-x::TableView关卡设计,文…全部内容,希望文章能够帮你解决Cocos2d-x::TableView关卡设计,文…所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1058259.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存