记又一次升级Cocos2dx-3.3版本后在lua中 ccb动画完成回调setAnimationCompletedCallback修改方法

本文介绍了在Cocos2d-x 3.3版本升级后,如何在lua中修改ccb动画完成的回调函数。主要内容包括在lua_cocos2dx_cocosbuilder_auto.cpp中添加新方法,实现LuaCCBAnimationWrapper类,并在lua脚本中调用新的回调功能。

没有找到自动生成的,我是手动加进去的

写的较简陋

1.把下面的方法加到lua_cocos2dx_cocosbuilder_auto.cpp文件中,找到CCBAnimationManager的地方加进去就可以了

tolua_function(tolua_S, "setAnimationCompletedCallback", tolua_cocos2d_CCBAnimationManager_setAnimationCompletedCallback);

static int tolua_cocos2d_CCBAnimationManager_setAnimationCompletedCallback(lua_State* tolua_S)
{
if (NULL == tolua_S)
return 0;


int argc = 0;
cocosbuilder::CCBAnimationManager* self = nullptr;


#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertype(tolua_S,1,"cc.CCBAnimationManager",0,&tolua_err)) goto tolua_lerror;
#endif


self = static_cast<cocosbuilder::CCBAnimationManager*>(tolua_tousertype(tolua_S,1,0));
#if COCOS2D_DEBUG >= 1
if (nullptr == self) {
tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_CCBAnimationManager_setAnimationCompletedCallback'\n", NULL);
return 0;
}
#endif


argc = lua_gettop(tolua_S) - 1;


if (argc == 1)
{
#if COCOS2D_DEBUG >= 1
if(!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err))
goto tolua_lerror;
#endif


LUA_FUNCTION handler = toluafix_ref_function(tolua_S,2,0);
//ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, ScriptHandlerMgr::HandlerType::NODE);


LuaCCBAnimationWrapper* tmpCallback = new LuaCCBAnimationWrapper();
tmpCallback->setCallback(handler);


self->setAnimationCompletedCallback(tmpCallback, CC_CALLFUNC_SELECTOR(LuaCCBAnimationWrapper::animationCompletedCallback));


return 0;
}


luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n", "cc.CCBAnimationManager:setAnimationCompletedCallback",argc, 1);
return 0;


#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'tolua_cocos2d_CCBAnimationManager_setAnimationCompletedCallback'.",&tolua_err);
return 0;
#endif
}


2.实现LuaCCBAnimationWrapper文件

LuaCCBAnimationWrapper.h


#ifndef __LuaCCBanimationWrapper_H_
#define __LuaCCBanimationWrapper_H_

#include "cocos2d.h"

using namespace cocos2d;
using namespace std;

class LuaCCBAnimationWrapper : public cocos2d::Ref 
{

public:
    LuaCCBAnimationWrapper();
    ~LuaCCBAnimationWrapper();


    void animationCompletedCallback();
    void setCallback(unsigned nFuncID);


private:
    CallFunc *pCCCallFunc;
    unsigned int m_nFuncID;
};


#endif //__LuaCCBanimationWrapper_H_



LuaCCBAnimationWrapper.cpp



#include "LuaCCBAnimationWrapper.h"
#include "CCLuaEngine.h"


LuaCCBAnimationWrapper::LuaCCBAnimationWrapper()
:pCCCallFunc(NULL), m_nFuncID(0) {
}


LuaCCBAnimationWrapper::~LuaCCBAnimationWrapper() {
}


void LuaCCBAnimationWrapper::animationCompletedCallback() {
/**
//2.x
    if (0 != m_nFuncID) {
        CallFunc *CallFuncTmp = CallFunc::create(m_nFuncID);
        CallFuncTmp->execute();
        m_nFuncID = 0; //TODO 当动画循环播放时会报错,记得修改
    }
**/

//3.x
if (0 != m_nFuncID){
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->executeFunctionByHandler(m_nFuncID, 1);
m_nFuncID = 0; //TODO 当动画循环播放时会报错,记得修改
}
}


void LuaCCBAnimationWrapper::setCallback(unsigned int nFuncID) {
    CCLOG("setCallback id: %i ", nFuncID);
    m_nFuncID = nFuncID;
}

记得要在上一个文件lua_cocos2dx_cocosbuilder_auto.cpp中导入头文件引用


3.lua中要修改CCBReaderLoad.lua文件,无需大改动,只需将ccbReader加在返回值返回即可

4.然后在lua中可直接使用了

local animationManager = ccbReader:getActionManager()
    animationManager:setAnimationCompletedCallback(function()
        print("setAnimationCompletedCallback")
    end)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值