分析 :出现此问题要么就是我们的方法结构写错了要么就是根本没有这个方法但是看到别人有用过所以搜索了一下该方法在我们工程的quick-3.3\cocos\scripting\lua-bindings\manual\cocos2d目录下的lua_cocos2dx_manual.cpp的文件中找一下没有找见但是找见了CardinalSplineBy这个方法后知道可能官方没有把这个方法注册到Lua中所以我们仿照这个方法注册一下(如下:)
在这个文件中需要添加的代码段完全仿照 tolua_cocos2d_CatmullRomBy_create()的注册方法(如下:)
①
②
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
}
③
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);
}
④
最后我们运行一下工程吧把这个方法注册到Lua中哦 别忘记了哈我们Android执行build_native脚本的时候看看这个.cpp文件链接进去没啊。
⑤