
.cpp文件 [cpp]@H_502_13@ view plain copy #include"HelloWorldScene.h" #include"SimpleAudioEngine.h" CCScene*HelloWorld::scene() { CCScene*scene=CCScene::create(); HelloWorld*layer=HelloWorld::create(); scene->addChild(layer); returnscene; } boolHelloWorld::init() { if(!cclayer::init()) { returnfalse; } CCSizesize=CCDirector::sharedDirector()->getWinSize(); stringstr="对敌人造成4,000的伤害、回合数延后一回合(上限10)当前战斗中,弱点属性伤害加成10%的上升。(不可重复)"; //水平间距与垂直间距都是10像素,每行宽为300像素。 cclabelTTF*ttf=horizontalSpacingANDverticalSpacing(str,"Helvetica",26,10,300); ttf->setposition(ccp(size.wIDth*0.2,size.height*0.8)); this->addChild(ttf); returntrue; } /* horizontalSpacing:水平间距 verticalSpacing:垂直间距 linewidth:一行的最大宽度 */ cclabelTTF*HelloWorld::horizontalSpacingANDverticalSpacing(string_string,floatlinewidth) { CCArray*labelTTF_arr=CCArray::create(); intindex=0; intindex_max=strlen(_string.c_str()); boolis_end=true; while(is_end){ if(_string[index]>=0&&_string[index]<=127){ stringenglishStr=_string.substr(index,1).c_str(); labelTTF_arr->addobject(cclabelTTF::create(englishStr.c_str(),Fontname,FontSize)); index+=1; } else{ stringchineseStr=_string.substr(index,3).c_str(); labelTTF_arr->addobject(cclabelTTF::create(chineseStr.c_str(),FontSize)); index+=3; } if(index>=index_max){ is_end=false; } } //以上步骤是根据ASCII码找出中英文字符,并创建成一个cclabelTTF对象存入labelTTF_arr数组中。 //下面创建的原理是在cclabelTTF对象上添加子对象cclabelTTF,以此组合成一句话,以左上角第一个字为锚点。。 cclabelTTF*returnTTF=(cclabelTTF*)labelTTF_arr->objectAtIndex(0); floatNowWIDth=returnTTF->getContentSize().wIDth; cclabelTTF*dangqiangTTF=returnTTF; cclabelTTF*lineBeginTTF=returnTTF; intarr_count=labelTTF_arr->count(); for(inti=1;i<arr_count;i++){ cclabelTTF*beforeTTF=(cclabelTTF*)labelTTF_arr->objectAtIndex(i); beforeTTF->setAnchorPoint(ccp(0,0.5)); NowWIDth+=beforeTTF->getContentSize().wIDth; if(NowWIDth>=linewidth){ NowWIDth=returnTTF->getContentSize().wIDth; dangqiangTTF=lineBeginTTF; beforeTTF->setposition(ccp(0,-dangqiangTTF->getContentSize().height*0.5-verticalSpacing)); lineBeginTTF=beforeTTF; }else{ beforeTTF->setposition(ccp(dangqiangTTF->getContentSize().wIDth+horizontalSpacing,dangqiangTTF->getContentSize().height*0.5)); } dangqiangTTF->addChild(beforeTTF); dangqiangTTF=beforeTTF; } returnreturnTTF; }
总结
以上是内存溢出为你收集整理的cocos2dx 利用CCLabelTTF设置字的水平间距与垂直间距全部内容,希望文章能够帮你解决cocos2dx 利用CCLabelTTF设置字的水平间距与垂直间距所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)