#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: };
源文件:
#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(); } }
DynamicText * dt = DynamicText::create(ccp(-512+30,-384+130), CCSizeMake(1024-60, 130), "Unicode在范围D800-DFFF中不存在任何字符,基本多文种平面中约定了这个范围用于UTF-16扩展标识辅助平面(两个UTF-16表示一个辅助平面字符)。","楷体",30, 0.02); node->addChild(dt);