返回
顶部

修改密码

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

+1

-1

收藏

+1

-1

点赞0

评论0

做RPG游戏的时候会有剧情对,中英文混搭,要求打字效果,一个字一个字的往出蹦。先看一下xml文件[html] viewplaincopy anchor:对齐方式 0左 1右创建一个对话映射的节点TalkNode[cpp] viewplaincopyclass TalkNode { public: bool anchor; std::string icon;…

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

先看一下xml文件

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

anchor:对齐方式 0左 1右



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

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


再看TalkNode的实现

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

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

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

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



到这里就可以实现打字效果了,但我们还得自动换行,这个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字符串就可以了

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

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

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



附上效果图两张





http://www.bkjia.com/Androidjc/816817.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/816817.htmlTechArticle做RPG游戏的时候会有剧情对,中英文混搭,要求打字效果,一个字一个字的往出蹦。 先看一下xml文件 [html] view plaincopy ? xml version = 1.0 enco...

扫一扫在手机打开

评论
已有0条评论
0/150
提交
热门评论
相关推荐
今日要闻
换一批
热点排行