In the cocos2dx3.0 lua tutorial, connect lua to the class that you write so that lua can call functions of its own class.

Source: Internet
Author: User

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.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.