cocos2d-x实现游戏剧情对话——打字效果

        cocos2d-x实现游戏剧情对话——打字效果       

386人阅读 评论(0) 收藏 举报

做RPG游戏的时候会有剧情对,中英文混搭,要求打字效果,一个字一个字的往出蹦。

先看一下xml文件

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2.  
  3. <plist version="1.0"> 
  4.   <talk anchor="0" icon="talk/icon.png" name="路人甲:" content="  听说他已经30了。"/> 
  5.   <talk anchor="1" icon="talk/icon.png" name="路人乙:" content="  是啊!都30了还叫to3(奔三),装嫩呢!"/> 
  6.   <talk anchor="0" icon="talk/icon.png" name="路人甲:" content="  哈哈哈……"/> 
  7. </plist> 

anchor:对齐方式 0左 1右



创建一个对话映射的节点TalkNode

  1. class TalkNode 
  2. public
  3.         bool anchor; 
  4.         std::string icon; 
  5.         std::string name; 
  6.         std::string content; 
  7.         void init(TiXmlNode *node); 
  8.         bool getAnchor(){return anchor;}; 
  9.         const char* getIcon(){returnicon.c_str();}; 
  10.         const char* getName(){returnname.c_str();}; 
  11.         const char* getContent(){returncontent.c_str();}; 
  12.         std::string getContentByLength(intlength); 
  13.         int contentLeght; 
  14.         int getContentLength(); 
  15. }; 


再看TalkNode的实现

  1. voidTalkNode::init(TiXmlNode *node) 
  2.         TiXmlElement *element =node->ToElement(); 
  3.   
  4.         int intValue; 
  5.         if(element->QueryIntAttribute("anchor", &intValue) ==TIXML_SUCCESS) 
  6.         { 
  7.                anchor = intValue; 
  8.         } 
  9.         else 
  10.         { 
  11.                anchor = false
  12.         } 
  13.         
  14.         name =element->Attribute("name"); 
  15.         icon =element->Attribute("icon"); 
  16.         content =element->Attribute("content"); 
  17.         contentLeght = 0; 
  18.   
  19.         int length = content.length(); 
  20.         int i = 0; 
  21.         while(i < length) 
  22.         { 
  23.                char ch = getContent()[i]; 
  24.                //重点在这里 
  25.                //中文在ASCII码中是-127~0 
  26.                if (ch > -127 && ch< 0) 
  27.                { 
  28.                        //这里为什么+=3呢 
  29.                        //因为一个中文占3个字节 
  30.                        i+=3; 
  31.                } 
  32.                else 
  33.                { 
  34.                        i++; 
  35.                } 
  36.                contentLeght++; 
  37.         } 
  38. //获取内容的总长度 
  39. int TalkNode::getContentLength() 
  40.     return contentLeght; 
  41.  
  42. //返回所需长度的字符串 
  43. std::string TalkNode::getContentByLength(int length) 
  44.     if (length >= contentLeght) 
  45.     { 
  46.         return getContent(); 
  47.     } 
  48.     int i = 0; 
  49.     int index = 0; 
  50.     while(index < length) 
  51.     { 
  52.         char ch = getContent()[i]; 
  53.         //这里上面说过了 
  54.         if (ch > -127 && ch < 0) 
  55.         { 
  56.             i+=3; 
  57.         } 
  58.         else 
  59.         { 
  60.             i++; 
  61.         } 
  62.         index++; 
  63.     } 
  64.  
  65.     //截取strng 
  66.     std::string str = content.substr(0, i); 
  67.     return str; 

有同学会问TiXmlElement是哪来的? to3是用的tinyxml解析xml,度娘可以找的到。

下面看一下to3的读取xml的函数

  1. std::vector <TalkNode*> talkList; 
  2. void Talking::readXml(const char *filename) 
  3.     unsigned long filesize; 
  4.     const char *path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(filename); 
  5.     char *buffer = (char *)CCFileUtils::sharedFileUtils()->getFileData(path, "rb", &filesize); 
  6.  
  7.  
  8.     if (buffer == NULL) 
  9.     { 
  10.         return
  11.     } 
  12.  
  13.     TiXmlDocument doc; 
  14.     doc.Parse(buffer); 
  15.     TiXmlNode *root = doc.FirstChild("plist");  
  16.  
  17.     if (root) 
  18.     { 
  19.         TiXmlElement *element = root->ToElement(); 
  20.         for (TiXmlNode* entityNode = root->FirstChild(); entityNode; entityNode = entityNode->NextSibling()) 
  21.         { 
  22.             TalkNode *node= new TalkNode(); 
  23.             node->init(entityNode); 
  24.             talkList.push_back(node); 
  25.         } 
  26.         talkCount = talkList.size(); 
  27.     } 



到这里就可以实现打字效果了,但我们还得自动换行,这个cocos2d-x的CCLabelTTF有提供这个API

CCLabelTTF * CCLabelTTF::create(const char *string, const char *fontName, float fontSize,  const CCSize& dimensions, CCTextAlignment hAlignment);

string:内容

fontName:字体

fontSize:字号

dimensions:显示框

hAlignment:对齐方式


最后在逻辑循环里更新你的CCLabelTTF字符串就可以了

  1. void Talking::logic(float cTime) 
  2.     TalkNode* node = (TalkNode*)talkList[talkIndex]; 
  3.     if (wordCount > node->getContentLength()) 
  4.     { 
  5.         return
  6.     } 
  7.  
  8.  
  9.     wordCount++; 
  10.     CCLabelTTF* label = (CCLabelTTF*)this->getChildByTag(kTalkingLabelTag); 
  11.     label->setString(node->getContentByLength(wordCount).c_str()); 

再加上点击屏幕显示全部内容

  1. bool Talking::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) 
  2.     TalkNode* node = (TalkNode*)talkList[talkIndex]; 
  3.     if (wordCount < node->getContentLength()) 
  4.     { 
  5.         wordCount = node->getContentLength(); 
  6.     } 
  7.      
  8.     return true



附上效果图两张



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值