- #ifndef__HELLOWORLD_SCENE_H__
- #define__HELLOWORLD_SCENE_H__
- #include"cocos2d.h"
- usingnamespacecocos2d;
- usingnamespacestd;
- classHelloWorld:publiccocos2d::CCLayer
- {
- public:
- virtualboolinit();
- staticcocos2d::CCScene*scene();
- CCLabelTTF*horizontalSpacingANDverticalSpacing(string_string,constchar*fontName,floatfontSize,floathorizontalSpacing,floatverticalSpacing,floatlineWidth);
- CREATE_FUNC(HelloWorld);
- };
- #endif//__HELLOWORLD_SCENE_H__
.cpp文件
- #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;
- }