--1.添加文件lua_spSkeletonData.hpp和lua_spSkeletonData.cpp
切换到引擎的frameworks/cocos2d-x/cocos/scripting/lua-bindings/manual/spine目录。
新建lua_spSkeletonData.hpp和lua_spSkeletonData.cpp两个文件。
--2.lua_spSkeletonData.hpp代码:
/*
*****************************************************************************************
*EvanJobs
*/
#ifndef LUA_SPSKELETONDATA_H
#define LUA_SPSKELETONDATA_H
#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif
//TOLUA_API int
TOLUA_API int register_spSkeletonData_manual(lua_State* L);
#endif // #ifndef LUA_SPSKELETONDATA_H
--3.lua_spSkeletonData.cpp代码:
/*
*****************************************************************************************
*EvanJobs
*/
#include "lua_spSkeletonData.hpp"
#include "tolua_fix.h"
#include "LuaBasicConversions.h"
#include "spine-cocos2dx.h"
using namespace spine;
//static int
static int lua_cocos2dx_SkeletonData_create(lua_State* L)
{
if (nullptr == L) {
return 0;
}
int argc = 0;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertable(L,1,"sp.SkeletonData",0,&tolua_err))goto tolua_lerror;
#endif
argc = lua_gettop(L)-1;
if (2 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!tolua_isstring(L, 2, 0, &tolua_err) ||
!tolua_isstring(L, 3, 0, &tolua_err))
{
goto tolua_lerror;
}
#endif
const char* skeletonDataFile = tolua_tostring(L,2, "");
const char* atlasFile = tolua_tostring(L,3, "");
spAtlas* atlas = spAtlas_createFromFile(atlasFile, NULL);
CCAssert(atlas, "Error reading atlas file.");
spSkeletonJson* json = spSkeletonJson_create(atlas);
spSkeletonData* data = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile);
CCAssert(data, json->error ? json->error : "Error reading skeleton data.");
spSkeletonJson_dispose(json);
tolua_pushusertype(L, (void*)data, "sp.SkeletonData");
tolua_register_gc(L, lua_gettop(L));
return 1;
}
luaL_error(L, "'create' function of SkeletonData has wrong number of arguments: %d, was expecting %d\n", argc,2);
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_cocos2dx_SkeletonData_create'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_SkeletonData_finalize(lua_State* L)
{
spSkeletonData* data = (spSkeletonData*)tolua_tousertype(L, 1, 0);
CC_SAFE_DELETE(data);
CCLOG("sp.SkeletonData freed");
return 0;
}
//static void
static void tolua_register_spSkeletonData(lua_State* tolua_S)
{
tolua_usertype(tolua_S,"sp.SkeletonData");
tolua_cclass(tolua_S,"SkeletonData","sp.SkeletonData","",lua_cocos2dx_SkeletonData_finalize);
tolua_beginmodule(tolua_S,"SkeletonData");
tolua_function(tolua_S,"create",lua_cocos2dx_SkeletonData_create);
tolua_endmodule(tolua_S);
std::string typeName = typeid(spSkeletonData).name();
g_luaType[typeName] = "sp.SkeletonData";
g_typeCast["SkeletonData"] ="sp.SkeletonData";
}
int register_spSkeletonData_manual(lua_State* L)
{
if (nullptr == L)
return 0;
tolua_open(L);
tolua_module(L, "sp", 0);
tolua_beginmodule(L, "sp");
tolua_register_spSkeletonData(L);
tolua_endmodule(L);
return 0;
}
--4.注册接口
切换到引擎的frameworks/cocos2d-x/cocos/scripting/lua-bindings/manual/spine目录。
打开文件lua_cocos2dx_spine_manual.cpp。
添加头文件 #include "lua_spSkeletonData.hpp"
在函数register_spine_module()中添加register_spSkeletonData_manual方法()。
#include "lua_spSkeletonData.hpp"
int register_spine_module(lua_State* L)
{
lua_getglobal(L,"_G");
if (lua_istable(L,-1))//stack:...,_G,
{
register_all_cocos2dx_spine(L);
register_all_cocos2dx_spine_manual(L);
/*
*****************************************************************************************
*EvanJobs
*/
register_spSkeletonData_manual(L);
}
lua_pop(L, 1);
return 1;
}
--5.扩展sp.SkeletonAnimation:create()
扩展sp.SkeletonAnimation:create()方法,使其接受sp.SkeletonData参数来创建。
static int lua_cocos2dx_CCSkeletonAnimation_createWithFile(lua_State* L)
{
if (nullptr == L)
return 0 ;
int argc = 0;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertable(L,1,"sp.SkeletonAnimation",0,&tolua_err))goto tolua_lerror;
#endif
argc = lua_gettop(L) - 1;
/*
*****************************************************************************************
*EvanJobs
*/
if (1 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(L,2, "sp.SkeletonData",0, &tolua_err)) goto tolua_lerror;
#endif
spSkeletonData* data = (spSkeletonData*)tolua_tousertype(L,2, 0);
auto tolua_ret = LuaSkeletonAnimation::createWithData(data);
int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1;
int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID :NULL;
toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret,"sp.SkeletonAnimation");
return 1;
/******************************************************************************************/
}else if (2 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!tolua_isstring(L,2, 0, &tolua_err) ||
!tolua_isstring(L, 3 ,0, &tolua_err))
{
goto tolua_lerror;
}
#endif
const char* skeletonDataFile =tolua_tostring(L, 2, "");
const char* atlasFile =tolua_tostring(L, 3, "");;
auto tolua_ret = LuaSkeletonAnimation::createWithFile(skeletonDataFile, atlasFile);
int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1;
int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID :NULL;
toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret,"sp.SkeletonAnimation");
return 1;
} else if (3 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!tolua_isstring(L,2, 0, &tolua_err) ||
!tolua_isstring(L, 3 ,0, &tolua_err) ||
!tolua_isnumber(L, 4 ,0, &tolua_err))
{
goto tolua_lerror;
}
#endif
const char* skeletonDataFile =tolua_tostring(L, 2, "");
const char* atlasFile =tolua_tostring(L, 3, "");
LUA_NUMBER scale = tolua_tonumber(L, 4, 1);
auto tolua_ret = LuaSkeletonAnimation::createWithFile(skeletonDataFile, atlasFile, scale);
int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1;
int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID :NULL;
toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret,"sp.SkeletonAnimation");
return 1;
}
// luaL_error(L, "'createWithFile' function of SkeletonAnimation has wrong number of arguments: %d, was expecting %d\n", argc, 2);
luaL_error(L,"'createWithFile' function of SkeletonAnimation has wrong number of arguments: %d, was expecting %d\n", argc,1);
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'createWithFile'.",&tolua_err);
#endif
return 0;
}
--6.LuaSkeletonAnimation的扩展
----1.LuaSkeletonAnimation.h代码:
class LuaSkeletonAnimation: public spine::SkeletonAnimation {
public:
static LuaSkeletonAnimation* createWithFile (constchar* skeletonDataFile, const char* atlasFile, float scale = 1);
LuaSkeletonAnimation (constchar* skeletonDataFile, const char* atlasFile, float scale = 1);
virtual ~LuaSkeletonAnimation();
/*
*****************************************************************************************
*EvanJobs
*/
staticLuaSkeletonAnimation* createWithData (spSkeletonData* data);
LuaSkeletonAnimation (spSkeletonData* data);
};
----2.LuaSkeletonAnimation.cpp中添加代码:
/*
*****************************************************************************************
*EvanJobs
*/
LuaSkeletonAnimation::LuaSkeletonAnimation (spSkeletonData* data)
: spine::SkeletonAnimation(data)
{
}
LuaSkeletonAnimation* LuaSkeletonAnimation::createWithData (spSkeletonData* data)
{
LuaSkeletonAnimation* node =new (std::nothrow)LuaSkeletonAnimation(data);
node->autorelease();
return node;
}
--7.项目中添加lua_spSkeletonData.hpp和lua_spSkeletonData.cpp
Xcode项目栏:cocos2d_lua_bindings.xcodeproj->manual->spine,鼠标右击,Add Files to "cocos2d_lua_bindings.xcodeproj",添加以上两个文件。
--8.勾选Target Membership
点击刚才添加的两个文件,最右侧属性栏,找到Target Membership,把libluacocos2d Mac和libluacocos2d iOS都勾选上。
--9.使用
local jsonPath = "hero/red/red.json"
local atlasPath = "hero/red/red.atlas"
local cacheData = sp.SkeletonData:create(jsonPath, atlasPath)
self.hero = sp.SkeletonAnimation:create(cacheData)
self.hero:setPosition(900, 300)
self:addChild(self.hero)
self.hero:setAnimation(0, "red_wait", true)
--10.android.mk修改
如果要打包android的话,由于新添加了问价别忘了在android.mk中添加lua_spSkeletonData.cpp
位置:
frameworks/cocos2d-x/cocos/scripting/lua-bindings/proj.android/Android.mk
添加:
#spine
LOCAL_SRC_FILES += ../manual/spine/lua_cocos2dx_spine_manual.cpp \
../manual/spine/LuaSkeletonAnimation.cpp \
../manual/spine/lua_spSkeletonData.cpp \
../auto/lua_cocos2dx_spine_auto.cpp