在本次做毕业设计过程中,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.org
- Permission is hereby granted,free of charge,to any person obtaining a copy
- of this software and associated documentation files (the "Software"),to deal
- in the Software without restriction,including without limitation the rights
- to use,copy,modify,merge,publish,distribute,sublicense,and/or sell
- copies of the Software,and to permit persons to whom the Software is
- furnished to do so,subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR
- IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER
- 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
- THE 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中实现这个方法:
因为暂时也就需要释放json文件所有动画,就不用继续拓展了,原本想像公司的那样,直接是传入Layout*root对象,这样就不用写json了,不过看了下公司的,太过麻烦,得改好几个文件的实现方式,斟酌下还是写个简单够用的吧= =。
- /****************************************************************************
- Copyright (c) 2013-2014 Chukong Technologies Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE 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,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,stExpCocoNode* pCocoNode)
- {
- std::string path = file;
- ssize_t pos = path.find_last_of("/");
- std::string fileName = path.substr(pos + 1,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,CallFunc* func)
- {
- ActionObject* action = getActionByName(jsonName,actionName);
- if (action)
- {
- action->play(func);
- }
- return action;
- }
- ActionObject* ActionManagerEx::stopActionByName(const char* 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,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);
- }
- }