Header file:
#include <string>using namespace std;#include "cocos2d.h"using namespace cocos2d;class DynamicText:public CCNode{private: CC_SYNTHESIZE(CCLabelTTF *, ttf, Ttf); CC_SYNTHESIZE(string, text, Text); CC_SYNTHESIZE(int, length, Length);public: static DynamicText * create(CCPoint leftTop, CCSize dim, const char* text,const char* fontName, float fontSize, float speed); void update(float dt);public: };
Source file:
#include "DynamicText.h" DynamicText * DynamicText::create(CCPoint leftTop, CCSize dim, const char* text,const char* fontName, float fontSize, float speed){ DynamicText * pRet = new DynamicText(); pRet->ttf = CCLabelTTF::create("", dim, kCCTextAlignmentLeft,kCCVerticalTextAlignmentTop, fontName, fontSize); if(pRet) { pRet->setPosition(leftTop); pRet->setText(text); pRet->setLength(0); pRet->addChild(pRet->ttf); pRet->ttf->setAnchorPoint(ccp(0, 1)); pRet->schedule(schedule_selector(DynamicText::update), speed); pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return 0;}void DynamicText::update(float dt){ length += 1; if(length <= text.size()){ //utf-8? if(text[length-1] < 0){ length += 2; } ttf->setString(text.substr(0, length).c_str()); }else{ unscheduleAllSelectors(); }}
Example:
Dynamictext * dt = dynamictext: Create (CCP (-512 + 30,-384 + 130), ccsizemake (1024-60,130), "Unicode does not have any character in the range D800-DFFF, this range is agreed in the basic multilingual plane for the UTF-16 extension identifier secondary plane (two UTF-16 representing one secondary flat character ). "," ", 30, 0.02); node-> addchild (DT );