cocostudio导出ui资源,也就是json文件里面可以包含UI动画,但是对它的管理是一个ActionManagerEx的单例,并且它没有提供单独一个界面里面UI动画的释放(releaseAction释放所有UI资源了),并且如果加载过一个界面的UI动画后,如果需要再次加载,那么会加载失败,因为这里保存的数据结构是一个unordered_map,同样的key,再次insert,会无效,所以需要添加一个方法,来释放某一个界面的资源:
void ActionManagerEx::releaseActionObjectByFileName(constchar * name)
{
if(_actionDic.size() <=0)
{
return;
}
std::unordered_map<std::string,cocos2d::Vector<ActionObject*>>::iterator iter =_actionDic.find(name);
if(iter == _actionDic.end())
{
}
else
{
cocos2d::Vector<ActionObject*> objList = iter->second;
objList.clear();
_actionDic.erase(iter);
}
}
上面这个是我自己写的一个方法,在ActionManagerEx::initWithDictionary()里面调用_actionDic.insert(std::pair<std::string,cocos2d::Vector<ActionObject*>>(fileName,
actionList)); 之前调用这个方法,如下代码红色部分:
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 ActionObject();
action->autorelease();
const rapidjson::Value &actionDic = DICTOOL->getDictionaryFromArray_json(dic, "actionlist", i);
action->initWithDictionary(actionDic,root);
actionList.pushBack(action);
}
releaseActionObjectByFileName(fileName.c_str());
_actionDic.insert(std::pair<std::string, cocos2d::Vector<ActionObject*>>(fileName, actionList));
}
二 ActionObject播放完毕的时候,如果是不循环播放的,需要把定时器停止掉,否则播放完毕后,回调函数里面不调用stop,那么之后的每一帧都会调用回调
(当然如果你很确定,会在actionObject的回调里面把调用stop()的话,你也可以认为这个不是bug)
{
bool isEnd = true;
for(const auto &e : _actionNodeList)
{
if (!e->isActionDoneOnce())
{
isEnd = false;
break;
}
}
if (isEnd)
{
if (_CallBack != nullptr)
{
_CallBack->execute();
}
if (_loop)
{
this->play();
}
else
{
_pScheduler->unschedule(schedule_selector(ActionObject::simulationActionUpdate), this);
}
}
}