cocos2d-x3.4json动画删除问题
来源:互联网 发布:有限元分析软件msc 编辑:程序博客网 时间:2024/05/01 14:42
在本次做毕业设计过程中,cocos studio2.0版本试用了下,导入代码啥的倒是便捷,csb唯一需要注意的是目录问题,哪怕加载csb文件时已经写了目录,也需要在代码中通过setSearchPaths添加该文件目录,否则会找不到资源。但是就本身UI编辑来说,我竟然找不到控件层级怎么调整,在属性中各种值也用不惯,动画编辑器还行,亮点也就是能直接运行了吧= =。所以自己还是守旧一点,用的cocosstudio1.6的版本了。
但是在cocos2d-x3.4中,场景切换的时候需要把cocosstudio的动画release了,否则将会造成再次进入该场景时出现野指针(即指向上个界面的动画对象),很奇怪我居然不知道,底层中只有一个releaseActions()方法,也就是会直接release所有动画,别的json的动画也会被释放,然后翻了下在公司用的底层,果然完全被改了= =。所以自己写一个根据json名来释放的方法吧:
找到CCActionManagerEx的头文件和cpp,或者直接在代码中找ActionManagerEx类中找到定义;
/****************************************************************************Copyright (c) 2013-2014 Chukong Technologies Inc.http://www.cocos2d-x.orgPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 INTHE SOFTWARE.****************************************************************************/#ifndef __ActionMANAGER_H__#define __ActionMANAGER_H__#include "cocostudio/CCActionObject.h"#include "cocostudio/DictionaryHelper.h"#include "cocostudio/CocosStudioExport.h"namespace cocostudio { class CocoLoader;struct stExpCocoNode;class CC_STUDIO_DLL ActionManagerEx:public cocos2d::Ref{public:/*** Default constructor* @js ctor*/ActionManagerEx();/*** Default destructor* @js NA* @lua NA*/virtual ~ActionManagerEx();/*** Gets the static instance of ActionManager.* @js getInstance* @lua getInstance*/static ActionManagerEx* getInstance();/*** Purges ActionManager point.* @js purge* @lua destroyActionManager*/static void destroyInstance();/*** Gets an ActionObject with a name.** @param jsonName UI file name** @param actionName action name in the UI file.** @return ActionObject which named as the param name*/ActionObject* getActionByName(const char* jsonName,const char* actionName);/*** Play an Action with a name.** @param jsonName UI file name** @param actionName action name in teh UIfile.** @return ActionObject which named as the param name*/ActionObject* playActionByName(const char* jsonName,const char* actionName);/*** Play an Action with a name.** @param jsonName UI file name** @param actionName action name in teh UIfile.** @param func ui action call back*/ActionObject* playActionByName(const char* jsonName,const char* actionName, cocos2d::CallFunc* func);/*** Stop an Action with a name.** @param jsonName UI file name** @param actionName action name in teh UIfile.** @return ActionObject which named as the param name*/ActionObject* stopActionByName(const char* jsonName,const char* actionName);/*init properties with json dictionay*/void initWithDictionary(const char* jsonName,const rapidjson::Value &dic, Ref* root); void initWithBinary(const char* file, Ref* root, CocoLoader* cocoLoader, stExpCocoNode*pCocoNode);/*** Release all actions.**/void releaseActions();//here,在这里下边添加一个函数protected:std::unordered_map<std::string, cocos2d::Vector<ActionObject*>> _actionDic;};}#endif在
void releaseActions();方法下边添加一个新方法:
void releaseAction(const char* jsonName);
而后在cpp中实现这个方法:
/****************************************************************************Copyright (c) 2013-2014 Chukong Technologies Inc.http://www.cocos2d-x.orgPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 INTHE SOFTWARE.****************************************************************************/#include "cocostudio/CCActionManagerEx.h"#include "cocostudio/CocoLoader.h"using namespace cocos2d;namespace cocostudio {static ActionManagerEx* sharedActionManager = nullptr;ActionManagerEx* ActionManagerEx::getInstance(){if (!sharedActionManager) {sharedActionManager = new (std::nothrow) ActionManagerEx();}return sharedActionManager;}void ActionManagerEx::destroyInstance(){if (sharedActionManager != nullptr){sharedActionManager->releaseActions();CC_SAFE_DELETE(sharedActionManager);}}ActionManagerEx::ActionManagerEx(){}ActionManagerEx::~ActionManagerEx(){_actionDic.clear();}void ActionManagerEx::initWithDictionary(const char* jsonName, const rapidjson::Value &dic, Ref* root){std::string path = jsonName;ssize_t pos = path.find_last_of("/");std::string fileName = path.substr(pos + 1, path.length());CCLOG("filename == %s", fileName.c_str());cocos2d::Vector<ActionObject*> actionList;int actionCount = DICTOOL->getArrayCount_json(dic, "actionlist");for (int i = 0; i < actionCount; i++) {ActionObject* action = new (std::nothrow) ActionObject();action->autorelease();const rapidjson::Value &actionDic = DICTOOL->getDictionaryFromArray_json(dic, "actionlist", i);action->initWithDictionary(actionDic, root);actionList.pushBack(action);}_actionDic[fileName] = actionList;}void ActionManagerEx::initWithBinary(const char* file,cocos2d::Ref *root,CocoLoader* cocoLoader,stExpCocoNode*pCocoNode){std::string path = file;ssize_t pos = path.find_last_of("/");std::string fileName = path.substr(pos + 1, path.length());CCLOG("filename == %s", fileName.c_str());cocos2d::Vector<ActionObject*> actionList;stExpCocoNode *stChildArray = pCocoNode->GetChildArray(cocoLoader);stExpCocoNode *actionNode = nullptr;for (int i = 0; i < pCocoNode->GetChildNum(); ++i) {std::string key = stChildArray[i].GetName(cocoLoader);if (key == "actionlist") {actionNode = &stChildArray[i];break;}}if (nullptr != actionNode){int actionCount = actionNode->GetChildNum();for (int i = 0; i < actionCount; ++i) {ActionObject* action = new (std::nothrow) ActionObject();action->autorelease();action->initWithBinary(cocoLoader, &actionNode->GetChildArray(cocoLoader)[i], root);actionList.pushBack(action);}}_actionDic[fileName] = actionList;}ActionObject* ActionManagerEx::getActionByName(const char* jsonName, const char* actionName){std::string path = jsonName;ssize_t pos = path.find_last_of("/");std::string fileName = path.substr(pos + 1, path.length());CCLOG("find filename == %s", fileName.c_str());auto iterator = _actionDic.find(fileName);if (iterator == _actionDic.end()){return nullptr;}auto actionList = iterator->second;for (int i = 0; i < actionList.size(); i++){ActionObject* action = actionList.at(i);if (strcmp(actionName, action->getName()) == 0){return action;}}return nullptr;}ActionObject* ActionManagerEx::playActionByName(const char* jsonName, const char* actionName){ActionObject* action = getActionByName(jsonName, actionName);if (action){action->play();}return action;}ActionObject* ActionManagerEx::playActionByName(const char* jsonName, const char* actionName, CallFunc* func){ActionObject* action = getActionByName(jsonName, actionName);if (action){action->play(func);}return action;}ActionObject* ActionManagerEx::stopActionByName(const char* jsonName, const char* actionName){ActionObject* action = getActionByName(jsonName, actionName);if (action){action->stop();}return action;}void ActionManagerEx::releaseActions(){std::unordered_map<std::string, cocos2d::Vector<ActionObject*>>::iterator iter;for (iter = _actionDic.begin(); iter != _actionDic.end(); iter++){cocos2d::Vector<ActionObject*> objList = iter->second;ssize_t listCount = objList.size();for (ssize_t i = 0; i < listCount; i++) {ActionObject* action = objList.at(i);if (action != nullptr) {action->stop();}}objList.clear();}_actionDic.clear();}//****************在这里实现void ActionManagerEx::releaseAction(const char* jsonName){//首先,先和获取动画一样的方式,获取出该json的动画列表std::string path = jsonName;ssize_t pos = path.find_last_of("/");std::string fileName = path.substr(pos + 1, path.length());CCLOG("find filename == %s", fileName.c_str());auto iterator = _actionDic.find(fileName);if (iterator == _actionDic.end()){return;}auto actionList = iterator->second;//而后遍历动画列表,将该json文件每个动画停止for (auto & action : actionList){action->stop();}actionList.clear();_actionDic.erase(fileName);}}因为暂时也就需要释放json文件所有动画,就不用继续拓展了,原本想像公司的那样,直接是传入Layout*root对象,这样就不用写json了,不过看了下公司的,太过麻烦,得改好几个文件的实现方式,斟酌下还是写个简单够用的吧= =。
1 0
- cocos2d-x3.4json动画删除问题
- Cocos2d-x3.4 EditBox的问题
- Cocos2d-x3.0 Json解析
- Cocos2d-x3.0 Json解析
- cocos2d-x3.8骨骼动画加载
- Cocos2d-x3.2 Animate帧动画
- Cocos2d-x3.2 Animate帧动画
- Cocos2d-x3.2 Animate帧动画
- Cocos2d-x3.1生成Json串
- cocos2d-x3.2 Socket传输Json字符串
- Cocos2d-x3.8.1解析Json文件
- cocos2d-x3.4 交叉编译出现问题总结
- cocos2d-x3.1.1动画制作编辑核心代码
- Cocos2d-x3.2中 简单循环动画的实现
- Cocos2d-x3.3beta0创建动画的3种方式
- cocos2d-x3.4源码研究-1
- cocos2D-x3.4 新建项目的批处理文件
- 三、python安装cocos2d-x3.4 autoBindings
- Oracle查询server字符集+修改字符集
- STM32M4DA_AD
- 巴菲特选股10招
- Android心得--对ContentProvide分解介绍
- WinSock编程流程(2)- UDP
- cocos2d-x3.4json动画删除问题
- Oracle查看字符集后修改oracle服务端和客户端字符集的步骤
- Visual Studio 2013 如何关闭调试而不关闭IIS Express
- 判断一个二叉树是否是完全二叉树
- Binary Tree Level Order Traversal II
- Oracle RMAN DUPLICATE/RESTORE/RECOVER 支持的混合平台
- struts2文件的上传
- 分享--> 书架UI实现
- web图片延时加载两种方法