We can write cocos2dx on lua and call the cocos2dx api with lua because
The Connection established in functions such as lua_cocos2dx_auto. You can refer to and write the connection yourself.
For example, if I create a class such as building
class Building : public Node{public: Building(const std::string &fileName); static Building* create(const std::string &fileName); void setSelected(bool selected); void setPosition(float x, float y);
I create a connection based on lua_cocos2dx_auto, as shown below:
In
TOLUA_API int register_all_classes(lua_State* tolua_S)
Write in the function
lua_register_Building(tolua_S);
Here is the implementation
int lua_register_Building(lua_State* tolua_S){ tolua_usertype(tolua_S,"Building"); tolua_cclass(tolua_S,"Building","Building","cc.Sprite",NULL); tolua_beginmodule(tolua_S,"Building"); tolua_function(tolua_S,"create", lua_Building_create);Here is the implementation of lua_Building_create and other functions
Int lua_Building_create (lua_State * tolua_S) {int argc = lua_gettop (tolua_S)-1; if (argc! = 1) {CCLOG ("% s, % d ++ argc error: % d", _ FUNCTION __, _ LINE __, argc); return 0;} std: string arg0; if (luaval_to_std_string (tolua_S, 2, & arg0) = false) // two elements in the stack, the 2nd and-1 are both the same {CCLOG ("% s, % d ++ get argc error", _ FUNCTION __, _ LINE _); return 0;} Building * ret = Building: create (arg0); if (NULL! = Ret) {std: string className = "BayuArmature"; cocos2d: Object * dynObject = dynamic_cast
(Building *) ret); if (NULL! = DynObject) {int ID = ret? (Int) (dynObject-> _ ID):-1; int * luaID = ret? & (DynObject-> _ luaID): NULL; toluafix_pushusertype_ccobject (tolua_S, ID, luaID, (void *) ret, className. c_str ();} else {tolua_pushusertype (tolua_S, (void *) ret, className. c_str () ;}} else {lua_pushnil (tolua_S);} return 1 ;}
Finally, call the register_all_classes function in the AppDelegate function clock, and the registration is successful.
The following code
// register lua engine auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); register_all_classes(engine->getLuaStack()->getLuaState());
Then you can call the building class in lua.