cocos2d-x3.4json动画删除问题

前端之家收集整理的这篇文章主要介绍了cocos2d-x3.4json动画删除问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在本次做毕业设计过程中,cocos studio2.0版本试用了下,导入代码啥的倒是便捷,csb唯一需要注意的是目录问题,哪怕加载csb文件时已经写了目录,也需要在代码中通过setSearchPaths添加文件目录,否则会找不到资源。但是就本身UI编辑来说,我竟然找不到控件层级怎么调整,在属性中各种值也用不惯,动画编辑器还行,亮点也就是能直接运行了吧= =。所以自己还是守旧一点,用的cocosstudio1.6的版本了。

但是在cocos2d-x3.4中,场景切换的时候需要把cocosstudio的动画release了,否则将会造成再次进入该场景时出现野指针(即指向上个界面的动画对象),很奇怪我居然不知道,底层中只有一个releaseActions()方法,也就是会直接release所有动画,别的json的动画也会被释放,然后翻了下在公司用的底层,果然完全被改了= =。所以自己写一个根据json名来释放的方法吧:

找到CCActionManagerEx的头文件和cpp,或者直接在代码中找ActionManagerEx类中找到定义;

  1. /****************************************************************************
  2. Copyright (c) 2013-2014 Chukong Technologies Inc.
  3.  
  4. http://www.cocos2d-x.org
  5.  
  6. Permission is hereby granted,free of charge,to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"),to deal
  8. in the Software without restriction,including without limitation the rights
  9. to use,copy,modify,merge,publish,distribute,sublicense,and/or sell
  10. copies of the Software,and to permit persons to whom the Software is
  11. furnished to do so,subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR
  17. IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER
  19. LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22.  
  23. #ifndef __ActionMANAGER_H__
  24. #define __ActionMANAGER_H__
  25.  
  26. #include "cocostudio/CCActionObject.h"
  27. #include "cocostudio/DictionaryHelper.h"
  28. #include "cocostudio/CocosStudioExport.h"
  29.  
  30. namespace cocostudio {
  31. class CocoLoader;
  32. struct stExpCocoNode;
  33.  
  34. class CC_STUdio_DLL ActionManagerEx:public cocos2d::Ref
  35. {
  36. public:
  37.  
  38. /**
  39. * Default constructor
  40. * @js ctor
  41. */
  42. ActionManagerEx();
  43.  
  44. /**
  45. * Default destructor
  46. * @js NA
  47. * @lua NA
  48. */
  49. virtual ~ActionManagerEx();
  50.  
  51. /**
  52. * Gets the static instance of ActionManager.
  53. * @js getInstance
  54. * @lua getInstance
  55. */
  56. static ActionManagerEx* getInstance();
  57.  
  58. /**
  59. * Purges ActionManager point.
  60. * @js purge
  61. * @lua destroyActionManager
  62. */
  63. static void destroyInstance();
  64.  
  65. /**
  66. * Gets an ActionObject with a name.
  67. *
  68. * @param jsonName UI file name
  69. *
  70. * @param actionName action name in the UI file.
  71. *
  72. * @return ActionObject which named as the param name
  73. */
  74. ActionObject* getActionByName(const char* jsonName,const char* actionName);
  75.  
  76. /**
  77. * Play an Action with a name.
  78. *
  79. * @param jsonName UI file name
  80. *
  81. * @param actionName action name in teh UIfile.
  82. *
  83. * @return ActionObject which named as the param name
  84. */
  85. ActionObject* playActionByName(const char* jsonName,const char* actionName);
  86.  
  87. /**
  88. * Play an Action with a name.
  89. *
  90. * @param jsonName UI file name
  91. *
  92. * @param actionName action name in teh UIfile.
  93. *
  94. * @param func ui action call back
  95. */
  96. ActionObject* playActionByName(const char* jsonName,const char* actionName,cocos2d::CallFunc* func);
  97.  
  98. /**
  99. * Stop an Action with a name.
  100. *
  101. * @param jsonName UI file name
  102. *
  103. * @param actionName action name in teh UIfile.
  104. *
  105. * @return ActionObject which named as the param name
  106. */
  107. ActionObject* stopActionByName(const char* jsonName,const char* actionName);
  108.  
  109. /*init properties with json dictionay*/
  110. void initWithDictionary(const char* jsonName,const rapidjson::Value &dic,Ref* root);
  111. void initWithBinary(const char* file,Ref* root,CocoLoader* cocoLoader,stExpCocoNode* pCocoNode);
  112.  
  113. /**
  114. * Release all actions.
  115. *
  116. */
  117. void releaseActions();//here,在这里下边添加一个函数
  118.  
  119. protected:
  120. std::unordered_map<std::string,cocos2d::Vector<ActionObject*>> _actionDic;
  121. };
  122.  
  123. }
  124.  
  125. #endif
  1. void releaseActions();
