- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- using namespace cocos2d;
- using namespace std;
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- CCLabelTTF* horizontalSpacingANDverticalSpacing(string _string, const char *fontName, float fontSize, float horizontalSpacing, float verticalSpacing, float lineWidth);
- 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);
- return scene;
- }
- bool HelloWorld::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- string str = "對敵人造成4,000的傷害、回合數延後一回合(上限10)當前戰鬥中,弱點屬性傷害加成10%的上升。(不可重複)";
- //水平間距與垂直間距都是10像素,每行寬爲300像素。
- CCLabelTTF* ttf = horizontalSpacingANDverticalSpacing(str, "Helvetica", 26, 10, 10, 300);
- ttf->setPosition(ccp(size.width*0.2, size.height*0.8));
- this->addChild(ttf);
- return true;
- }
- /*
- horizontalSpacing: 水平間距
- verticalSpacing: 垂直間距
- lineWidth: 一行的最大寬度
- */
- CCLabelTTF* HelloWorld::horizontalSpacingANDverticalSpacing(string _string, const char *fontName, float fontSize, float horizontalSpacing, float verticalSpacing, float lineWidth)
- {
- CCArray* labelTTF_arr = CCArray::create();
- int index = 0;
- int index_max = strlen(_string.c_str());
- bool is_end = true;
- while (is_end) {
- if (_string[index] >= 0 && _string[index] <= 127) {
- string englishStr =_string.substr(index,1).c_str();
- labelTTF_arr->addObject(CCLabelTTF::create(englishStr.c_str(), fontName, fontSize));
- index+= 1;
- }
- else{
- string chineseStr =_string.substr(index,3).c_str();
- labelTTF_arr->addObject(CCLabelTTF::create(chineseStr.c_str(), fontName, fontSize));
- index+= 3;
- }
- if (index>=index_max) {
- is_end=false;
- }
- }
- //以上步驟是根據ASCII碼找出中英文字符,並創建成一個CCLabelTTF對象存入labelTTF_arr數組中。
- //下面創建的原理是在CCLabelTTF對象上添加子對象CCLabelTTF,以此組合成一句話,以左上角第一個字爲錨點。。
- CCLabelTTF* returnTTF = (CCLabelTTF*)labelTTF_arr->objectAtIndex(0);
- float nowWidth = returnTTF->getContentSize().width;
- CCLabelTTF* dangqiangTTF = returnTTF;
- CCLabelTTF* lineBeginTTF = returnTTF;
- int arr_count = labelTTF_arr->count();
- for (int i=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;
- }
- return returnTTF;
- }