VS build Lua development environment and Luabridge Register C + + class (COCOS2DX project) (ii)

Source: Internet
Author: User
Tags lua

In this article we register 2 classes to test the use of the Luabridge, before we begin, we use C + + to invoke the LUA function on the basis of the previous article. We add a Main.lua file to the resource folder. The LUA code is as follows:

1 function Init () 2     Print (1) 3 End

Then, add the following code in the Initscript () function:

1 std::string FilePath = Fileutils::getinstance ()->fullpathforfilename ("Main.lua "); 2     int ret = lual_dofile (m_luastate, Filepath.c_str ()); 3     if (! ret) 4     {5         runluafunc ("init"); 6     }

The Runluafunc function is as follows:

void Helloworld::runluafunc (std::string  funcName) {    new  luabridge::luaref (getglobal (M_luastate, Funcname.c_str ()));     Try     {        if (lua_func->isfunction ())        {            (*lua_func) ();        }    }     Catch (luaexception& e)    {        Cclog ("%s", E.what ());    }    }

OK, call success ...

The following adds a class, Uimanage class, for adding UI and registering control events ...

We move the initscript and Runluafunc functions into the Uimanage class. Modify the Initscript function as follows:

1 voidUimanage::initscript ()2 {3M_luastate =lual_newstate ();4 lual_openlibs (m_luastate);5 6 getglobalnamespace (m_luastate)7 8. Beginnamespace ("Game")9 Ten.beginclass<widget> ("Widgets") One. AddFunction ("Getchildbytag", &Widget::getchildbytag) A . Endclass () -  -.beginclass<uimanage> ("Uimanage") the. Addstaticfunction ("instance", &uimanage::getintance) -. AddFunction ("AddWidget", &uimanage::addwidget) - . Endclass () -  + . Endnamespace () -  +         ; A  atSTD::stringFilePath = Fileutils::getinstance ()->fullpathforfilename ("Main.lua"); -     intRET =lual_dofile (M_luastate, Filepath.c_str ()); -     if(!ret) -     { -Runluafunc ("Init"); -     } in     Else -     { toCclog ("Lua Error:%s", Lua_tostring (M_luastate,-1)); +     } -}

We register the getinstance and AddWidget functions into Lua, and then the Main.lua as follows:

Uimanage =function()    local PNL = Uimanage:addwidget ("  Loginui/loginui.exportjson")End

First get the Uimanage Singleton class, then call the function Addwidget,ok, find the interface successfully added.

Add: The calling code is as follows ...

void Helloworld::onenter () {    layer::onenter ();    Uimanage::getintance ()initscript ();}

The following adds events to the controls on the interface ....

Add the function as follows (void Addtoucheventlistener (Ccwidgettouchcallback callback)):

1 voidUimanage::addeventlisten (widget* UI, std::stringfuncName)2 {3STD::stringBeginname = FuncName +"_begin";4STD::stringMovename = FuncName +"_move";5STD::stringEndname = FuncName +"_end";6STD::stringCancelname = FuncName +"_cancel";7 8Auto Beginfunc =NewLuaref (Getglobal (M_luastate, Beginname.c_str ()));9Auto Movefunc =NewLuaref (Getglobal (M_luastate, Movename.c_str ()));TenAuto Endfunc =NewLuaref (Getglobal (M_luastate, Endname.c_str ())); OneAuto Cancelfunc =NewLuaref (Getglobal (M_luastate, Cancelname.c_str ())); A  -Ui->addtoucheventlistener ([Beginfunc, Movefunc, Endfunc, Cancelfunc] (ref*ref, Widget::toucheventtype type) -     { the         Switch(type) -         { -          CaseCocos2d::ui::widget::toucheventtype::began: -             if(beginfunc->isfunction ()) +             { -(*Beginfunc) (); +             } A              Break; at          Casecocos2d::ui::widget::toucheventtype::moved: -             if(movefunc->isfunction ()) -             { -(*Movefunc) (); -             } -              Break; in          Casecocos2d::ui::widget::toucheventtype::ended: -             if(endfunc->isfunction ()) to             { +(*Endfunc) (); -             } the              Break; *          Casecocos2d::ui::widget::toucheventtype::canceled: $             if(cancelfunc->isfunction ())Panax Notoginseng             { -(*Cancelfunc) (); the             } +              Break; A         } the     }); +}


Registering in Lua

1 . AddFunction ("addeventlisten", &uimanage::addeventlisten)

The LUA Code registration event is as follows:

1--Events2Clcickevent =3 {4Ends =function ()5Print1)6 End7 }8 9init =function ()TenLocal PNL = Uimanage:addwidget ("Loginui/loginui.exportjson") OneAddevent (pnl,clcickevent)--Add Event AUimanage:addeventlisten (PNL,"PNL") - End -  the  -  -Addevent = function (UI,Event) -_g["Pnlends"] =Event. Ends--Global Table C + +Direct access to pnlends calls in code event.ends +End

OK, event invocation succeeded

VS build Lua development environment and Luabridge Register C + + class (COCOS2DX project) (ii)

Related Article

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.