位置: 编程技术 - 正文
推荐整理分享cocos2dx ActionManager播放动画回调问题,希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!
更改代码如下 :问题解决,红色部分为更改部分,建议使用对比工具对比一下
#include "CCActionObject.h"
#include "CCActionNode.h"
#include "../Json//DictionaryHelper.h"
#include "../Json/CocoLoader.h"
NS_CC_EXT_BEGIN
ActionObject::ActionObject()
: m_ActionNodeList(NULL)
, m_name("")
, m_loop(false)
, m_bPause(false)
, m_bPlaying(false)
, m_fUnitTime(0.1f)
, m_CurrentTime(0.0f)
, m_pScheduler(NULL)
, m_CallBack(NULL)
, m_fTotalTime(0.0f)
{
m_ActionNodeList = CCArray::create();
m_ActionNodeList->retain();
m_pScheduler = CCDirector::sharedDirector()->getScheduler();
CC_SAFE_RETAIN(m_pScheduler);
}
ActionObject::~ActionObject()
{
m_ActionNodeList->removeAllObjects();
m_ActionNodeList->release();
//CC_SAFE_RELEASE_NULL(m_ActionNodeList);
CC_SAFE_RELEASE(m_CallBack);
CC_SAFE_RELEASE(m_pScheduler);
}
void ActionObject::setName(const char* name)
{
m_name.assign(name);
}
const char* ActionObject::getName()
{
return m_name.c_str();
}
void ActionObject::setLoop(bool bLoop)
{
m_loop = bLoop;
}
bool ActionObject::getLoop()
{
return m_loop;
}
void ActionObject::setUnitTime(float fTime)
{
m_fUnitTime = fTime;
int frameNum = m_ActionNodeList->count();
for ( int i = 0; i < frameNum; i )
{
ActionNode* actionNode = (ActionNode*)m_ActionNodeList->objectAtIndex(i);
actionNode->setUnitTime(m_fUnitTime);
}
}
float ActionObject::getUnitTime()
{
return m_fUnitTime;
}
float ActionObject::getCurrentTime()
{
return m_CurrentTime;
}
void ActionObject::setCurrentTime(float fTime)
{
m_CurrentTime = fTime;
}
float ActionObject::getTotalTime()
{
return m_fTotalTime;
}
bool ActionObject::isPlaying()
{
return m_bPlaying;
}
void ActionObject::initWithDictionary(const rapidjson::Value& dic,CCObject* root)
{
setName(DICTOOL->getStringValue_json(dic, "name"));
setLoop(DICTOOL->getBooleanValue_json(dic, "loop"));
setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime"));
int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist");
int maxLength = 0;
for (int i=0; i<actionNodeCount; i) {
ActionNode* actionNode = new ActionNode();
actionNode->autorelease();
const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i);
actionNode->initWithDictionary(actionNodeDic,root);
actionNode->setUnitTime(getUnitTime());
m_ActionNodeList->addObject(actionNode);
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
if(length > maxLength)
maxLength = length;
}
m_fTotalTime = maxLength*m_fUnitTime;
}
void ActionObject::initWithBinary(cocos2d::extension::CocoLoader *pCocoLoader, cocos2d::extension::stExpCocoNode *pCocoNode, cocos2d::CCObject *root)
{
stExpCocoNode *stChildNode = pCocoNode->GetChildArray(pCocoLoader);
stExpCocoNode *actionNodeList = NULL;
int count = pCocoNode->GetChildNum();
for (int i = 0; i < count; i) {
std::string key = stChildNode[i].GetName(pCocoLoader);
std::string value = stChildNode[i].GetValue(pCocoLoader);
if (key == "name") {
setName(value.c_str());
}else if (key == "loop"){
setLoop(valueToBool(value));
}else if(key == "unittime"){
setUnitTime(valueToFloat(value));
}else if (key == "actionnodelist"){
actionNodeList = &stChildNode[i];
}
}
if(NULL != actionNodeList)
{
int actionNodeCount = actionNodeList->GetChildNum();
stExpCocoNode *actionNodeArray = actionNodeList->GetChildArray(pCocoLoader);
int maxLength = 0;
for (int i=0; i<actionNodeCount; i) {
ActionNode* actionNode = new ActionNode();
actionNode->autorelease();
actionNode->initWithBinary(pCocoLoader, &actionNodeArray[i] , root);
actionNode->setUnitTime(getUnitTime());
m_ActionNodeList->addObject(actionNode);
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
if(length > maxLength)
maxLength = length;
}
m_fTotalTime = maxLength*m_fUnitTime;
}
}
int ActionObject::valueToInt(std::string& value)
{
return atoi(value.c_str());
}
bool ActionObject::valueToBool(std::string& value)
{
int intValue = valueToInt(value);
if (1 == intValue) {
return true;
}else{
return false;
}
}
float ActionObject::valueToFloat(std::string& value)
{
return atof(value.c_str());
}
void ActionObject::addActionNode(ActionNode* node)
{
if (node == NULL)
{
return;
}
m_ActionNodeList->addObject(node);
node->setUnitTime(m_fUnitTime);
}
void ActionObject::removeActionNode(ActionNode* node)
{
if (node == NULL)
{
return;
}
m_ActionNodeList->removeObject(node);
}
void ActionObject::play()
{
stop();
this->updateToFrameByTime(0.0f);
int frameNum = m_ActionNodeList->count();
for ( int i = 0; i < frameNum; i )
{
ActionNode* actionNode = (ActionNode*)m_ActionNodeList->objectAtIndex(i);
actionNode->playAction();
}
if (m_loop)
{
m_pScheduler->scheduleSelector(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f , kCCRepeatForever, 0.0f, false);
}
else
{
m_pScheduler->scheduleSelector(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f, false);
}
}
void ActionObject::play(CCCallFunc* func)
{
this->play();
m_CallBack = func;
CC_SAFE_RETAIN(m_CallBack);
}
void ActionObject::pause()
{
m_bPause = true;
}
void ActionObject::stop()
{
if (this->m_CallBack)
{
this->m_CallBack->release();
this->m_CallBack = NULL;
}
int frameNum = m_ActionNodeList->count();
for ( int i = 0; i < frameNum; i )
{
ActionNode* actionNode = (ActionNode*)m_ActionNodeList->objectAtIndex(i);
actionNode->stopAction();
}
m_pScheduler->unscheduleSelector(schedule_selector(ActionObject::simulationActionUpdate), this);
m_bPause = false;
}
void ActionObject::updateToFrameByTime(float fTime)
{
m_CurrentTime = fTime;
int nodeNum = m_ActionNodeList->count();
for ( int i = 0; i < nodeNum; i )
{
ActionNode* actionNode = (ActionNode*)m_ActionNodeList->objectAtIndex(i);
actionNode->updateActionToTimeLine(fTime);
}
}
void ActionObject::simulationActionUpdate(float dt)
{
bool isEnd = true;
int nodeNum = m_ActionNodeList->count();
for ( int i = 0; i < nodeNum; i )
{
ActionNode* actionNode = (ActionNode*)m_ActionNodeList->objectAtIndex(i);
if (actionNode->isActionDoneOnce() == false)
{
isEnd = false;
break;
}
}
if (isEnd)
{
if (m_CallBack != NULL)
{
m_CallBack->execute();
}
if (m_loop)
{
this->play();
}
else
{
m_bPlaying = false;
m_pScheduler->unscheduleSelector(schedule_selector(ActionObject::simulationActionUpdate), this);
}
}
}
NS_CC_EXT_END
RapidJSON的一些方便操作的宏定义 以前是一直使用CCJsonConventer去把JSON字符串转化为CCDictionary对象的,现在使用cocos2d-x3.x后,不推荐使用CCDictionary了,而且,JSON库也换成了rapidjson,不过我
cocos2dx 3.2 解决触摸事件中两个按钮同时响应的问题 上篇触摸机制讲解中提到过存在两个按钮同时响应的问题。问题描述我们一般使用EventListenerTouchOneByOne注册触摸事件,但是这里的触摸消息是按顺序依次
【cocos2dx】创建精灵的两种方法 一种为直接引用单张图片,但是这样效率不高,不推荐。第二中方法采用精灵帧的方法,通过工具TexturePacker将纹理打包成.plist文件,然后在进行访问。
标签: cocos2dx ActionManager播放动画回调问题
本文链接地址:https://www.jiuchutong.com/biancheng/372605.html 转载请保留说明!友情链接: 武汉网站建设