码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d-x 3.8 lua 关于setAnimationCompletedCallback的修改

时间:2016-04-10 14:26:48      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

最近想实现使用ccb动画播放攻击的时候,加点绚丽的特效,在其CCBAnimationManager类中,也找到了相对应的接口:

/**
     * when this function bound to js ,the second param are callfunc_selector
     * @lua NA
     */
    void setAnimationCompletedCallback(cocos2d::Ref *target, cocos2d::SEL_CallFunc callbackFunc)

不过可惜的是,未发现lua与c++的交互调用,唯一的方法就是补充添加了哦!

 

1. 打开cocos2d_lua_bindings.xcodeproj/auto/lua_cocos2dx_cocosbuilder_auto.cpp找到:

int lua_register_cocos2dx_cocosbuilder_CCBAnimationManager(lua_State* tolua_S)

该方法不用多说,大家一看其代码就会明白了,我们在其中添加其代码:

tolua_function(tolua_S,"setAnimationCompletedCallback",
  lua_cocos2dx_cocosbuilder_CCBAnimationManager_setAnimationCompletedCallback);

 然后在该类中,继续编写如下:

static int lua_cocos2dx_cocosbuilder_CCBAnimationManager_setAnimationCompletedCallback(lua_State* tolua_S)
{
    if (nullptr == 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 ‘lua_cocos2dx_cocosbuilder_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
        
        // 注意此处的LuaCCBAnimationWrapper
        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, "‘setAnimationCompletedCallback‘function has wrong number of arguments: %d\n", argc);
    return 0;
    
#if COCOS2D_DEBUG >= 1
tolua_lerror:
    tolua_error(tolua_S,"#ferror in function ‘lua_cocos2dx_cocosbuilder_CCBAnimationManager_setAnimationCompletedCallback‘.",&tolua_err);
    return 0;
#endif
}

2. 接着开始创建LuaCCBAnimationWrapper.h和LuaCCBAnimationWrapper.cpp的文件,其目录大家自定义,能够调用到,方便维护就行了哈。

(1) LuaCCBAnimationWrapper.h

#ifndef __LuaCCBanimationWrapper_H_
#define __LuaCCBanimationWrapper_H_

#include "cocos2d.h"

USING_NS_CC;
using namespace std;

class LuaCCBAnimationWrapper : public Ref
{
public:
    LuaCCBAnimationWrapper();
    ~LuaCCBAnimationWrapper();
    
    void animationCompletedCallback();
    void setCallback(unsigned nFuncID);
    
private:
    CallFunc *pCCCallFunc;
    unsigned int m_nFuncID;
};

#endif

(2) LuaCCBAnimationWrapper.cpp

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

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

LuaCCBAnimationWrapper::~LuaCCBAnimationWrapper()
{
    //
}

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

void LuaCCBAnimationWrapper::animationCompletedCallback()
{
    /*
     2.x
    if (0 != m_nFuncID)
    {
        CallFunc *CallFuncTmp = CallFunc::create();
        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; 
    }
}

 

参考:http://blog.csdn.net/playddt/article/details/43230045

 

cocos2d-x 3.8 lua 关于setAnimationCompletedCallback的修改

标签:

原文地址:http://www.cnblogs.com/SkyflyBird/p/5373968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!