quick-cocos2d-x 3.3 final 中cc.CardinalSplineTo:create导出到lua



         最近做的一个quick-cocos2d-x 3.3 final项目中使用到了曲线运动CardinalSpline,发现cc.CardinalSplineBy:create是可以使用的,但是cc.CardinalSplineTo:create却是一个空值报错,让我感到有些疑惑,后来发现有朋友跟我的问题一样,http://www.cocoachina.com/bbs/read.php?tid-273815.html,的确是cc.CardinalSplineTo:create没有导出给lua,但是它的实现都已经在c++端做好了。

如果我们要在模拟器上运行cc.CardinalSplineTo:create,那么我们先找到 quick-3.3\cocos\scripting\lua-bindings\manual\cocos2d目录下的lua_cocos2dx_manual.cpp文件,打开之后全局搜索CardinalSplineTo,发现没有搜索到,但是CardinalSplineBy却可以搜到,所以我们可以仿照CardinalSplineBy,将CardinalSplineTo导出给lua端使用。下面共有三处需要添加修改:

1.

int register_all_cocos2dx_manual(lua_State* tolua_S)
{
    if (NULL == tolua_S)
        return 0;
    
    extendNode(tolua_S);
    extendScene(tolua_S);
    extendLayer(tolua_S);
    extendMenuItem(tolua_S);
    extendMenuItemImage(tolua_S);
    extendMenuItemLabel(tolua_S);
    extendMenuItemFont(tolua_S);
    extendMenuItemSprite(tolua_S);
    extendMenuItemToggle(tolua_S);
    extendMenu(tolua_S);
    extendScheduler(tolua_S);
    extendSequence(tolua_S);
    extendCallFunc(tolua_S);
    extendSpawn(tolua_S);
    extendCardinalSplineBy(tolua_S);
    extendCardinalSplineTo(tolua_S); //这一行是添加进去的
    extendCatmullRomBy(tolua_S);
    extendCatmullRomTo(tolua_S);
    extendBezierBy(tolua_S);
    extendBezierTo(tolua_S);
    extendDrawNode(tolua_S);
    extendSprite(tolua_S);
    extendLayerColor(tolua_S);
    extendLayerMultiplex(tolua_S);
    extendParticleSystem(tolua_S);
    extendFileUtils(tolua_S);
    extendUserDefault(tolua_S);
    extendGLProgram(tolua_S);
    extendTexture2D(tolua_S);
    extendSpriteBatchNode(tolua_S);
    extendEventListenerKeyboard(tolua_S);
    extendEventListenerTouchOneByOne(tolua_S);
    extendEventListenerTouchAllAtOnce(tolua_S);
    extendEventListenerMouse(tolua_S);
    extendEventListenerCustom(tolua_S);
    extendEventListenerAcceleration(tolua_S);
    extendActionCamera(tolua_S);
    extendGridAction(tolua_S);
    
    extendMotionStreak(tolua_S);
    extendAtlasNode(tolua_S);
    extendParticleBatchNode(tolua_S);
    extendLabel(tolua_S);
    extendTMXTiledMap(tolua_S);
    extendConsole(tolua_S);
    extendGLProgramState(tolua_S);
    extendOrbitCamera(tolua_S);
    extendTMXLayer(tolua_S);
    extendApplication(tolua_S);
    extendTextureCache(tolua_S);
    extendGLView(tolua_S);
    extendCamera(tolua_S);
    return 0;
}

2.

int lua_cocos2d_CardinalSplineTo_create(lua_State* tolua_S)
{
    if (NULL == tolua_S)
        return 0;
    
    int argc = 0;
    bool ok = true;
    
#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
    if (!tolua_isusertable(tolua_S,1,"cc.CardinalSplineTo",0,&tolua_err)) goto tolua_lerror;
#endif
    
    argc = lua_gettop(tolua_S) - 1;
    
    if (argc == 3)
    {
        double dur = 0.0;
        ok &= luaval_to_number(tolua_S, 2, &dur, "cc.CardinalSplineTo:create");
        if (!ok)
            return 0;
        
        int num = 0;
        cocos2d::Vec2 *arr = NULL;
        ok &= luaval_to_array_of_vec2(tolua_S, 3, &arr, &num, "cc.CardinalSplineTo:create");
        if (!ok)
            return 0;
        
        double ten = 0.0;
        ok &= luaval_to_number(tolua_S, 4, &ten, "cc.CardinalSplineTo:create");
        if (!ok)
        {
            CC_SAFE_DELETE_ARRAY(arr);
            return 0;
        }
        
        if (num > 0)
        {
            PointArray* points = PointArray::create(num);
            
            if (NULL == points)
            {
                CC_SAFE_DELETE_ARRAY(arr);
                return 0;
            }
            
            for( int i = 0; i < num; i++) {
                points->addControlPoint(arr[i]);
            }
            
            CC_SAFE_DELETE_ARRAY(arr);
            CardinalSplineTo* tolua_ret = CardinalSplineTo::create(dur, points, ten);
            if (NULL != tolua_ret)
            {
                int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1;
                int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL;
                toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.CardinalSplineTo");
                return 1;
            }
        }
    }
    
    luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n", "cc.CardinalSplineTo:create", argc, 3);
    return 0;
    
#if COCOS2D_DEBUG >= 1
tolua_lerror:
    tolua_error(tolua_S,"#ferror in function 'lua_cocos2d_CardinalSplineTo_create'.",&tolua_err);
    return 0;
#endif
}


3.

static void extendCardinalSplineTo(lua_State* tolua_S)
{
    lua_pushstring(tolua_S,"cc.CardinalSplineTo");
    lua_rawget(tolua_S,LUA_REGISTRYINDEX);
    if (lua_istable(tolua_S,-1))
    {
        lua_pushstring(tolua_S,"create");
        lua_pushcfunction(tolua_S,lua_cocos2d_CardinalSplineTo_create);
        lua_rawset(tolua_S,-3);
    }
    lua_pop(tolua_S, 1);
}


其中,1步骤加注释的那一行加在相应的位置,2和3都是直接添加到文件中。

现在我们打开  quick-3.3\quick\player\proj.win32\player.sln文件,生成项目,进行编译,就可以发现我们的player中CardinalSplineTo不再是控制了。

如果要在android上编包运行,那么我们可以在当前项目下的 \frameworks\cocos2d-x\cocos\scripting\lua-bindings\manual\cocos2d目录中打开lua_cocos2dx_manual.cpp文件,按照之前的三个步骤进行修改添加,然后重新编译,运行build_native.bat,打包在手机上即可。




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。