方法下边添加一个新方法
void releaseAction(const char* jsonName);

而后在cpp中实现这个方法

  1. /****************************************************************************
  2. Copyright (c) 2013-2014 Chukong Technologies Inc.
  3.  
  4. http://www.cocos2d-x.org
  5.  
  6. Permission is hereby granted,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  7. THE SOFTWARE.
  8. ****************************************************************************/
  9.  
  10. #include "cocostudio/CCActionManagerEx.h"
  11. #include "cocostudio/CocoLoader.h"
  12.  
  13. using namespace cocos2d;
  14.  
  15. namespace cocostudio {
  16.  
  17. static ActionManagerEx* sharedActionManager = nullptr;
  18.  
  19. ActionManagerEx* ActionManagerEx::getInstance()
  20. {
  21. if (!sharedActionManager) {
  22. sharedActionManager = new (std::nothrow) ActionManagerEx();
  23. }
  24. return sharedActionManager;
  25. }
  26.  
  27. void ActionManagerEx::destroyInstance()
  28. {
  29. if (sharedActionManager != nullptr)
  30. {
  31. sharedActionManager->releaseActions();
  32. CC_SAFE_DELETE(sharedActionManager);
  33. }
  34.  
  35. }
  36.  
  37. ActionManagerEx::ActionManagerEx()
  38. {
  39. }
  40.  
  41. ActionManagerEx::~ActionManagerEx()
  42. {
  43. _actionDic.clear();
  44. }
  45.  
  46. void ActionManagerEx::initWithDictionary(const char* jsonName,Ref* root)
  47. {
  48. std::string path = jsonName;
  49. ssize_t pos = path.find_last_of("/");
  50. std::string fileName = path.substr(pos + 1,path.length());
  51. CCLOG("filename == %s",fileName.c_str());
  52. cocos2d::Vector<ActionObject*> actionList;
  53. int actionCount = DICTOOL->getArrayCount_json(dic,"actionlist");
  54. for (int i = 0; i < actionCount; i++) {
  55. ActionObject* action = new (std::nothrow) ActionObject();
  56. action->autorelease();
  57. const rapidjson::Value &actionDic = DICTOOL->getDictionaryFromArray_json(dic,"actionlist",i);
  58. action->initWithDictionary(actionDic,root);
  59. actionList.pushBack(action);
  60. }
  61. _actionDic[fileName] = actionList;
  62. }
  63.  
  64. void ActionManagerEx::initWithBinary(const char* file,cocos2d::Ref *root,stExpCocoNode* pCocoNode)
  65. {
  66. std::string path = file;
  67. ssize_t pos = path.find_last_of("/");
  68. std::string fileName = path.substr(pos + 1,fileName.c_str());
  69. cocos2d::Vector<ActionObject*> actionList;
  70.  
  71. stExpCocoNode *stChildArray = pCocoNode->GetChildArray(cocoLoader);
  72. stExpCocoNode *actionNode = nullptr;
  73. for (int i = 0; i < pCocoNode->GetChildNum(); ++i) {
  74. std::string key = stChildArray[i].GetName(cocoLoader);
  75. if (key == "actionlist") {
  76. actionNode = &stChildArray[i];
  77. break;
  78. }
  79. }
  80. if (nullptr != actionNode)
  81. {
  82. int actionCount = actionNode->GetChildNum();
  83. for (int i = 0; i < actionCount; ++i) {
  84. ActionObject* action = new (std::nothrow) ActionObject();
  85. action->autorelease();
  86.  
  87. action->initWithBinary(cocoLoader,&actionNode->GetChildArray(cocoLoader)[i],root);
  88.  
  89. actionList.pushBack(action);
  90. }
  91. }
  92. _actionDic[fileName] = actionList;
  93.  
  94. }
  95.  
  96.  
  97. ActionObject* ActionManagerEx::getActionByName(const char* jsonName,const char* actionName)
  98. {
  99. std::string path = jsonName;
  100. ssize_t pos = path.find_last_of("/");
  101. std::string fileName = path.substr(pos + 1,path.length());
  102. CCLOG("find filename == %s",fileName.c_str());
  103. auto iterator = _actionDic.find(fileName);
  104. if (iterator == _actionDic.end())
  105. {
  106. return nullptr;
  107. }
  108. auto actionList = iterator->second;
  109. for (int i = 0; i < actionList.size(); i++)
  110. {
  111. ActionObject* action = actionList.at(i);
  112. if (strcmp(actionName,action->getName()) == 0)
  113. {
  114. return action;
  115. }
  116. }
  117. return nullptr;
  118. }
  119.  
  120. ActionObject* ActionManagerEx::playActionByName(const char* jsonName,const char* actionName)
  121. {
  122. ActionObject* action = getActionByName(jsonName,actionName);
  123. if (action)
  124. {
  125. action->play();
  126. }
  127. return action;
  128. }
  129.  
  130. ActionObject* ActionManagerEx::playActionByName(const char* jsonName,CallFunc* func)
  131. {
  132. ActionObject* action = getActionByName(jsonName,actionName);
  133. if (action)
  134. {
  135. action->play(func);
  136. }
  137. return action;
  138. }
  139.  
  140. ActionObject* ActionManagerEx::stopActionByName(const char* jsonName,actionName);
  141. if (action)
  142. {
  143. action->stop();
  144. }
  145. return action;
  146. }
  147.  
  148. void ActionManagerEx::releaseActions()
  149. {
  150. std::unordered_map<std::string,cocos2d::Vector<ActionObject*>>::iterator iter;
  151. for (iter = _actionDic.begin(); iter != _actionDic.end(); iter++)
  152. {
  153. cocos2d::Vector<ActionObject*> objList = iter->second;
  154. ssize_t listCount = objList.size();
  155. for (ssize_t i = 0; i < listCount; i++) {
  156. ActionObject* action = objList.at(i);
  157. if (action != nullptr) {
  158. action->stop();
  159. }
  160. }
  161. objList.clear();
  162. }
  163.  
  164. _actionDic.clear();
  165. }
  166. //****************在这里实现
  167. void ActionManagerEx::releaseAction(const char* jsonName)
  168. {//首先,先和获取动画一样的方式,获取出该json的动画列表
  169. std::string path = jsonName;
  170. ssize_t pos = path.find_last_of("/");
  171. std::string fileName = path.substr(pos + 1,fileName.c_str());
  172. auto iterator = _actionDic.find(fileName);
  173. if (iterator == _actionDic.end())
  174. {
  175. return;
  176. }
  177. auto actionList = iterator->second;//而后遍历动画列表,将该json文件每个动画停止
  178. for (auto & action : actionList)
  179. {
  180. action->stop();
  181. }
  182. actionList.clear();
  183. _actionDic.erase(fileName);
  184. }
  185.  
  186.  
  187. }
因为暂时也就需要释放json文件所有动画,就不用继续拓展了,原本想像公司的那样,直接是传入Layout*root对象,这样就不用写json了,不过看了下公司的,太过麻烦,得改好几个文件的实现方式,斟酌下还是写个简单够用的吧= =。

猜你在找的Cocos2d-x相关文章